[英]what event get's fired when data has been entered in the cell in datagridview
As the title suggests what event gets fired when the user has entered text in a cell. 如标题所示,当用户在单元格中输入文本时会触发什么事件。 I wan't to use this event for handling some validation.
我不想使用此事件来处理一些验证。
So far i am using CellValidating
event but the problem with it is that it also gets called whenever the user clicks in the cell. 到目前为止,我正在使用
CellValidating
事件,但是它的问题在于,每当用户单击单元格时,它也会被调用。 Where as i want an event that only gets called once the data has been entered so i can perform validation. 我想在哪里输入数据后才调用事件,以便执行验证。
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();
}
}
Try datagridview cellendedit event 尝试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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.