Checkbox problem in Datagrid

Posted on 2006-04-06
Last Modified: 2008-02-01
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)
        myAdapter.Fill(ds, "inspection_temp")

        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)
                        .Add(New DataGridTextBoxColumn)
                    End If
                    With .Item(i)
                        .MappingName = col_name
                        .HeaderText = col_name
                        .ReadOnly = False
                        .NullText = ""
                    End With
            End With
        End With
    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
Question by:LanAnh
    LVL 39

    Expert Comment



    .Add(New DataGridBoolColumn)


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

    change 0 and 1 to the values indicating true and false in your DB.

    Accepted Solution

    Thanks appari
    I have tried your solution but it does not work, once the cell lost focus it goes back to grey.
    However I have tried an alternative way which could solve my problem by constructing a DataTable.
           For i = 0 To rs.Fields.Count - 1
                col_name = rs.Fields(i).Name
                col_type = rs.Fields(i).Type
                Select Case col_type
                    Case 5 : myColumn = New DataColumn(col_name, GetType(Double))
                    Case 16 : myColumn = New DataColumn(col_name, GetType(Boolean))
                    Case 133 : myColumn = New DataColumn(col_name, GetType(Date))
                    Case 200 : myColumn = New DataColumn(col_name, GetType(String))
                End Select
            myAdapter.SelectCommand = New MySqlClient.MySqlCommand(sql, myCon)
            myBuilder = New MySqlClient.MySqlCommandBuilder(myAdapter)
            myAdapter.fill(myTable) 'fill data to myTable
            ds.Tables.Add(myTable) 'add myTable to the dataset
            dg.SetDataBinding(ds, "inspection_temp")
    So I have solve the problem in this way.

    Author Comment

    I forgot how to close this question as I have solved it myself. Any experts knows how?Thanks
    LVL 39

    Expert Comment

    post a question asking for points refund in community support giving link to this question.

    Author Comment


    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    755 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

    17 Experts available now in Live!

    Get 1:1 Help Now