0

正如标题所暗示的,当用户在单元格中输入文本时会触发什么事件。我不想使用这个事件来处理一些验证。

到目前为止,我正在使用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();
        }
    }
4

1 回答 1

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();
         }
        }    

}
于 2013-11-15T10:03:21.500 回答