Question binding datagridview and (textbox, radiobutton) to the same table

Mimosa777

Active member
Joined
Aug 28, 2014
Messages
28
Programming Experience
1-3
Hi there,

im currently binding a datagridview and some textboxes + 2 radiobutton to a table in my db. Everytime I click on a row in my datagrid, the values are perfectly showing also in the appropriate textboxes using databinding properties.

My only issue is with the radiobutton option. I have a field in my table called "Transmission" - on my form, I have 2 radiobutton, the first is called "Automatic" and the second "Manual"

I would like to see the "Automatic" radiobutton checked if the value in my table is Automatic or else. I look for the checked value in the IDE propreties, tag and also text and could not find a way to retrieve the value of my table into the appropriate radiobutton. Basically, if the field in my table has the "Automatic" value, then my radiobutton "Automatic" should be checked.

Any idea how to fix this using databinding ?

Thank you :topsy_turvy:
 
You can't do it the way you want. If you wanted to bind a RadioButton then you'd need a Boolean to bind its Checked property to. Even then though, it still won't work. The fact that multiple RadioButtons with the same parent are linked interferes with the data-binding process. In short, you can bind the RadioButtons and will have to write code to manage them and the associated data source column/property yourself.
 
I understand - after few reading and research, I found an easier way (for me at least) to do it using dataset - Ill appreciate your expert view on that just to know if this is good practice or not. thank you mate.
VB.NET:
[Private Sub btnAddSaveCar_Click(sender As Object, e As EventArgs) Handles btnAddSaveCar.Click
Dim cb As New OleDb.OleDbCommandBuilder(daCars)
        Dim dsNewRow As DataRow
        'using dataset newrow
        dsNewRow = ds.Tables("Cars").NewRow()
            dsNewRow.Item("Brand") = UCase(tbBrand.Text)
            dsNewRow.Item("Model") = UCase(tbModel.Text)
            dsNewRow.Item("Color") = UCase(tbColor.Text)
            If rbAutomatic.Checked = True Then
                dsNewRow.Item("Transmission") = UCase(rbAutomatic.Text)
            Else
                dsNewRow.Item("Transmission") = UCase(rbManual.Text)
            End If

        End If
        ds.Tables("Cars").Rows.Add(dsNewRow)
        daCars.Update(ds, "Cars")
End sub

and then, ill read the same way to view the data in my form like this :
VB.NET:
tbBrand.Text = dgCars.CurrentRow.Cells("Brand").Value.ToString
tbModel.Text = dgCars.CurrentRow.Cells("Model").Value.ToString
tbColor.Text = dgCars.CurrentRow.Cells("Color").Value.ToString
If dgCars.CurrentRow.Cells("Transmission").Value.ToString = "Manual" Then
    rbManual.Checked = True
Else
    rbAutomatic.Checked = True
End If
 
Last edited:

Latest posts

Back
Top