1

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

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

我的网格有 3 行,这里新行的索引为 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 是否有用

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

谢谢孙

4

2 回答 2

2

MSDN 是这样说的:

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

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

于 2012-05-08T19:59:08.263 回答
0

没有把握。尝试用 System.Diagnostics.Debug.WriteLine 替换 MessageBox.Show 语句,并在 RowsValidating 事件中执行相同操作。在这种情况下,MessageBox 可能会导致问题,因为它“暂停”了处理。

private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) 
{ 
    System.Diagnostics.Debug.WriteLine(e.Context.ToString()); 
    e.Cancel = true; 
} 
于 2012-05-08T19:53:44.133 回答