我有一个看起来像这样的DataGridView:
ColumnName
hello
hello
bye
hello
bye
crocodile
hello
crocodile
如何找到每个元素的数量?即Hello = 4,bye = 3,鳄鱼= 2 看到它们在DataGridView列中输出。
请帮忙
答案 0 :(得分:1)
查询DataGridView
可能有更好的方法,但循环和创建Linq
的群组也有效。所以这是我的想法:
Sub CountRows()
Dim lstCountRows as List(Of String)
For Each row As DataGridViewRow In MyDataGridView.Rows
'2 is the index of the column you are querying
lstCountRows.Add(row.Cells(2).Value)
Next
'Create groups for the values on lstCountRows
Dim groups = lstCountRows.GroupBy(Function(value) value)
'Loop and print the groups count
For Each group In groups
MsgBox(group(0) & "-" & group.Count)
Next
End Sub
尝试一下,让我知道你的意见