Cant bind radio button to a text column

tarunkapoor

Member
Joined
Apr 10, 2005
Messages
17
Programming Experience
3-5
Cant bind radio button to a text column [RESOLVED]

Hi,
I have two radio buttons in a group box in my form which i want to bind to a text column in my database. The column can have two text values "Share" or "Bond". I want the first radiobutton to be checked if the clumn has a value "Share" and the second one to be checked when the value is "Bond". Also, I want the database to be updated if the radio buttons are changed.I tried binding the checked property of the radio button to the text column but it doesnt work. Any help will be appreciated.

Thanks !!
 
Last edited:
There is a property named "Tag" , first you need to connect to database at design time and then you will be displayed a list of all tables:fields:columns appropriately.
Then you can choose at which column you want to bind the field, but i think the data type of both the table field and the property of the object you want to bind should be same.....
In this Tag property set the name of column and also if you want to directly change the values to database when clicking on radio buttons then you can write code behind the checked changed event of radio buttons...
If it works let me know or otherwise reply back.....
Good Luck..........
 
Still doesnt work

Thanks for your immediate reply. Appreciate that.
I tried setting the tag property of radio button to the column i want to use. But what then. The radio button's state (checked/unchecked) doesnt change as the value in the column changes.

The checked state property is true/false (boolean) whereas the column is text type.

-----------------------------------------------------------------------


There is a property named "Tag" , first you need to connect to database at design time and then you will be displayed a list of all tables:fields:columns appropriately.
Then you can choose at which column you want to bind the field, but i think the data type of both the table field and the property of the object you want to bind should be same.....
In this Tag property set the name of column and also if you want to directly change the values to database when clicking on radio buttons then you can write code behind the checked changed event of radio buttons...
If it works let me know or otherwise reply back.....
Good Luck..........
 
you are welcomed...Ok tell me what is the data type of column where you change the value...i mean is it boolean or something else... I think it must be boolean in order for checkbox to change its checked/unchecked....
 
Thats the whole problem. Data type is string. But there is still a way to do. I have seen a lot of posts on other forums where people have been able to do that and got stuck somewhere further. But i am stuck on the first step itself.
 
Here is a subClassed GroupBox control that accomplishes your goal.
To use it add radioButtons to the control and set the Tag property of each radioButton to the String value you want it to represent ("Share" & "Bond" in your case). Bind to the new myTag property of the groupbox.
VB.NET:
Public Class myGroupBox
    Inherits GroupBox

    Private _myTag As String
    <System.ComponentModel.Bindable(True)> _
    Public Property myTag() As String
        Get
            Return _myTag
        End Get
        Set(ByVal Value As String)
            _myTag = Value
            SelectRadioButton()
        End Set
    End Property

    'selects the correct RadioButton based on Tag values
    Private Sub SelectRadioButton()
        Dim ctrl As Control
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is RadioButton Then
                Dim rb As RadioButton = CType(ctrl, RadioButton)
                If Not rb.Tag Is Nothing Then
                    rb.Checked = rb.Tag.Equals(_myTag)
                End If
            End If
        Next
    End Sub

    Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim rb As RadioButton = CType(sender, RadioButton)
        If rb.Checked Then
            Me._myTag = rb.Tag.ToString
        End If
    End Sub

    Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
        If TypeOf e.Control Is RadioButton Then
            Dim rb As RadioButton = CType(e.Control, RadioButton)
            AddHandler rb.CheckedChanged, AddressOf rb_CheckedChanged
        End If
    End Sub
End Class
 
This is sick !!

Paszt,
It took me some time to understand your idea. Your comments on top of the code were kinda brief. But it finally makes sense. And its an awesome idea.

As a workaround, this is what i was doing.
I bind one of the radio buttons(in the box) to the dataset. Then in the Format event of the binding, I manually check/uncheck the radio buttons depending onthe value of the binding. In the Parse event, I check the status of all the radio buttons in the box and then set the output value of the binding. But it was lot of hardcoding.

Good job and thanks !!!
 
Paszt,

I am not able to bind to the custom Property. This is what i am doing.

TriggerCurrencyBinding = New Binding("_myTag", form3Instance.CnvtMasterBindingSource, "TriggerCurrency")

It gives me an error "Cant bind to this property"
 
Shouldn't it be myTag.... not _myTag? _myTag is the private variable... myTag is the public property.

Tg
 
I am sorrry. I was doing myTag only. Still doesnt work.

Error: Cannot bind to the property 'customTag' on the target control.
Parameter name: PropertyName
 
I am sorrry. I was doing myTag only. Still doesnt work.

Error: Cannot bind to the property 'myTag' on the target control.
Parameter name: PropertyName

I usually get this error when the property name is not found on the control.
 
[resolved]

Sorry guys. I had forgotten to remove the old binding i had. So the control had two bindings for the same property. I did F11 , five times through the errored line and didnt notice. Thanks for the help anyways.

Now hold on. Let me go shoot myself.
 
Back
Top