【问题标题】:Remove Last Row Databound DataGridView C#删除最后一行数据绑定 DataGridView C#
【发布时间】:2010-09-15 21:20:16
【问题描述】:

我正在使用 VS 2008/C# 并将帮助程序类的本地列表绑定为 DataGridView 控件的数据源。在我的助手类列表上调用 Remove() 方法会触发 DataGridView 的 CellFormatting 事件,这是有道理的(有点)。

当删除网格中最后一行的 DataBoundItem 时(只要网格不止一行),DataGridView 的 Rows 集合在此事件触发之前不会更新。因此,在 CellFormatting 事件处理程序中,我得到了一个 IndexOutOfRangeException,因为 Rows 集合太大了。

我尝试使用 DataGridView.Rows.Remove() 方法删除行,并使用 BindingSource 进行绑定,而不是将 List 直接绑定为数据源。

我通过 Google 找到了一些关于此事件的引用,但答案要么没有出现,要么据说在 DataGridView 或 DataGridView.Rows 集合上使用 Delete() 方法 - 目前都不存在。

排序似乎也不是问题,因为执行/不执行排序会导致相同的结果。

“最后一行”作为删除问题的唯一例外是 DataGridView 仅包含一行 - 在这种情况下一切正常。

【问题讨论】:

    标签: c# datagridview


    【解决方案1】:

    首先将Datagridview的属性禁用为

    dataGridView1.AllowUserToAddRows = false;
    

    然后通过保持 -1 使用 for 循环删除任意数量的最后一行。

    dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
    dataGridView1.Rows.RemoveAt(dataGridView1.Rows.Count - 1);
    

    【讨论】:

      【解决方案2】:

      要隐藏最后一行,请更改 datagridview 的 AllowUserToAddRows 属性,如下所示:

      myDataGridView1.AllowUserToAddRows = false
      

      【讨论】:

        【解决方案3】:

        我也有同样的问题。我找到了解决方案。尝试将所有行复制到下一行。然后删除第一行dgv。示例代码:

            If Dgv.CurrentCell.RowIndex + 1 = Dgv.Rows.Count Then
                For m = Dgv.Rows.Count - 2 To 0 Step -1
                    Dgv.Rows(m + 1).Cells(0).Value = Dgv.Rows(m).Cells(0).Value
                    Dgv.Rows(m + 1).Cells(1).Value = Dgv.Rows(m).Cells(1).Value
                    Dgv.Rows(m + 1).Cells(2).Value = Dgv.Rows(m).Cells(2).Value
                    Dgv.Rows(m + 1).Cells(3).Value = Dgv.Rows(m).Cells(3).Value
        
                Next
                Dgv.Rows.RemoveAt(0)
                Exit Sub
            End If
            Dgv.Rows.Remove(Dgv.CurrentRow)
        

        试试看:)

        【讨论】:

          【解决方案4】:

          我知道这是一个老问题,但我找到了另一个对我有用的解决方案。在从数据源/绑定源中删除项目之前,取消连接 CellFormatting 事件的事件处理程序,然后重新连接它。例如:

          RemoveHandler DataGridView1.CellFormatting, AddressOf DataGridView1_CellFormatting
          Me.BindingSource1.Remove(item)
          AddHandler DataGridView1.CellFormatting, AddressOf DataGridView1_CellFormatting
          

          【讨论】:

            【解决方案5】:

            这是一个很老的问题,但是我通过处理row-remove事件解决了,如下。

            private void dgViewItems_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
            {
                 dataAdapter.Update((DataTable)bindingSource1.DataSource);
            }
            

            它成功了。

            【讨论】:

              【解决方案6】:

              我过去遇到过这个问题,如果我没记错的话,你可以做两件事之一。从集合中删除记录时,将 datagridview 上的数据源属性设置为 null,然后将其重新绑定到列表。这应该可以解决问题。

              或者,您可以在 dataGridview 上处理 DataError 事件,在方法中您可以说 e.Cancel = true 来抑制异常,或者您可以在那里进一步处理它。

              【讨论】:

              • 将此标记为一个疯狂的老问题的例外答案。 :-) 我相信我当时尝试了这个建议但没有成功。但它似乎对其他人有用。
              猜你喜欢
              • 1970-01-01
              • 2014-02-05
              • 1970-01-01
              • 2023-03-16
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-10-30
              相关资源
              最近更新 更多