简体   繁体   English

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

[英]Remove Last Row Databound DataGridView C#

I am using VS 2008/C# and binding a local List of helper classes as the DataSource for a DataGridView control. 我正在使用VS 2008 / C#并将本地的辅助类列表绑定为DataGridView控件的DataSource。 Calling the Remove() method on my List of helper classes fires the CellFormatting event of the DataGridView, which makes sense (a bit). 在我的助手类列表上调用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. 当删除网格中最后一行的DataBoundItem时(只要网格有多行),DataGridView的Rows集合在此事件触发之前不会更新。 So, in the CellFormatting event handler, I get an IndexOutOfRangeException as the Rows collection is one too large. 因此,在CellFormatting事件处理程序中,我得到一个IndexOutOfRangeException,因为Rows集合太大了。

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只包含一行 - 在这种情况下一切正常。

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. 从集合中删除记录时,将datagridview上的datasource属性设置为null,然后将其重新绑定到列表中。 That should do the trick. 这应该够了吧。

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来抑制异常,或者您可以在那里进一步处理它。

First just disable the property of Datagridview as 首先,只需禁用Datagridview的属性即可

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);

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

myDataGridView1.AllowUserToAddRows = false

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. 它起作用了。

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. 在从数据源/绑定源中删除项目之前,取消连接CellFormatting事件的事件处理程序,然后重新连接它。 For example: 例如:

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

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. 然后删除第一行dgv。 A sample code for this: 一个示例代码:

    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:) 试试吧:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM