We help IT Professionals succeed at work.
Get Started

bind boolean values to checkbox in DataGridView

Last Modified: 2012-05-09
Looking for some help in displaying boolean values from a dataset in a datagridview as chgeckboxes.

I have the dataset coming back successfully from a call to a MySQL database and the values are being displayed correctly as textboxes, however I have one column that is returning booleans, 1 and 0's and I want these to be displayed as checkboxes.

Here is the code I have so far, vb.Net with MySQL

        Using mDS As New RunQueries ' A class that sets up the DB call
            mDS.SetupTextCommand(strSQL) ' Open the Connection and pass it a select statement
            DS = mDS.ReturnDataSet("mDS") - return a dataset called DS
        End Using

        dgv2.DataSource = DS.Tables("mDS")

  ' On my form, dvg2 is a datagridview, Visual Studio 2005

        With dgv2
            .RowHeadersVisible = False

            .AllowUserToResizeColumns = True
            .AllowUserToDeleteRows = False
            .AllowUserToAddRows = False
            .SelectionMode = DataGridViewSelectionMode.RowHeaderSelect
            .AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders ' DataGridViewAutoSizeRowsMode.None;
            .AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
            .RowsDefaultCellStyle.BackColor = Color.LightGray
            .AlternatingRowsDefaultCellStyle.BackColor = Color.White

            .Columns(0).DisplayIndex = 0 ' Service
            .Columns(0).HeaderText = "Timetable"

            .Columns(4).DisplayIndex = 1 ' Depart Station
            .Columns(4).HeaderText = "Departs"

        Now get to the bit that doesn't work, I have return YTD - "yet to Depart" and it is a boolen value.  It is Column(11) in the Dataset

   Private Sub AddYTDCheckColumn()
        Dim column As New DataGridViewCheckBoxColumn()
        With column
            .HeaderText = "YTD"            
            .DisplayIndex = 14
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .CellTemplate = New DataGridViewCheckBoxCell()

        End With

What am I doing wrong?  
Watch Question
This problem has been solved!
Unlock 2 Answers and 2 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE