如何在DatagridView中查找每个元素的频率

时间:2017-04-02 20:01:53

标签: vb.net linq datagridview datatable elements

我有一个看起来像这样的DataGridView:

ColumnName
hello
hello
bye
hello
bye
crocodile
hello
crocodile

如何找到每个元素的数量?即Hello = 4,bye = 3,鳄鱼= 2 看到它们在DataGridView列中输出。

请帮忙

1 个答案:

答案 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

尝试一下,让我知道你的意见