开发者

Null reference exception on column click in datagridview

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜