删除最后一行数据绑定 DataGridView C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/180629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:52:15  来源:igfitidea点击:

Remove Last Row Databound DataGridView C#

c#datagridview

提问by C B

I am using VS 2008/C# and binding a local List of helper classes as the DataSource for a DataGridView control. Calling the Remove() method on my List of helper classes fires the CellFormatting event of the DataGridView, which makes sense (a bit).

我正在使用 VS 2008/C# 并绑定一个本地的辅助类列表作为 DataGridView 控件的数据源。在我的辅助类列表上调用 Remove() 方法会触发 DataGridView 的 CellFormatting 事件,这很有意义(有点)。

When removing whatever happens to be the DataBoundItem of the last row in the grid (so long as the grid has more than one row) the DataGridView's Rows collection is not updated before this event fires. So, in the CellFormatting event handler, I get an IndexOutOfRangeException as the Rows collection is one too large.

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

I've tried removing the row using the DataGridView.Rows.Remove() method, and binding using a BindingSource rather than binding the List directly as the data source.

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

I found a few references to this occurance via Google, but answers were either not forthcoming or said to use a Delete() method on either the DataGridView or the DataGridView.Rows collection - neither of which currently exist.

我通过 Google 找到了一些对这种情况的引用,但答案要么没有出现,要么据说在 DataGridView 或 DataGridView.Rows 集合上使用 Delete() 方法 - 目前两者都不存在。

Sorting does not appear to be the issue either, as performing/not performing a sort results in the same outcome.

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

The only exception to the "last row" being a problem for removal is if the DataGridView contains only one row - in which case everything works fine.

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

采纳答案by BFree

I've had this problem in the past, and if I remember correctly there's one of two things you can do. When you remove the record from the collection, set the datasource property on your datagridview to null, and then rebind it to your list. That should do the trick.

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

Alternatively, you can handle the DataError event on your dataGridview and in the method you can say e.Cancel = true to suppress the exception, or you can further deal with it there.

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

回答by inam101

It is a very old problem, but i solved it by handling the row-remove event, as follows.

这是一个非常古老的问题,但我通过处理 row-remove 事件解决了它,如下所示。

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

and it worked.

它奏效了。

回答by DCNYAM

I know this is an old question, but I found another solution that worked for me. Before deleting the item from the data source / binding source, un-wire the event handler for the CellFormatting event and then re-wire it after. For example:

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

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

回答by Caravansary

I have the same problem. I have found a solution. Try to copy all the rows to next row. then remove the first row of dgv. A sample code for this:

我也有同样的问题。我找到了解决办法。尝试将所有行复制到下一行。然后删除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)

try it:)

尝试一下:)

回答by Asad Naeem

To hide the last row, change the AllowUserToAddRows property of the datagridview as shown:

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

myDataGridView1.AllowUserToAddRows = false

回答by Airy

First just disable the property of Datagridviewas

首先只是禁用Datagridviewas的属性

dataGridView1.AllowUserToAddRows = false;

and then just remove the last rows as many rows as you want either with for loop by keeping -1.

然后通过保持-1,使用for循环删除最后几行。

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