I have a data grid on my form filled by a dataset. On the SelectionChanged
event I have this code:
int index = dataGridView1.CurrentRow.Index;
But when you click the column to sort the data, a null reference expection gets thrown.
I need to be able to get the current row they have selected and get data out of it. How c开发者_高级运维an I do this?
void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
var index = e.RowIndex;
}
Probably because when you select a column all row selections get removed. So therefore the CurrentRow
is null
.
You have to capture the selected item when selection occurs.
精彩评论