从DataGridView删除行 [英] Remove row from DataGridView

查看:1056
本文介绍了从DataGridView删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了初始化我VouchersDGV数据网格视图我用下面的

  DGV.AllowUserToDeleteRows = TRUE
 对于i = 1到DGV.RowCount  -  1
     DGV.Rows.Remove(DGV.Rows(I  -  1))
     DGV.Refresh()
 下一个
 

但是,当我乳宁它在我第一次的错误

  

{未提交的新行不能删除。} System.InvalidOperationException

如果我会继续和运行我的code和我的数据网格写一个新行,我会尝试初始化一次(我现在有两行,一行有数据,另一个是空的),我借此错误

  

{未提交的新行不能删除。} System.InvalidOperationException

我不解决这个问题,请在那里有人来帮助我一下吗?

解决方案

 如果不DGV.Rows(我).IsNewRow然后
    DGV.Rows.RemoveAt㈠
结束如果
 

添加此条件下,以你的循环。另外,您也可以使用DGV.RejectChanges(),它会重置所有行状态,以原始的。

In order to initialize my VouchersDGV Data Grid View I'm Using the following

 DGV.AllowUserToDeleteRows = True
 For i = 1 To DGV.RowCount - 1
     DGV.Rows.Remove(DGV.Rows(i - 1))
     DGV.Refresh()
 Next

But when I'm runing it for the first time I take the error of

{"Uncommitted new row cannot be deleted."} System.InvalidOperationException

If I will continue and run my code and write a new row in my Data Grid and I will try to initialize again (now I have two rows, one has the data and the other is empty) I take this error

{"Uncommitted new row cannot be deleted."} System.InvalidOperationException

I can't solve it, Please is there someone to assist me on that?

解决方案

If Not DGV.Rows(i).IsNewRow Then
    DGV.Rows.RemoveAt(i)
End If

Add this condition to your loop. Alternatively, you could also use DGV.RejectChanges() which will reset all the row status to original.

这篇关于从DataGridView删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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