Link to home
Start Free TrialLog in
Avatar of LanAnh
LanAnh

asked on

Checkbox problem in Datagrid

Hi Experts,
Currently I have a problem of not being able to edit the checkbox on the grid. The datagrid bound to a dataset which is filled by a DataAdapter. Since the table stored in MySql at run-time, I do not know the fields in advace, therefore I have to get data from database to dataset via Adapter. Also in MySQL boolean type is present at tinyint(1). Thus when being filled in dataset, it has "SByte" type instead of "Boolean" type .
Here is my code:

myAdapter.SelectCommand = New MySqlClient.MySqlCommand("select * from inspection_temp", myCon)
        myBuilder = New MySqlClient.MySqlCommandBuilder(myAdapter)
        myCon.Open()
        myAdapter.Fill(ds, "inspection_temp")

        format_grid()
        dg.SetDataBinding(ds, "inspection_temp")

Private Sub format_grid()
        Dim i As Integer
        Dim data_type, col_name As String
        Dim grdStyle As New DataGridTableStyle

        With grdStyle
            .MappingName = "inspection_temp"
            With .GridColumnStyles
                For i = 0 To (ds.Tables("inspection_temp").Columns.Count - 1)
                    data_type = ds.Tables("inspection_temp").Columns(i).DataType.Name
                    col_name = ds.Tables("inspection_temp").Columns(i).ColumnName
                    If data_type = "SByte" Then
                        .Add(New DataGridBoolColumn)
                    Else
                        .Add(New DataGridTextBoxColumn)
                    End If
                    With .Item(i)
                        .MappingName = col_name
                        .HeaderText = col_name
                        .ReadOnly = False
                        .NullText = ""
                    End With
                Next
            End With
        End With
        dg.TableStyles.Clear()
        dg.TableStyles.Add(grdStyle)
    End Sub

The Datagid displays grey checkbox which I can edit when having focus, but when I tab to the next cell it goes back to the grey checkbox. other columns in the datagrid are editable as they are String and Number types.
Anyone could help,please
LA
Avatar of appari
appari
Flag of India image


change

.Add(New DataGridBoolColumn)

to

       Dim boolCol As DataGridBoolColumn = New DataGridBoolColumn()
        boolCol.FalseValue = "0"
        boolCol.TrueValue = "1"
       .Add(boolCol)

change 0 and 1 to the values indicating true and false in your DB.
ASKER CERTIFIED SOLUTION
Avatar of LanAnh
LanAnh

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LanAnh
LanAnh

ASKER

I forgot how to close this question as I have solved it myself. Any experts knows how?Thanks
post a question asking for points refund in community support https://www.experts-exchange.com/Community_Support/ giving link to this question.
Avatar of LanAnh

ASKER

Thanks