datagridview计算特定数据

时间:2015-11-18 09:35:18

标签: vb.net linq datagridview

在DataGridView(vb.net10)中,我希望能够查找特定的数据 这可能是一个数字或字符串。例如,让我们取字符串“阳光”。所以,在我的专栏中,我想计算“阳光”出现的次数,然后把它放在一个texbox中。问题因事实而复杂化很多列根本没有数据。(null)。有一种方法可以循环遍历列,计算空值并计算某列数据出现在列中的次数(在这个案例是阳光)。我已经设法在vba中做到这一点,但无法在vb.net中破解它。任何帮助赞赏 looknow

1 个答案:

答案 0 :(得分:2)

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

使用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()

我推荐LINQ,因为它比循环方法

更好