C# 什么事件得到';在datagridview的单元格中输入数据时激发
正如标题所示,当用户在单元格中输入文本时,会触发什么事件。我不想使用此事件来处理某些验证 到目前为止,我使用的是C# 什么事件得到';在datagridview的单元格中输入数据时激发,c#,.net,datagridview,C#,.net,Datagridview,正如标题所示,当用户在单元格中输入文本时,会触发什么事件。我不想使用此事件来处理某些验证 到目前为止,我使用的是cellvalizing事件,但问题是每当用户单击单元格时,它也会被调用。其中,我需要一个只在输入数据后调用的事件,以便执行验证 private void totalPurchaseDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { if (e.C
cellvalizing
事件,但问题是每当用户单击单元格时,它也会被调用。其中,我需要一个只在输入数据后调用的事件,以便执行验证
private void totalPurchaseDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex==0)
{
SqlDataAdapter ad = new SqlDataAdapter(@"SELECT id from Customer", connection);
DataTable dt = new DataTable();
ad.Fill(dt);
int value = int.Parse(e.FormattedValue.ToString());
DataRow[] dr = dt.Select("id = " + value);
if (!dr.Any())
{
totalPurchaseDataGridView.Rows[e.RowIndex].ErrorText = "Foreign key problem";
e.Cancel = true;
}
Form2 second = new Form2();
this.AddOwnedForm(second);
second.Show();
}
}
尝试datagridview cellendedit事件
private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==0)
{
SqlDataAdapter ad = new SqlDataAdapter(@"SELECT id from Customer", connection);
DataTable dt = new DataTable();
ad.Fill(dt);
int value = int.Parse(e.FormattedValue.ToString());
DataRow[] dr = dt.Select("id = " + value);
if (!dr.Any())
{
message.show("Foreign key problem");
}
else {
Form2 second = new Form2();
this.AddOwnedForm(second);
second.Show();
}
}
}
查看查找所有可用事件及其描述的。CellEndEdit的问题是,我无法获得与上面代码中类似的单元格值,我使用了
e.FormattedValue.ToString()
e.FormattedValue.ToString()单元格当前值?dataGridView1.CurrentCell.value-获取CurrentCell值或dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].ValueOk现在还有另一个问题。在我的代码中,如果验证失败,我只需调用e.cancel=true
停止对基础数据进行更改。但是,在此事件中,Eventargs没有cancel属性,因此,如果所做的更改无效,如何取消这些更改。我理解,但是否有任何方法可以取消该事件,以便在验证失败的情况下不会修改基础属性。