繁体   English   中英

如何检查 vb.net 中的 Datagridview 列是否为空

[英]how to check Datagridview column if empty or not in vb.net

请任何人都可以帮助我。 请问如何使用vb.net检查datagridview中的整个特定列是否有值

我尝试了不同的方法,但我没有得到我想要的。

我使用此代码,但它只检查当前单元格而不是整个列

    If (String.IsNullOrWhiteSpace(mainform.DataGridView2.CurrentCell.Value.ToString())) Then
        MsgBox("Empty")
    Else
        MsgBox("Not Empty")
    End If

这是我在 datagirdview 中的示例数据

尝试这个

 For i = 0 To dgv.RowCount - 1

            If String.IsNullOrEmpty(dgv.Item("columnname", i).Value) Then

            End If

          

        Next

全部

要获取给定列的所有单元格是否都有值:

使用 LINQ:

Dim col = 1 'Or name
Dim allHaveValues = YourDataGridView.Rows.OfType(Of DataGridViewRow).
    Where(Function(r) Not r.IsNewRow).
    All(Function(r) r.Cells.OfType(Of DataGridViewTextBoxCell).
    Where(Function(c) c.OwningColumn.Index = col AndAlso 
    If(c.Value?.ToString(), String.Empty).Trim().Length > 0).Any())

MessageBox.Show(If(allHaveValues, "Yes", "No"))

或者,对于每个循环:

Dim col = "ColumnName"
Dim allHaveValues = True

For Each row As DataGridViewRow In YourDataGridView.Rows
    If Not row.IsNewRow Then
        If If(row.Cells(col).Value?.ToString(), String.Empty).Trim().Length = 0 Then
            allHaveValues = False
            Exit For
        End If
    End If
Next

MessageBox.Show(If(allHaveValues, "Yes", "No"))

任何

相反,如果您需要获取任何单元格是否具有值:

LINQ:

Dim anyHasValue = YourDataGridView.Rows.
    OfType(Of DataGridViewRow).
    Where(Function(r) Not r.IsNewRow AndAlso r.Cells.
    OfType(Of DataGridViewTextBoxCell).
    Any(Function(c) c.OwningColumn.Index = col AndAlso
    If(c.Value, String.Empty).ToString().Trim().Length > 0)).
    Any()

MessageBox.Show(If(anyHasValue, "Yes", "No"))

环形:

Dim col = 1
Dim anyHasValue = False

For Each r As DataGridViewRow In YourDataGridView.Rows
    If Not r.IsNewRow Then
        If If(r.Cells(col).Value, String.Empty).ToString().Length > 0 Then
            anyHasValue = True
            Exit For
        End If
    End If
Next

MessageBox.Show(If(anyHasValue, "Yes", "No"))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM