請任何人都可以幫助我。請問如何使用vb.net檢查datagridview中的整個特定列是否有值
我嘗試了不同的方法,但我沒有得到我想要的。
我使用此代碼,但它只檢查當前單元格而不是整個列
If (String.IsNullOrWhiteSpace(mainform.DataGridView2.CurrentCell.Value.ToString())) Then
MsgBox("Empty")
Else
MsgBox("Not Empty")
End If
這是我在 datagirdview 中的示例資料
uj5u.com熱心網友回復:
試試這個
For i = 0 To dgv.RowCount - 1
If String.IsNullOrEmpty(dgv.Item("columnname", i).Value) Then
End If
Next
uj5u.com熱心網友回復:
全部
要獲取給定列的所有單元格是否都有值:
使用 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"))
任何
相反,如果您需要獲取任何單元格是否具有值:
林克:
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"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/405788.html
標籤:
下一篇:洗掉VB.Net中的特定陣列字串
