[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Checkbox problem in Datagrid

Posted on 2006-04-06
6
Medium Priority
?
573 Views
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)
        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
0
Comment
Question by:LanAnh
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:appari
ID: 16397372

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.
0
 

Accepted Solution

by:
LanAnh earned 0 total points
ID: 16397570
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
            myTable.Columns.Add(myColumn)
        Next
        myAdapter.SelectCommand = New MySqlClient.MySqlCommand(sql, myCon)
        myBuilder = New MySqlClient.MySqlCommandBuilder(myAdapter)
        myCon.Open()
        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.
LA
0
 

Author Comment

by:LanAnh
ID: 16397583
I forgot how to close this question as I have solved it myself. Any experts knows how?Thanks
0
 
LVL 39

Expert Comment

by:appari
ID: 16397683
post a question asking for points refund in community support http://www.experts-exchange.com/Community_Support/ giving link to this question.
0
 

Author Comment

by:LanAnh
ID: 16397858
Thanks
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

872 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