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.MySqlCommandBu ilder(myAd apter)
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_tem p").Column s.Count - 1)
data_type = ds.Tables("inspection_temp ").Columns (i).DataTy pe.Name
col_name = ds.Tables("inspection_temp ").Columns (i).Column Name
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(grdStyl e)
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
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("
myBuilder = New MySqlClient.MySqlCommandBu
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_tem
data_type = ds.Tables("inspection_temp
col_name = ds.Tables("inspection_temp
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(grdStyl
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
ASKER
Thanks
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.