简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM