Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在vb.net中通过单击按钮检查datagridview单元格是否为空?_Vb.net_Datagridview - Fatal编程技术网

如何在vb.net中通过单击按钮检查datagridview单元格是否为空?

如何在vb.net中通过单击按钮检查datagridview单元格是否为空?,vb.net,datagridview,Vb.net,Datagridview,在保存数据之前,在筛选或检查一个或多个datagridview单元格是否为null时,我需要帮助。我试过几个代码,但总是有错误。下面是图片 提前感谢。您可以编写如下函数: For Each rw As DataGridViewRow In dataGridView1.Rows For i As Integer = 0 To rw.Cells.Count - 1 If rw.Cells(i).Value Is Nothing OrEls

在保存数据之前,在筛选或检查一个或多个datagridview单元格是否为null时,我需要帮助。我试过几个代码,但总是有错误。下面是图片


提前感谢。

您可以编写如下函数:

For Each rw As DataGridViewRow In dataGridView1.Rows
    For i As Integer = 0 To rw.Cells.Count - 1                  
        If rw.Cells(i).Value Is Nothing OrElse rw.Cells(i).Value = DBNull.Value OrElse  String.IsNullOrWhitespace(rw.Cells(i).Value.ToString()) Then
                  'empty
        End If
    Next
Next
Public Function IsDataGridViewEmpty(ByRef dataGridView As DataGridView) As Boolean
    Dim isEmpty As Boolean = True
    For Each row As DataGridViewRow In dataGridView.Rows
        For Each cell As DataGridViewCell In row.Cells
            If Not String.IsNullOrEmpty(cell.Value) Then
                If Not String.IsNullOrEmpty(Trim(cell.Value.ToString())) Then
                    isEmpty = False
                    Exit For
                End If
             End If
        Next
    Next
    Return isEmpty
 End Function
或使用Linq:

Public Function IsDataGridViewEmpty(ByRef dataGridView As DataGridView) As Boolean
    Dim isEmpty As Boolean = True
    For Each row As DataGridViewRow In From row1 As DataGridViewRow In dataGridView.Rows Where (From cell As DataGridViewCell In row1.Cells Where Not String.IsNullOrEmpty(cell.Value)).Any(Function(cell) Not String.IsNullOrEmpty(Trim(cell.Value.ToString())))
        isEmpty = False
    Next
    Return isEmpty
End Function

…先生,你能把它翻译成vb.net代码吗?…谢谢你的翻译。我希望它能帮上忙。如果我试过你建议的代码,但它仍然可以保存,尽管有空单元格。如果我找到了解决方案。我刚刚将“String.IsNullOrEmpty(rw.Cells(I.Value)”替换为“String.IsNullOrWhitespace(rw.Cells(I.Value.ToString())”。无论如何,非常感谢你的想法:-)