Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Comments
Maciej Los 12-Aug-13 4:12am    
Do you mean a header is checked or not?

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:

VB
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



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. If DataGridView is databound:

We can make use of DataView[^] class or DataTable.Select[^] method. See the examples in the attached link.
 
Share this answer
 
v2
Comments
Maciej Los 12-Aug-13 17:37pm    
+5!
Omkaara 14-Aug-13 7:36am    
ya this is also cool one !!!
try this :
VB
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
 
Share this answer
 
i coded the easy solution for this


VB
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



where dgvInv is my datagridview
 
Share this answer
 
v2
Comments
Shahan Ayyub 14-Aug-13 14:11pm    
You are still missing "Exit for" after "chkCOl = True" otherwise there will be a lack in performance. Every time your loop will run till the end when you are only interested in finding either checked or unchecked. Hope it makes sense :)
Omkaara 16-Aug-13 6:11am    
opps i forgot
yogesh vaidya 21-Jun-18 20:13pm    
i have 3 columns in datagridview 1 is prod_name 2 is prod_qty and 3 is chkbox
i want a solution that any of above columns was clicked i want to chkbox value=true for same row

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900