【问题标题】:Updating a database record after datagridview cell value has been changed?更改 datagridview 单元格值后更新数据库记录?
【发布时间】:2010-11-02 08:23:17
【问题描述】:

我们曾经使用远点网格,这很容易,但现在我们将 datagridview 用于我们的表单。 我的问题是我需要更新一条记录,因为它在 datagridview 中发生了变化,我使用了 Cell Leave 事件。

private void dataGridView1_CellLeave(object sender, DataGridViewCellEventArgs e)
    {
        if (dataGridView1[e.ColumnIndex, e.RowIndex].Value == null)//incase it is a blanked cell
        {
            MessageBox.Show("Cell changed:" + e.ColumnIndex.ToString() + ":" + e.RowIndex.ToString() + " New Value:" + " null");

        }
        else
        {
            MessageBox.Show("Cell changed:" + e.ColumnIndex.ToString() + ":" + e.RowIndex.ToString() + " New Value:" + dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());

        }
    }

我通过更改一个值来测试上面的代码,然后按回车键(就像用户当前所做的那样),但它显示的不是更改的值,而是前一个。

我已经完成的一种解决方法是更新 datagridview 中的所有记录,问题是它比以前多花费大约 15 秒,这是我不想要的 - 另外,如果仅更改一条记录,则没有必要。

【问题讨论】:

    标签: datagridview


    【解决方案1】:

    好的,我想我可能已经找到了答案: 如果我将以下代码放在 if-else 语句之前,则单元格更改已被识别

        dataGridView1.EndEdit();
    

    之后,可以看到更改后的单元格值..看起来不错!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2017-07-18
      • 2021-04-10
      • 2011-09-13
      • 2021-12-11
      • 1970-01-01
      相关资源
      最近更新 更多