Identify checkbox.checked = True in a datagridview

I have a datagridview that uses a SQL query as the data source.  The last column of the datagridview is a checkbox column.  The code to generate the window is this:

        Dim cmd As SqlClient.SqlCommand = New SqlClient.SqlCommand
        cmd.Connection = cn
        cmd.CommandType = CommandType.Text

        cmd.CommandText = "SELECT ORDER_NUMBER, LINE_NUMBER, QUANTITY, '' AS ADD_WINDOW FROM PCT_PROD_BATCHING WHERE STATUS = 0 ORDER BY ORDER_NUMBER, LINE_NUMBER"
        cmd.CommandTimeout = 0

        Dim dataAdapter As SqlDataAdapter = New SqlDataAdapter(cmd.CommandText, cn)
        dataAdapter.SelectCommand.CommandTimeout = 0
        Dim builder As SqlCommandBuilder = New SqlCommandBuilder(dataAdapter)
        Dim table = New DataTable
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        dataAdapter.SelectCommand.CommandTimeout = 0
        dgvAddWindow.AutoGenerateColumns = False


        dgvAddWindow.Columns.Add(New DataGridViewTextBoxColumn With {.DataPropertyName = "ORDER_NUMBER", .Name = "Order Number"})
        dgvAddWindow.Columns.Add(New DataGridViewTextBoxColumn With {.DataPropertyName = "LINE_NUMBER", .Name = "Line Number"})
        dgvAddWindow.Columns.Add(New DataGridViewTextBoxColumn With {.DataPropertyName = "QUANTITY", .Name = "Quantity"})
        dgvAddWindow.Columns.Add(New DataGridViewCheckBoxColumn With {.DataPropertyName = "ADD_WINDOW", .Name = "Add Window"})

        dataAdapter.Fill(table)
        AddWindowSource.DataSource = table
        dgvAddWindow.DataSource = AddWindowSource

After selecting rows by checking the checkboxes, and then clicking a Process button, I need to do two things:

1.  Count the number of checkboxes that have been checked.
2.  For each checkbox that has been checked, run some other processes.

I know how to run the other processes, but I don't know how to cycle through the rows and see if the DataGridViewCheckBoxColumn is checked.  Thanks for your help.

T
T HoecherlDeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Karrtik IyerSoftware ArchitectCommented:
Something like below:
 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        For Each row As DataGridViewRow In _dgvProdBatch.Rows
'Use column name in Cells ("Your col Name") which has check box
            If Convert.ToBoolean(row.Cells("ADD_WINDOW").Value) = True Then   
                MsgBox(row.Cells(0).Value.ToString()) 'Karrtik: Do you work, run other processes
            End If
        Next
    End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
T HoecherlDeveloperAuthor Commented:
This is what I have for testing:

    Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click
        For Each row As DataGridViewRow In dgvAddWindow.Rows
            If Convert.ToBoolean(row.Cells("Add Window").Value) = True Then
                MsgBox("Checked")
            Else
                MsgBox("Not Checked")
            End If
        Next
        Exit Sub
 If the box is checked, I get the msgbox("Checked"), as expected.  But if the box is unchecked I get the attached error.

T
BooleanError.docx
T HoecherlDeveloperAuthor Commented:
I figured it out.  This is what I used:

        For i As Integer = 0 To dgvAddWindow.Rows.Count - 2
            If dgvAddWindow.Rows(i).Cells(3).Value.ToString() = "" Then
                x = x
            Else
                x = x + 1
            End If
        Next
 
Thanks.

T
Karrtik IyerSoftware ArchitectCommented:
The reason you got that error was when check box is not selected, the value expected while debugging for that column is 0, only then convert to boolean code shall work for both cases, when checked as well as unchecked. It seems in your current case for checked it is coming as 1,but for unchecked cases it is coming as empty string instead of 0.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.