Help-Adding check boxes to data grid control and accessing them

Hi
I need to add to check box controls to the data grid view, when I am retreiving data from database, i.e I need to loop the check boxes such that how many ever rows I get from the database, these rows should have check boxes beside them.
I tried writing some code which is below, I was able to get check boxes, but I am not able to access them, i.e I am not able to check them, I also want something like SelectAll and UnSelectAll buttons, when I click on the SelectAll button, it should check all the check boxes and when I click UnselectAll button, it should be able to uncheck all check boxes.

And also one more thing, I don't why it is adding an extra empty row in the data gird view, how do I  avoid that.
If GetDataSet(DS, sSQL) Then
            With DS.Tables("Table")
                count = .Rows.Count
                If .Rows.Count > 0 Then
                    'Populated the DataGridView with the data from the dataset
                    RDGVW.DataSource = DS.Tables("Table")
                    With Chkbx
                        .Name = "check"
                        .HeaderText = "Status"
                     End With
                    RDGVW.Columns.Add(Chkbx)
                End If
            End With
        End If

Open in new window

code.doc
LVL 1
SGUDAPATAsked:
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.

ptakjaCommented:
I am not sure about the MS Gridview Control, since I use Infragistics almost exclusively for grid rendering, but what if you add a column to your DataTable object BEFORE you bind it to the grid view? Set the data type of the column to "Bit" or "Boolean". Some grids will automatically render a bit/boolean data type as a checkbox.

To check all/clear all, you simply would then loop through the grid and for each row set the value of that cell to 0.

Also, looking at your code, there is not enough there to determine why you get an extra row.
0
SGUDAPATAuthor Commented:
Hi
Thank you for the response, how do I add the data table to data grid view?

I did something like
dt.Columns.Add("NO")
dt.Columns.Add("LINE")
and added the columns to data table, but how do I add the check boxes to the data table and and them to the data grid........ I am begginner in Vb.Net, please explain it to me.
0
ptakjaCommented:
OK. I tested this and it works just fine. The MS GridView control does render columns that have a data type of Boolean as checkboxes.

Your idea was close, but the default data type for a new unbound column in the GridView control is String. To change it, you need to use one of the overloaded Add methods like this:


Here's some code I created to test this out. My form has a standard GridView control on it. It is bound to a DataSet that has 1 table in it with three columns:

Column 0 --> "Key", System.Int32
Column 1 --> "Name", System.String
Column 2 --> "Active", System.Boolean

The code shown fills the data set with some bogus data. The screenshot shows the results...that any column with a Data Type of BOOLEAN will automatically render as a checkbox.

Note that the GridView control will inherit the data type from the data source. Be that a DataSet, DataTable, or if you manually add an unbound column to the grid.

Hope that helps!
dt.Columns.Add("Line", GetType(System.Boolean))
 
 
Here's the Form1.Shown event handler to load up the dataset:
 
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        With DataSet1
            For ii = 0 To 1000
                Dim newrow As DataRow = DataSet1.Tables(0).NewRow
                newrow.Item("Name") = "Tom" & ii.ToString
                newrow.Item("Active") = IIf(ii Mod 2 = 0, True, False)
                .Tables(0).Rows.Add(newrow)
            Next
        End With
 
    End Sub

Open in new window

MSGridView.jpg
0

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
ptakjaCommented:
Solution has been posted. User can revisit to get answer at another time.
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.