Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically set cell value in DataGridView?

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.

I'm trying something like this:

SetValueFromSerial (decimal newValue)
{
    dataGridView.CurrentCell.Value = newValue;
}

using a string doesn't help:

    dataGridView.CurrentCell.Value = newValue.ToString ();

In both cases, I don't see anything in the grid, and the underlying value is unchanged.

I did Google and search here, but I didn't find anything. (I may have missed something, perhaps something obvious, but I'm not utterly lazy.)

like image 759
XXXXX Avatar asked Oct 04 '09 12:10

XXXXX


2 Answers

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

like image 134
Thomas Levesque Avatar answered Sep 18 '22 15:09

Thomas Levesque


dataGridView1[1,1].Value="tes";
like image 27
arced Avatar answered Sep 20 '22 15:09

arced