We help IT Professionals succeed at work.

bind boolean values to checkbox in DataGridView

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"
            AddYTDCheckColumn()





        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?  
Comment
Watch Question

Commented:
change your sub as follows


   Private Sub AddYTDCheckColumn()
        Dim column As New DataGridViewCheckBoxColumn()
        With column
            .HeaderText = "YTD"            
            .DisplayIndex = 14
            .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
            .FlatStyle = FlatStyle.Standard
            .CellTemplate = New DataGridViewCheckBoxCell()
            .ValueType = GetType(Boolean)
            .FalseValue = False
            .TrueValue = True
           .DataPropertyName = "YTD"
        End With
 
dgv2.columns.add column


as said by appari you need to set DataPropertyName for binding and add that dgv2.
You need to remove YTD column first.
i.e.
dgv2.Columns.Remove("YTD")
AddYTDCheckColumn()