Radio button group linked to database integer value

brotherkennyh

Member
Joined
Jun 18, 2013
Messages
11
Programming Experience
Beginner
Hi,
I have a database field that contains an integer value.
What is the easiest way to link the database field to a radio button group on my form? I want the radio button to change to represent the new integer value when the user changes the current record and I want the new value to be updated back to the database when the user changes the selected radio button.
I am using Visual studio 2012 Pro. The database is a MySQL server. I am a relative beginner.

Hope that makes sense.

Kenny
 
You could create a UserControl to contain the RadioButtons and add a property that you could bind your database value to. Failing that, I'd suggest putting the RadioButtons into an array and then writing two methods to check the appropriate RadioButton based on the value and get the current value based on the RadioButton that's checked, e.g.
Private radioButtons As RadioButton()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    radioButtons = {RadioButton1, RadioButton2, RadioButton3}
End Sub

Private Sub SetRadioButtonValue(value As Integer)
    radioButtons(value).Checked = True
End Sub

Private Function GetRadioButtonValue() As Integer
    Return radioButtons.IndexOf(radioButtons.Single(Function(rb) rb.Checked))
End Function
That assumes that your database value can be 0, 1 or 2. You just add more RadioButtons if there are more values and perform any appropriate translation, e.g. if the values are 1, 2 or 3 instead:
Private radioButtons As RadioButton()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    radioButtons = {RadioButton1, RadioButton2, RadioButton3}
End Sub

Private Sub SetRadioButtonValue(value As Integer)
    radioButtons(value - 1).Checked = True
End Sub

Private Function GetRadioButtonValue() As Integer
    Return radioButtons.IndexOf(radioButtons.Single(Function(rb) rb.Checked)) + 1
End Function
 
Back
Top