数据错误对话框 [英] DataError Dialog Box

查看:30
本文介绍了数据错误对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个 datagridview 默认错误对话框.它说处理数据错误事件.我正在从数据表加载 myy DGV 中的数据.当我尝试添加新行时,我单击带有 (*) 的空行.当我开始在新行单元格中输入一些值时,数据错误会显示并进入无限循环.

I am getting a datagridview default error dialog box. There it said handle the dataerror event. I am loading the data in myy DGV from a datatable. When I try to add a new row, I click on the empty row with a (*). When I start typing some values in the new row cell, then the dataerror is shown and goes in a infinte loop.

DataGridViewDataError.context = display. 
Error: Index 3 does not have a value. 

我的网格有 3 行,这里新行的索引为 3.

My grid has 3rows and here the new row will have index 3.

这是加载DGV的代码

 private void frmPlant_Load(object sender, EventArgs e)
    {
        Program.fMain = this;
        Program.LoadAllForms();
        string selectCommand = "select * from T1";
        FillData(selectCommand);

    }
    private void FillData(string selectCommand)
    {
        using (SQLiteConnection conn = new SQLiteConnection(connString))
        {
            conn.Open();
            dataGridView1.AutoGenerateColumns = true;

            da = new SQLiteDataAdapter("selectCommand", connString);
            ds = new DataSet();

            commandBuilder = new SQLiteCommandBuilder(da);

            da.Fill(ds, "T1");
            dt = ds.Tables[0];
            dataGridView1.DataSource = ds.Tables[0];
            dataGridView1.DataMember = "T1";
            dataGridView1.Columns["TID"].ReadOnly = true;
        }

    }

我不知道这在代码中的确切位置发生.处理了 DataError 事件.不确定 UserAddedRows、RowsValidating 是否有用

I don't know where exactly in the code this is happening. Handled the DataError event. Not sure if UserAddedRows, RowsValidating would be useful

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            MessageBox.Show(e.Context.ToString());
            e.Cancel = true;
        }

谢谢太阳

推荐答案

MSDN 说明如下:

与此事件关联的 DataGridViewDataErrorEventArgs 对象的 ColumnIndex 和 RowIndex 属性通常指示发生数据错误的单元格.但是,当错误发生在外部数据源中时,数据源可能不提供发生错误的列.在这种情况下,ColumnIndex 属性通常指示出错时当前单元格的列.

The ColumnIndex and RowIndex properties of the DataGridViewDataErrorEventArgs object associated with this event normally indicate the cell in which the data error occurred. When the error occurs in an external data source, however, the data source may not provide the column in which the error occurred. In this case, the ColumnIndex property typically indicates the column of the current cell at the time of the error.

使用这些属性找出错误发生在哪一列.

Use those properties to find out in which column does the error occur.

这篇关于数据错误对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆