2017-04-02 63 views
0

每個元素的頻率我有一個DataGridView看起來有點像這樣:如何找到在DataGridView中

ColumnName 
hello 
hello 
bye 
hello 
bye 
crocodile 
hello 
crocodile 

如何找到每個元素的計數?即Hello = 4,bye = 3,鱷魚= 2 看到它們在DataGridView列中輸出。

請幫

+0

你有沒有考慮通過列循環和計數呢? – JohnG

+0

如果你確實有一個DataTable,你可以查詢它。 – Plutonix

回答

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 

給它一個嘗試,讓我知道你的意見

相關問題