如何检查没有检查DataGridViewCheckBoxColumn的行 [英] how to check that no row of DataGridViewCheckBoxColumn has been checked
问题描述
如何检查没有检查DataGridViewCheckBoxColumn的行
datagridview 4列和x个行添加
第一列是DataGridViewCheckBoxColumn
i想知道没有一行复选框列是检查
how to check that no row of DataGridViewCheckBoxColumn has been checked
in datagridview 4 columns and x nos of rows added
1st column is DataGridViewCheckBoxColumn
i want to know that not a single row of checkbox column is check
推荐答案
从你的问题我假设两种可能性:
1)当DataGridView是 NOT 数据绑定时:
AFAIK,我们无法通过DataGridViewRowCollection获取行单元格的值。
一种方法是使用循环:
From your question I assume two possibilities:
1) When DataGridView is NOT databound:
AFAIK, we can not get the values of a row cell(s) without looping through DataGridViewRowCollection.
One way to do so is using loop:
Dim cnt As Integer = 0
For i As Integer = 0 To DataGridView1.Rows.Count - 1
With DataGridView1.Rows(i).Cells("NameOfYourColumn")
If (.Value = True) Then
cnt += 1
Exit For
End If
End With
Next
Debug.Print(cnt) ' If here it is greater than zero than some row is checked
使用LINQ :( 一行a pproach )
using LINQ: (one line approach)
DataGridView1.Rows.Cast(Of DataGridViewRow).Where(Function(c) c.Cells("NameOfYourColumn").Value = True).Count()
' If count > 0 then some row is checked
2。如果DataGridView是数据绑定:
我们可以使用DataView [ ^ ]类或 DataTable.Select [< a href =http://msdn.microsoft.com/en-us/library/det4aw50.aspxtarget =_ blanktitle =New Window> ^ ]方法。请参阅所附链接中的示例。
2. If DataGridView is databound:
We can make use of DataView[^] class or DataTable.Select[^] method. See the examples in the attached link.
试试这个:
try this :
dim x as integer = 0
for counter as integer = 0 to datagrid.rows.count -1
if datagrid.rows(counter).cells(0).value = true then
x = 1
exit for
else
x = 0
end if
if x = 0
MessageBox.Show("No records selected.", "Null Value", MessageBoxButtons.OK,MessageBoxIcon.Information)
Exit Sub
end if
我为此编写了简单的解决方案
i coded the easy solution for this
Dim chkCOl As Boolean = False
For i As Integer = 0 To dgvInv.Rows.Count - 1
If dgvInv(1, i).Value = True Then
chkCOl = True
' if any of row is checked will com in this if condition
'else i will get chkCOl as false
Exit For
End If
Next
其中dgvInv是我的datagridview
where dgvInv is my datagridview
这篇关于如何检查没有检查DataGridViewCheckBoxColumn的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!