我的SQL表中有两个字段:Name
和Status
。当我在datagridview和数据集上更新或更改Name
的任何内容时。它也应该更新Status
。我该怎么做?
答案 0 :(得分:2)
你有很多选择。
例如:
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示例:
作为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";
}