datagridview行单元格值

时间:2013-03-11 20:56:30

标签: c# winforms datagridview

我在网上搜索答案但没有找到答案。这是C#winforms。 有可能做这样的事情:

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;
}
}

我需要根据单元格[2]中的其他一些标准清除单元格[4]的现有内容。

感谢您的帮助。 莱恩

1 个答案:

答案 0 :(得分:2)

如果DataGridView是数据绑定的,则不应直接修改单元格的内容。相反,您应该修改数据绑定对象。您可以通过DataGridViewRow的DataBoundItem访问该对象:

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

如果绑定的对象不支持INotifyPropertyChanged事件,则刷新网格。

相关问题