none
How to know how many checkboxes are checked in gridview

    Question

  • How to know how many  checkboxes are  checked in gridview

    i want no of checked checkboxes.. in gridview.. i am binding gridview with database..

    adil
    Saturday, August 09, 2008 4:40 PM

Answers

  •  You really need to loop through all the rows and check the value of the cell that corresponds to the checkbox column...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim counter As Int32 = 0

    For Each row As DataGridViewRow In DataGridView1.Rows
       If row.Cells("MyCheckboxColumn1").Value = True Then
           counter += 1
       End If
    Next

    MessageBox.Show("There are " & counter & " checkboxes checked.")

    End Sub

    • Marked as answer by adilahmed Sunday, August 10, 2008 8:55 AM
    Saturday, August 09, 2008 6:41 PM

All replies

  •  You really need to loop through all the rows and check the value of the cell that corresponds to the checkbox column...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim counter As Int32 = 0

    For Each row As DataGridViewRow In DataGridView1.Rows
       If row.Cells("MyCheckboxColumn1").Value = True Then
           counter += 1
       End If
    Next

    MessageBox.Show("There are " & counter & " checkboxes checked.")

    End Sub

    • Marked as answer by adilahmed Sunday, August 10, 2008 8:55 AM
    Saturday, August 09, 2008 6:41 PM
  • hi dig boy

    am displying student info in Datagrid. each student belongs to diffrent courses ..here what i am doing is i will approve 2 students per one course by clicking check box in Datagrid

    If i click more than 2   students in one paticular couse i want a alert ..  here how i write  code  for restrict checkboxes

    My tables:course and student

    course table have

    cid,

    courename,

    description,

    maxno..

     

    My student table:

    sid,

    sname,

    courseId

     



    adil
    Wednesday, August 13, 2008 4:33 PM
  • I'm not sure if I have the right field names for your DGV table (i'm guessing the courses table is in it) but maybe the Validating event could help you here.  It checks to see if there are more than two people checked off for the course Id of the row being checked.  If so then a message appears and the check is removed.  It is not tested so it could have bugs.

     
        Private Sub DataGridView1_CellValidating(ByVal sender As ObjectByVal e As System.Windows.Forms.DataGridViewCellValidatingEventArgs) Handles DataGridView1.CellValidating  
            If e.ColumnIndex = DataGridView1.Columns("MyCheckboxColumn").Index Then 
                Dim cid As Int32 = DataGridView1.Rows(e.RowIndex).Cells("cid").Value  'cid column assumed to be type INT.  
                Dim counter As Int32 = 0  
                For Each r As DataGridViewRow In DataGridView1.Rows  
                    If r.Cells("cid").Value = cid Then 
                        counter += 1  
                        If counter > 2 Then 
                            MessageBox.Show("You cannot have more than two students in this course.")  
                            e.Cancel = True 
                        End If 
                    End If 
                Next 
            End If 
        End Sub 
     

    Wednesday, August 13, 2008 4:57 PM
  • Hi Dig boy
    thanks for you code
    i implemented your code

    but i want to know how i add checkboxes to datagridview1

    ..I added through Addcolumns from  datagrid property

    after i excuted your code in  DataGridView1_CellValidating event

    i am getting this error
    Operation did not succeed because the program cannot commit or quit a cell value change.



    adil
    Thursday, August 14, 2008 10:54 AM
  • Hey adil,  I have to say that I have never seen that exception before and I am at a loss to explain it to you.  It would help if we knew which column the problem cell value belongs to.  Does it happen when the grid is loading (i.e. you never even get to use the grid)?  This might mean you need some kind of a bypass flag variable to temporarily turn off the validating until the grid's data is initially loaded.  It woul also mean that your data violates your 2-people per course rule before it gets put in the grid.

    Friday, August 15, 2008 11:49 AM