如何删除datagirdview中的所有空行? [英] How to delete all the empty rows in a datagirdview?
本文介绍了如何删除datagirdview中的所有空行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想删除列中的空行。这是我尝试但不起作用的,我收到一个错误,因为使用'new'关键字创建一个对象实例!感谢您的帮助。
我的尝试:
I would like to delete the empty rows from a column. Here is what I've tried but does not work, I am getting an error as "use 'new' keyword to create an object instance"! Your help would be appreciated, thanks.
What I have tried:
For i= data.Rows.Count - 1 To 0 Step -1
Dim row As DataGridViewRow = data.Rows(i)
if (Not row.IsNewRow Andalso data.Rows(i).Cells(1).Value.Tostring() = "") Then
data.Rows.RemoveAt(i)
Else
End If
推荐答案
当你在一个集合上迭代并同时修改它时,你需要反向工作以避免跳过行。
When you're iterating over a collection and modifying it at the same time, you need to work in reverse to avoid skipping rows.
For i = data.Rows.Count - 1 To 0 Step -1
(如果这不能解决问题,那么你需要解释一下不工作意味着。)
我能够使用这种方法让它工作......
I was able to get it work using this approach...
For i= data.Rows.Count - 1 To 0 Step -1
If String.IsNullOrEmpty(row.cell(1).Value) Then
data.Rows.RemoveAt(i)
End If
这篇关于如何删除datagirdview中的所有空行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文