简体   繁体   English

datagridview行单元格值

[英]datagridview row cell value

I have search the web for an answer but have not found it. 我在网上搜索答案但没有找到答案。 This is C# winforms. 这是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]. 我需要根据单元格[2]中的其他一些标准清除单元格[4]的现有内容。

Thanks for any help. 谢谢你的帮助。 Ryan 瑞安

If the DataGridView is databound, you shouldn't directly modify the content of the cell. 如果DataGridView是数据绑定,则不应直接修改单元格的内容。 Instead, you should modify the databound object. 相反,您应该修改数据绑定对象。 You can access that object through the DataBoundItem of the DataGridViewRow : 您可以通过DataGridViewRow的DataBoundItem访问该对象:

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

Refresh the grid afterwards if your bound object does not support INotifyPropertyChanged events. 如果绑定对象不支持INotifyPropertyChanged事件,则在之后刷新网格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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