DataTable.AcceptTChanges in loop -> ArgumentOutOfRangeException



我从SQLite DB(包含多个表)获取数据,并将它们加载到选项卡中的dataGridView中。我想通过在循环中使用 DataTable 对象的 AcceptChanges() 函数初始化每个表的每个行状态(->"未更改"),但我遇到以下错误消息:

抛出的异常:mscorlib中的"System.ArgumentOutOfRangeException".dll

Rem. 0 : 异常发生在循环中的"随机"第 n 个表(共 16 个)处。就我而言,它首先在第 9n 个表中,然后在添加新代码行后,它实际上在第 5 个表中。

Rem. 1 : 我还检查了所有数据表对象是否有自己唯一的表名。

List<String> tableList = SQLITE.getDBElement(SQLITE.getCnx(SQLITE.path), "table");
foreach (String name in tableList)
    {
        // Creating tab and data gridview
        TabPage tabPage = new TabPage(name);
        tabPage.Name = name;
        tabControl1.TabPages.Add(tabPage);
        tabPage.AutoScroll = true;
        DataGridView dataGridView = new DataGridView();
        dataGridView.Name = name;
        dataGridView.AutoSize = true;
        tabPage.Controls.Add(dataGridView);
        // Get table datas
        DataTable table = SQLITE.getTableContent_v2(SQLITE.getCnx(SQLITE.path), name);
        dataGridView.DataSource = table;
        // Add row change event
        table.RowChanged += new DataRowChangeEventHandler(Table_RowChanged);
        // Init lines status
        table.AcceptChanges(); //-> throw an exception after several loops
        }

雷姆2:没有最后一行"表。AcceptChanges();",没有错误(所有带有其 dataGridView 的选项卡都加载了数据。

Rem. 3 : 如果我手动做一个"表.AcceptChanges();" 通过按钮单击事件,它适用于所有加载的表。

解决方法

:交换这两行

// Add row change event
table.RowChanged += new DataRowChangeEventHandler(Table_RowChanged);
// Init lines status
table.AcceptChanges(); //-> throw an exception after several loops

AcceptChanges() 可以抛出 RowChanged 事件,在这种情况下我有例外。

最新更新