ADGV C#DataGridView - 更新查询错误?

时间:2017-10-01 15:17:46

标签: c# .net datagridview sql-update

我正在尝试使用自动生成的advancedDataGrid - ADGV(adgv.codeplex.com)。

问题是,每次更新SQL表时,下面显示的过程都没有成功...它正好是每隔一个事件 ...

private void advancedDataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
    {
        int rowIdx = advancedDataGridView1.CurrentCell.RowIndex;
        DataRowView drv = (DataRowView)advancedDataGridView1.Rows[rowIdx].DataBoundItem;
        DataRow dr = drv.Row;
        this.levTableAdapter.Update(dr); //strange, but Update is done every second time....
    }

如何调试?如果我添加另一行this.levTableAdapter.Update(dr) - 它也无济于事......

编辑: 再看一下行为,我必须更新:

1st enter "A" - noChangeSQL, Event picks "A", "A" in the datagrid cell.
2nd enter "B" -  "A" in SQL, Event picks "A", "A" in cell.
3rd enter "C" -  "A" in SQL, Event picks "C", "C" in cell.
4rd enter "D" -  "C" in SQL, Event picks "C", "C" in cell.
5th enter "E" -  "C" in SQL, Event picks "E", "E" in cell.

1 个答案:

答案 0 :(得分:1)

你确定,这是每一次活动吗?难道它不会只保存您更改的最后一行吗?如果是计时问题或执行顺序问题(如处理EndEdit事件后发生行状态更新),您可以使用Form的BeginInvoke推迟它,如下所示:

private void advancedDataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
    int rowIdx = advancedDataGridView1.CurrentCell.RowIndex;
    DataRowView drv = (DataRowView)advancedDataGridView1.Rows[rowIdx].DataBoundItem;
    DataRow dr = drv.Row;
    BeginInvoke((Action)(() => SaveRowChanges(dr)));
}

private void SaveRowChanges(DataRow dr)
{
    DataRow[] rows = { dr };
    adapter.Update(rows);
}