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

Posted on 2008-11-08
Last Modified: 2012-05-05
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


                End If

            End With

        End If

Open in new window

Question by:SGUDAPAT
    LVL 14

    Expert Comment

    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.
    LVL 1

    Author Comment

    Thank you for the response, how do I add the data table to data grid view?

    I did something like
    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.
    LVL 14

    Accepted Solution

    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)
            End With
        End Sub

    Open in new window

    LVL 14

    Expert Comment

    Solution has been posted. User can revisit to get answer at another time.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
    This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now