【问题标题】:Insert a Row with a sorted DataGridView插入带有排序的 DataGridView 的行
【发布时间】:2009-11-02 22:09:12
【问题描述】:

我的 DataGridView 和 Insert Form 绑定到同一个 BindingSource,Name 列是排序的。插入完成后,groupBindingSource.Current 不会返回新插入的 DataRowView,而是返回排序顺序中的最后一行,这使得 Update 什么都不做。

        FormGroup formGroup = new FormGroup();
        formGroup .Source = groupBindingSource;
        formGroup .setMode(FormGroup.Mode.Insert);
        if (formGroup .ShowDialog() == DialogResult.OK)
        {
            DataRowView drv = (DataRowView)groupBindingSource.Current;
            grupoTableAdapter.Update(drv.Row);
        }

【问题讨论】:

标签: c# winforms datagridview sql-server-ce


【解决方案1】:

你可以在插入之前禁用排序并在之后启用它吗?

类似这样的:

//...
//disable sorting
if (formGroup .ShowDialog() == DialogResult.OK)
{
    DataRowView drv = (DataRowView)groupBindingSource.Current;
    grupoTableAdapter.Update(drv.Row);
}
//enable sorting

【讨论】:

    【解决方案2】:
        DataRowView drv = (DataRowView)source.AddNew();
        grupoTableAdapter.Update(drv.Row);
        grupoBindingSource.Position = grupoBindingSource.Find("ID", drv.Row.ItemArray[0]);
    

    它使绑定的 DataGridView 相应地选择新添加的行,即使任何列被排序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多