Ryan
Ryan

Reputation: 672

datagridview row cell value

I have search the web for an answer but have not found it. This is C# winforms. Is is possible to do something like this:

private void datagridItems_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int ID;
DataGridViewRow row = this.datagridItems.Rows[e.RowIndex];
DataGridViewCell cell = row.Cells[e.ColumnIndex];

if (row.Cells[2].Value == some value)
{
//set the value of a cell
row.Cells[4].Value = new value;
}
}

I need to clear existing contents of the cell[4] based on some other criteria in cell[2].

Thanks for any help. Ryan

Upvotes: 0

Views: 8160

Answers (1)

Ryan Gunn
Ryan Gunn

Reputation: 1169

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;

Refresh the grid afterwards if your bound object does not support INotifyPropertyChanged events.

Upvotes: 2

Related Questions