For this project, my client asked me if he could select seven names from the list (which is a DataGridView) and use them in a single, separate form. The solution that came to my mind is to use the CheckBoxColumn for this matter. My problem is I have never used this kind of column (I have tried the ComboBoxColumn before but failed because the solutions I find are either too complicated for my knowledge or does not work), so I want to learn how to use this CheckBox type of column in DataGridView. Can anybody teach me about this matter? Any help will be greatly appreciated. Thanks in advance :)

The checkboxcolumn is rather straightforward. I'm not sure that is your problem. Is your datagridview bound? If so you need to bind the CheckBoxColumn to a boolean variable. Otherwise if you can see in the cell's value property if the checkbox was checked or not.

Let us know if this helps and if we can be of further assistance.

Good luck.

My datagridview is bound and I have already included a boolean variable for the checkbox. What should happen is that the user is only allowed to select seven checkboxes, and the data from those checkboxes will be forwarded to another datagridview (in another form). I thought I can use this code I used for another datagridview's celldoubleclick event, but I don't know what event to use for the checked checkbox event:

Private Sub DataGridView1_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick

        Dim crlno As Object = DataGridView1.Rows(e.RowIndex).Cells(1).Value
        Dim devname As Object = DataGridView1.Rows(e.RowIndex).Cells(2).Value
        Dim devcode As Object = DataGridView1.Rows(e.RowIndex).Cells(3).Value
        Dim orno As Object = DataGridView1.Rows(e.RowIndex).Cells(4).Value
        Dim voucherdt As Object = DataGridView1.Rows(e.RowIndex).Cells(5).Value
        Dim dtrecvd As Object = DataGridView1.Rows(e.RowIndex).Cells(6).Value

        frmComm.txtCRLNo.Text = Convert.ToString(crlno)
        frmComm.cmbDevName.Text = Convert.ToString(devname)
        frmComm.txtDevCode.Text = Convert.ToString(devcode)
        frmComm.txtORNo.Text = Convert.ToString(orno)
        frmComm.txtVoucherDate.Text = Convert.ToString(voucherdt)
        frmComm.txtDateReceived.Text = Convert.ToString(dtrecvd)

        Me.Close()

End Sub

To be honest, I'm a little puzzled by your me.close statement but other than that I understand that What you're describing here is what happens when the user doubleclicks on the dgv. The values on the row where the doubleclick happened get passed to the other form.

You might want to set the checkbox of the row to true if that happens. Assuming the checkobx is in the 8th column (not sure if you are zero based or not).

DataGridView1.Rows(e.RowIndex).Cells(7).Value = true

To manage how many rows were passed to other the form, you will need to count the number of checkboxes that are set to true. If there are more than 7, you will exit your sub.

In addition, you might want to implement toggling the "checkbox", so you should check if the checkbox was checked and set it off if it was (you need to code deleting the corresponding row in other form then).

Let us know if this helps and if we can be of further assistance.

Good luck.

Edited 2 Years Ago by PerplexedB

I put Me.Close so that after double-clicking on a cell, the form closes immediately.

At least I understand a bit about checkbox column now...

I have another question: how does the checkbox column here affect the boolean variable in my database? I mean, if I check the columns, does the variable change value from 0 to 1, and vice versa? (If I'm using SQL, the boolean is the same as the bit type, right?)

Edited 2 Years Ago by airhalynn101: clarification

I put Me.Close so that after double-clicking on a cell, the form closes immediately.

But you said the user can have 7 liens no?

how does the checkbox column here affect the boolean variable

as long as you do not set

DataGridView1.Rows(e.RowIndex).Cells(7).Value = true

your database will not be affected.

I'm sorry, I think I'm causing confusion here. The sample code I posted before was an example from a different datagridview, because I thought I could get row data from my datagridview (the one with the checkbox) if I used the same code.

I think I'm getting the idea here. Thank you very much for your help! I'll just update you if I still have questions.

Edited 2 Years Ago by airhalynn101: clarification

I tried putting it like this but it doesn't work (the values don't get passed on to the other form):

    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        'Dim count As Integer
        For Each row As DataGridViewRow In DataGridView1.Rows
            If DataGridView1.Rows(e.RowIndex).Cells(0).Value = True Then
                ExecQuery("update tblUnreleasedComm set uc_flag='1' where uc_payeename='" & Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value) & "'")

                acctn1 = DataGridView1.Rows(e.RowIndex).Cells(3).Value
                cltname1 = DataGridView1.Rows(e.RowIndex).Cells(4).Value
                unitcd1 = DataGridView1.Rows(e.RowIndex).Cells(5).Value
                commtype1 = DataGridView1.Rows(e.RowIndex).Cells(6).Value
                amount1 = DataGridView1.Rows(e.RowIndex).Cells(25).Value
            End If

        Next

    End Sub

I did not include the 'count' part yet (where it would count the number of checkboxes checked and see if it is already 7 or not) because I just wanted to check if it works or not (and...it doesn't. Sigh.) I guess I still don't completely understand the whole thing. I also tried solutions I found from Google but I can't get it to work also. :(

Here are the 'solutions' I tried:
This one...
And this one...
And I've yet to try this one.

If the checkbox column is the first column in my dgv, then I should address it as Cell(0), am I right?

I think I got it working using this solution:
This one.

And here is my code from that:

    Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        'Dim count As Integer
        Dim selected As Boolean = DataGridView1.Rows(e.RowIndex).Cells(0).Value

        If selected = True Then
            ExecQuery("update tblUnreleasedComm set uc_flag='1' where uc_payeename='" & Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value) & "'")

            acctn1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
            cltname1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
            unitcd1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
            commtype1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(6).Value)
            amount1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(25).Value)
        End If

    End Sub

I think I can go from here. Thank you very much for all your help. :)

One LAST question: Wher am I supposed to put my FOR loop / counter? I tried

Private Sub DataGridView1_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
        Dim counter As Integer
        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim selected As Boolean = DataGridView1.Rows(e.RowIndex).Cells(0).Value
            If selected = True Then
                counter += 1
                MsgBox(counter.ToString + " out of 7 checked")
                If counter = 1 Then

                    acctn1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount1 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                ElseIf counter = 2 Then
                    'MsgBox(counter.ToString + " out of 7 checked")
                    acctn2 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname2 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd2 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype2 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount2 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                ElseIf counter = 3 Then
                    'MsgBox(counter.ToString + " out of 7 checked")
                    acctn3 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname3 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd3 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype3 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount3 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                ElseIf counter = 4 Then
                    'MsgBox(counter.ToString + " out of 7 checked")
                    acctn4 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname4 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd4 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype4 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount4 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                ElseIf counter = 5 Then
                    'MsgBox(counter.ToString + " out of 7 checked")
                    acctn5 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname5 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd5 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype5 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount5 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                ElseIf counter = 6 Then
                    'MsgBox(counter.ToString + " out of 7 checked")
                    acctn6 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname6 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd6 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype6 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount6 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                ElseIf counter = 7 Then
                    'MsgBox(counter.ToString + " out of 7 checked")
                    acctn7 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(2).Value)
                    cltname7 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(3).Value)
                    unitcd7 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(4).Value)
                    commtype7 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(5).Value)
                    amount7 = Convert.ToString(DataGridView1.Rows(e.RowIndex).Cells(26).Value)

                Else
                    MsgBox("You can only select up to 7 names.")
                End If
            End If
        Next

    End Sub

But it looks (and counts) wrong. I guess I had put the FOR loop in the wrong place here. Also, the form shows the data from the seventh checked row on all seven placeholders. I got confused again. :/

I'm not quite sure if understand what you're trying to do, but to count the number of checked checkboxes is simple:

Dim counter As Integer = 0
For Each row As DataGridViewRow In DataGridView1.Rows
    if DataGridView1.Rows(e.RowIndex).Cells(0).Value = true then
        counter += 1
    endif
next
if counter>= 7
    msbox "Cannot select more than 7 items"
    exit sub
endif

The remaining you already solved right? To be clear, you count the number of checks before you start passing to the other form.

Tell me us if this helps and if there is anything else we can help you with.

Good luck.

Edited 2 Years Ago by PerplexedB

Yes I already got it to work. I can close this thread now. Really, thank you very much. :)

This question has already been answered. Start a new discussion instead.