[英]How can I change another field when I update one DataGridView field?
我的SQL表中有2個字段: Name
和Status
。 當我更新或更改datagridview和數據集上的Name
中的任何內容時。 它也應該更新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示例:
作為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";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.