如何在datagridview中使用空白单元格计算行数

时间:2015-07-27 10:45:17

标签: vb.net datagridview

我是vb.net 2008的用户,目前正在为我们部门开发系统。我需要你的帮助来计算datagridview中有空单元格的行。行数必须显示在标签中。

这是我的代码。

For i = 0 To dgvMonitoringBoard.Rows.Count - 1
    If dgvMonitoringBoard.Rows(i).Cells(24).Value.ToString = " " Then
        x += 1
    End If
Next

lblForTransfer.Text = "Items for transfer to Purchasing:" & x

2 个答案:

答案 0 :(得分:1)

以下代码循环遍历DataGridView行和单元格。如果在任何行上找到任何空单元格,则计数器会增加,以计算具有空单元格的行数。

  Dim countRows as Integer = 0        
    For Each dgvRow As DataGridViewRow In dgvMonitoringBoard.Rows  'NOTE: Use dgvMonitoringBoard.Rows - 1  if AllowUserToAddRows property is set to True
        For Each dgvCell As DataGridViewCell In dgvRow.Cells
            If dgvCell.Value = "" Then
                countRows += 1
                Exit For
            End If
        Next
    Next

   lblForTransfer.Text = "Items for transfer to Purchasing: " + countRows.ToString

答案 1 :(得分:0)

与@equisde有点不同的方法。我会根据你的情况使用Linq。这将返回有DBNull.Value 空字符串的行数...

这是一个班轮......

 Dim count As Integer = DataGridView1.Rows.Cast(Of DataGridViewRow).Where(Function(r) r.Cells.Cast(Of DataGridViewCell).ToList.Any(Function(c) c.Value Is DBNull.Value OrElse String.IsNullOrEmpty(CStr(c.Value).Trim))).Count

这里自上而下 - 有时候更容易阅读......

   Dim count As Integer = DataGridView1.Rows.Cast(Of DataGridViewRow) _
                          .Where(Function(r) r.Cells.Cast(Of DataGridViewCell).ToList _
                          .Any(Function(c) c.Value Is DBNull.Value _
                          OrElse String.IsNullOrEmpty(CStr(c.Value).Trim))).Count

然后你就可以......

 lblForTransfer.Text = "Items for transfer to Purchasing: " & count.ToString

旁注:尝试不使用+进行字符串连接,使用&进行字符串连接。你也可以将它放在一个函数中并将DataGridView传递给它,这样你可以在任何需要的地方重复使用它并返回值。

每次请求更新

这是我编写的一个共享函数,它使用DataGridView对象和可选列索引来搜索你。此功能可以在您想要使用它的任何地方使用...注意:如果您有Allow Adding Rows True ,如果不允许,也会排除新行无所谓。

  Public Shared Function EmptyCount(ByVal dgrid As DataGridView, Optional ByVal intColumn As Integer = -1) As Integer
    Dim count As Integer = 0

    If dgrid IsNot Nothing AndAlso dgrid.Rows.Count > 0 Then
        If intColumn >= 0 Then 'Specific Column...
            If intColumn <= dgrid.Columns.Count Then
                count = dgrid.Rows.Cast(Of DataGridViewRow).Where(Function(rs) Not rs.IsNewRow) _
                    .Select(Function(r) r.Cells(intColumn)).Where(Function(r) r.Value Is DBNull.Value _
                     OrElse String.IsNullOrEmpty(CStr(r.Value))).Count
            End If
        Else 'Any columns...
            count = dgrid.Rows.Cast(Of DataGridViewRow).Where(Function(rs) Not rs.IsNewRow) _
                     .Where(Function(r) r.Cells.Cast(Of DataGridViewCell).ToList _
                     .Any(Function(c) c.Value Is DBNull.Value _
                     OrElse String.IsNullOrEmpty(CStr(c.Value).Trim))).Count
        End If
    End If

    Return count
End Function

使用示例

 'Include all columns...
 lblForTransfer.Text = "Items for transfer to Purchasing: " & EmptyCount(YOURDATAGRIDVIEWNAME).ToString

 'Specific column...
 lblForTransfer.Text = "Items for transfer to Purchasing: " & EmptyCount(YOURDATAGRIDVIEWNAME, 24).ToString