I have a datagridview that no matter what cell I click on returns an out of index error.
When I debug the following code I get a -1 value for e.ColumnIndex. As for e.RowIndex, I get the correct row that I have clicked on.
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
form2.value = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
}
I have tried to replace the e.ColumnIndex with a static int just to see if it works. When I add the int it doesn’t do anything when I click on a particular row or column.
Anyone any ideas how I can change the ColumnIndex or how I can work around this issue.
-1 for
e.ColumnIndexindicates that theRowHeaderis being selected. Are you sure you don’t have theRowSelectedevent bound to this event as well?In any case, it is good practice to have checks of
e.ColumnIndexande.RowIndexthat both are 0 or greater in anyCellClickevent- theRowHeaderandColumnHeadercells will also fire this event when clicked, and would not contain data, which would be somewhat confusing for users.