簡體   English   中英

如何在datagridview中計算帶有空白單元格的行

[英]how to count the rows with blank cells in 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

下面的代碼遍歷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

與@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 RowsTrue ,那么這也將排除新行,如果不允許的話,也沒關系。

  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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM