以编程方式更新所选行会错过 dgv.DataSource.GetChanges() 中的最后一行吗? [英] Programmatically updating selected rows misses the last one in dgv.DataSource.GetChanges()?

查看:23
本文介绍了以编程方式更新所选行会错过 dgv.DataSource.GetChanges() 中的最后一行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有列(组合框列)的数据网格视图.已实现以下功能.

I have a data grid view with a column (combobox column). The following function has been implemented.

  1. 选择多行(点击最左边的行标题并拖动).
  2. 以编程方式将值设置为某个值(见下文).所有选定的行都会发生变化.

  1. Select several rows (click the left most row header and drag).
  2. Programmingly set the value to something (see following). All the selected rows changes.

    foreach (DataGridViewRow item in dgv.SelectedRows)
    {
        item.Cells["cbxxxxx"].Value = p;
    }
    dgv.EndEdit();

  • 点击保存按钮保存更改.但是,最后一行被排除在 (dgv.DataSource as DataTable).GetChanges() 中.更改数据表的计数始终比所选行数少一.缺失的行是最后一行(带有黑色三角形).

  • Click the save button to save the changes. However, the last row is excluded in the (dgv.DataSource as DataTable).GetChanges(). The count of the changed data table is always one less than the selected rows. The missing row is the last one (with the black triangle).

    如何解决问题?

    推荐答案

    最后一行大概还是活动行,所以需要通过 BindingContext 结束

    The last row presumably is still the active row, so you need to end the edit through the BindingContext:

    foreach (DataGridViewRow item in dgv.SelectedRows) {
      item.Cells["cbxxxxx"].Value = p;
    }
    this.BindingContext[dgv.DataSource].EndCurrentEdit();
    

    这篇关于以编程方式更新所选行会错过 dgv.DataSource.GetChanges() 中的最后一行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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