简体   繁体   English

在datagridview的单元格中输入数据后触发什么事件

[英]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.

相关问题 当用户更改单元格值时捕获的 DataGridView 事件 - DataGridView Event to Catch When Cell Value Has Been Changed by User 当 datagridview 列自动排序时会触发什么事件? - What event is fired when a datagridview column is automatically sorted? 当数据源已经分配了数据时,为什么数据源显示为Datagridview的null? - why the datasource shows as null of the Datagridview, when it's already has been assigned with data? 检查事件是否至少被触发过一次 - Check if an event has been fired at least once 当数据输入到DataGridView的单元格中时,我可以在单元格验证之前修改其条目吗? - When data is entered into a cell of a DataGridView can I modify their entry before cell validation? 测试DataGridView以查看是否已选择Cell - Testing DataGridView to see if Cell has been Selected 数据已写入流时发生的事件? - Event when data has been written to stream? 是否未触发DataGridView行收藏夹事件? - DataGridView RowsAdded event not fired? 在wpf数据网格中对所有行进行排序后,将触发哪个事件。 - Which Event is fired after all row has been sorted in wpf data grid. 如何使单元格仅在填充后才能读取? 数据网格视图 C# - How can I make a cell to read only when it has been filled? DataGridView C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM