0
我需要幫助時,過濾或檢查如果一個或多個datagridview單元格爲空或不保存數據之前。我嘗試了幾個代碼,但總是有錯誤。下面是圖片。如何通過vb.net中的按鈕單擊來檢查datagridview單元格是否爲空?
在此先感謝。
我需要幫助時,過濾或檢查如果一個或多個datagridview單元格爲空或不保存數據之前。我嘗試了幾個代碼,但總是有錯誤。下面是圖片。如何通過vb.net中的按鈕單擊來檢查datagridview單元格是否爲空?
在此先感謝。
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代碼please..Thanks –
翻譯爲vb。我希望它有幫助 –
先生我已經嘗試過您的建議代碼,但它仍然保存,雖然有空單元 –