开发者

Null reference exception on column click in datagridview

开发者 https://www.devze.com 2023-02-28 06:38 出处:网络
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;

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

精彩评论

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