我的SQL表中有2个字段:Name
和Status
。当我更新或更改Name
datagridview和数据集上的任何内容时。它也应该更新Status
。我怎么做?
您有很多选择可以做到这一点。
例如:
CellValueChanged示例:
根据您的评论作为使用示例CellValueChanged
:
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
//Suppose 0 is the index of Name column and 1 is the index of Status Column
//We check if the change is in a datagrid view row and in name column
//Then we change value of Status column.
if (e.RowIndex >= 0 && e.ColumnIndex == 0)
this.dataGridView1.Rows[e.RowIndex].Cells[1].Value = "Modified";
}
或者,即使您没有将Status
列添加到网格中,也可以使用以下代码进行更改:
((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Status"] = "Modified";
ColumnChanged示例:
void table1_ColumnChanged(object sender, DataColumnChangeEventArgs e)
{
//Check if the event is raised for Name column
if (e.Column.ColumnName == "Name")
e.Row["Status"] = "Modified";
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句