Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2008-11-08
Medium Priority
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
  • 3
LVL 14

Expert Comment

ID: 22912695
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.

Author Comment

ID: 22924581
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

ptakja earned 1000 total points
ID: 22926027
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

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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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.
Suggested Courses

572 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