How to programmatically set cell value in DataGridView?

前端 未结 14 873
無奈伤痛
無奈伤痛 2020-11-30 02:05

I have a DataGridView. Some of the cells receive their data from a serial port: I want to shove the data into the cell, and have it update the underlying bound object.

14条回答
  •  -上瘾入骨i
    2020-11-30 02:57

    If the DataGridView is databound, you shouldn't directly modify the content of the cell. Instead, you should modify the databound object. You can access that object through the DataBoundItem of the DataGridViewRow :

    MyObject obj = (MyObject)dataGridView.CurrentRow.DataBoundItem;
    obj.MyProperty = newValue;
    

    Note that the bound object should implement INotifyPropertyChanged so that the change is reflected in the DataGridView

提交回复
热议问题