由于标题表明当用户在单元格中输入文本时会触发哪个事件。我不想用这个事件来处理一些验证。
到目前为止,我正在使用CellValidating
事件,但问题是每当用户点击单元格时也会调用它。我想要一个只在输入数据后才被调用的事件,以便我可以执行验证。
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();
}
}
答案 0 :(得分:0)
尝试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();
}
}
}