vb.net datagridview 统计特定数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33776328/
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
datagridview counting specific data
提问by looknow
In DataGridView (vb.net10) I want to be able to look for a specific piece of data this could be a number or string.For the example lets take the string "sunshine".So in my column I want to count the number of times "sunshine" appears and then place this in a texbox.The problem is compounded by the fact that many of the columns have no data in them at all.(null).Is there a way I could loop through the column,account for the nulls and count the number of times a certain piece of data has appeared in the column(in this case sunshine) .I have managed to do this in vba,but cant crack it in vb.net.Any help appreciated looknow
在 DataGridView (vb.net10) 中,我希望能够查找特定的数据,这可以是数字或字符串。例如,让我们使用字符串“sunshine”。所以在我的专栏中,我想计算次“阳光”出现,然后把它放在一个 texbox 中。由于许多列根本没有数据这一事实使问题更加复杂。(空)。有没有办法我可以遍历列,说明空值并计算某条数据出现在列中的次数(在这种情况下是阳光)。我已经设法在 vba 中做到了这一点,但无法在 vb.net 中破解它。任何帮助赞赏looknow
回答by Vivek S.
Dim itemCount as integer
For i As Integer = 0 To DataGridView1.Rows.Count - 1
If (DataGridView1.Rows(i).Cells("Column").Value IsNot DBNull.Value or trim(DataGridView1.Rows(i).Cells("Column").Value <> Nothing ) AndAlso DataGridView1.Rows(i).Cells("Column").Value = "Sunshine" Then
'your logic
itemCount = itemCount + 1
End If
Next
By using LINQ
通过使用 LINQ
Dim count = (From row As DataGridViewRow In DataGridView1.Rows _
Where row.Cells("Your_Column_Name").Value = "Sunshine" And row.Cells("Your_Column_Name").Value IsNot DBNull.Value _
Select row.Cells("Your_Column_Name").Value).Count()
I recommend LINQ because it's more better than Looping method
我推荐 LINQ 因为它比循环方法更好

