开发者

Remove row from DataGridView

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, Ple开发者_运维问答ase 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.


If your goal is to empty the row collection, you can just use the Clear method of the Rows collection:

DGV.Rows.Clear()

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜