1
votes

I am using CellClick event to get the row and column clicked.

Now when the row header is clicked, it gives me the last column selected index.

This will create an issue for me since i need to perform actions on the current selected row and column. How can i tackle this? I have seen tons of help but all of these talk about column header click.

Here is my basic test to check the row and col index when cell is clicked

private void dgvUsers_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        if (dgvUsers.CurrentRow == null || dgvUsers.IsLoading()) return;

        var row = dgvUsers.CurrentCell.RowIndex; //CurrentRow.Index;
        var col = dgvUsers.CurrentCell.ColumnIndex;
        var ugactive = _ugActive;
    }

In the above code, IsLoading is just an extension method that is checking for a particular text placed inside the tag.

1

1 Answers

2
votes

In a DataGrdView CellClick() event, the current clicked cell can be determined inspecting the DataGridViewCellEventArgs e parameter, where e.RowIndex and e.ColumnIndex values report the reference indexes of the clicked cell Column and Row.

This also applies to Columns and Rows headers. In this case the value reported is negative (-1).

For example, if a Column header is clicked, e.ColumnIndex will report the index of the Column while the e.RowIndex will be -1. The opposite for a Row header.

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    //(...)
    var row = e.RowIndex;
    var col = e.ColumnIndex;
    //(...)
}