【问题标题】:How to find frequency of each element in DatagridView如何在 DatagridView 中查找每个元素的频率
【发布时间】:2017-04-02 20:01:53
【问题描述】:

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

ColumnName
hello
hello
bye
hello
bye
crocodile
hello
crocodile

如何找到每个元素的计数?即你好= 4,再见= 3和鳄鱼= 2 看到它们在 DataGridView 列中输出。

请帮忙

【问题讨论】:

  • 您是否考虑过遍历列并计算它们?
  • 如果你真的有一个DataTable,你可以查询它。

标签: vb.net linq datagridview datatable elements


【解决方案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

试一试,让我知道你的 cmets

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多