Shrad_k
Shrad_k

Reputation: 86

Datagridview cellclick event for next rows

I have two different button on datagridview as shown in snapshot on first click the action is performed correctly but on second click if sactioned or unsanctioned is click all the rows below the clicked till the end perform same action.

void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    // Ignore clicks that are not on button cells.
    if (e.RowIndex < 0 || e.ColumnIndex == dataGridView1.Columns["Status Cancel"].Index)
    {
        statusNotSanctioned(e);
        dataGridView1.ClearSelection();
        GetData();
        return;
    }

    if (e.RowIndex < 0 || e.ColumnIndex == dataGridView1.Columns["Status Confirm"].Index)
    {
        confirmLeave(e);
        dataGridView1.ClearSelection();
        GetData();
        return;
    }
    else
    {
        return;
    }
}

Upvotes: 1

Views: 359

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125197

void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //if click is on new row or header row
    if( e.RowIndex == dataGridView1.NewRowIndex || e.RowIndex < 0)
        return;

    //Handle First Button Click
    if( e.ColumnIndex  == dataGridView1.Columns["Your First Button"].Index)
    {
        //Do the stuff for first button click
        MessageBox.Show("First Button Clicked");
    }

    //Handle Second Button Click
    if( e.ColumnIndex == dataGridView1.Columns["Your Second Button"].Index)
    {
        //Do the stuff for second button click
        MessageBox.Show("Seccond Button Clicked");
    }

}

Upvotes: 1

Ivan Stoev
Ivan Stoev

Reputation: 205539

I don't see how the action can be performed correctly at all, since both if conditions in the snippet are wrong. They should be

if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Status Cancel"].Index)
{
    // ...
}
if (e.RowIndex >= 0 && e.ColumnIndex == dataGridView1.Columns["Status Confirm"].Index)
{
    // ...
}

Upvotes: 0

Related Questions