Unbound DataGridView with Checkboxes

mxracer

Member
Joined
Jan 10, 2007
Messages
7
Programming Experience
Beginner
I have an interesting problem and have been unable to find any code examples for it. BTW i am using visual Studio 2005.
I have an unbound DataGridView that I populate with information (WMI Type) from a number of functions and Subs. I would like to be able to check the checkbox next to the item and when I click the button it will give me the text in the column to the left as shown.

I would think I need to either loop through the rows but am not sure at all... I am completely stumped on this one.

I have tried a lot of code in examples I have found but to no avail. my vurrent code looks like this .. (Just giving me teh word "test")

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

If Me.DataGridView2.SelectedRows.Count > 0 AndAlso _
Not Me.DataGridView2.SelectedRows(0).Index = _
Me.DataGridView2.Rows.Count - 1 Then
Me.TextBox1.Text = "test"
End If

End Sub

Any Help would be appriceated.:)
vb.jpg
 
Unbound datagridview solved

Ok So I solved it myself and thought I would post the solution in case someone else needed it. In this case the test I want to work with is in column 1 and the checkbox is in column 2. If the checkbox is checked I write the results to a listbox. Anything could be done with the output. I have not seen a solution like this so here it is. The sub is a button...

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim share As String = ""
        If DataGridView2.Rows.Count > 0 Then
            For Each dgRow As DataGridViewRow In DataGridView2.Rows

                Dim cell As DataGridViewCell = dgRow.Cells(0)
                Dim check As DataGridViewCell = dgRow.Cells(1)
                Dim check1 As Boolean = check.Value
                If check1 = True Then
                    share = CStr(cell.Value)
                    Me.ListBox1.Items.Add(share)

                End If

            Next
        End If

    End Sub

Couple other datagridview things that might help someone.

To refresh a datagridview:

VB.NET:
DataGridview1.refresh()

To delete a row

VB.NET:
DataGridView1.Rows.RemoveAT(rowIndex)

I have a couple more if needed.
 
Back
Top