Radiobutton Databinding Driving Me Nuts

I'm two days into trying to solve issues with databinding radiobuttons and I'm really hoping someone (anyone) can help.  As a newcomer to VB, this is proving to be the most challenging issue I've come across. Hopefully it's an easy fix for the more experienced.  Here's my problem:

I have two radiobuttons on a form.  One radiobutton is labeled "Yes" and the other "No".  I databound the checked property of the "Yes" radiobutton to a column (char data type) of a table in my SQL Express database.  When I click on the "Yes" radiobutton, the database is appropriately updated and the radiobutton is checked.  However when I click the "No" radiobutton, close and open the form again, none of the radiobuttons are checked.  Try as I might, I can't seem to solve what should be an easy fix.

How can I get the "No" button to default to being checked whenever the form is loaded or when sifting through the records using the binding navigator buttons

Any help on this would be GREATLY appreciated!

Tony


Answer this question

Radiobutton Databinding Driving Me Nuts

  • Rahul_hk1

    Thanks very much!  I'll try this also.

    Tony

  • Alaska

    We had a similar problem with a checkbox, so we created our own checkbox control.  Here's the code:


        public class CheckBox : System.Windows.Forms.CheckBox
        {
            private String _trueValue;
            private String _falseValue;

            /// <summary>
            /// Default base constructor
            /// </summary>
            public CheckBox()
            {
            }

            /// <summary>
            /// The string that signals the checkbox to be checked (e.g. 'YES' may indicate checked)
            /// </summary>
            public String TrueValue
            {
                get { return _trueValue; }
                set { _trueValue = value; }
            }
           
            /// <summary>
            /// The string that signals the checkbox to be unchecked (e.g. 'YES' may indicate checked)
            /// </summary>
            public String FalseValue
            {
                get { return _falseValue; }
                set { _falseValue = value; }
            }

            /// <summary>
            /// Gets or sets the database value for the checkbox based on TrueValue and FalseValue
            /// </summary>
            public String DBValue
            {
                get
                {
                    if (this.Checked)
                    {
                        return _falseValue;
                    }
                    else
                    {
                        return _trueValue;
                    }
                }
                set
                {
                    if (value == _falseValue)
                    {
                        this.Checked = true;
                    }
                    else
                    {
                        this.Checked = false;
                    }
                }
            }
        }


     



    This allowed for binding DBValue to any column in the database...

    Let me know if you need more help with this,
    Josh Lindenmuth



  • Airex

    Hi. I am new to .net environment. Just wondering about your solution. How do you translate that to C#

    For my application. I have this table column that can contain the values Y or N or '' (blank). I really sure how I'm suppose to represent it on the form.

    Thanks,

    Rick..

  • elias_adum

    vb code

    Public Class CheckBox

    Inherits System.Windows.Forms.CheckBox

    Private _trueValue As String

    Private _falseValue As String

    ''' <summary>

    ''' Default base constructor

    ''' </summary>

    Public Sub New()

    End Sub 'New

    ''' <summary>

    ''' The string that signals the checkbox to be checked (e.g. 'YES' may indicate checked)

    ''' </summary>

    Public Property TrueValue() As String

    Get

    Return _trueValue

    End Get

    Set(ByVal Value As String)

    _trueValue = Value

    End Set

    End Property

    ''' <summary>

    ''' The string that signals the checkbox to be unchecked (e.g. 'YES' may indicate checked)

    ''' </summary>

    Public Property FalseValue() As String

    Get

    Return _falseValue

    End Get

    Set(ByVal Value As String)

    _falseValue = Value

    End Set

    End Property

    ''' <summary>

    ''' Gets or sets the database value for the checkbox based on TrueValue and FalseValue

    ''' </summary>

    Public Property DBValue() As String

    Get

    If Me.Checked Then

    Return _falseValue

    Else

    Return _trueValue

    End If

    End Get

    Set(ByVal Value As String)

    If Value = _falseValue Then

    Me.Checked = True

    Else

    Me.Checked = False

    End If

    End Set

    End Property

    End Class 'CheckBox

    Remco


  • Praj

    Josh,

    Thanks very much for the reply!  I'm going to translate it to VB and give it a try.  Do you happen to have the above code in VB

    Thanks again,
    Tony


  • Anubhava

    I had the same problem and solved it with a bit of code.

    My code handled a customers table. Substitute your Binding Source and field name for mine.  Place this code right after  InitializeComponent()

    Dim YesNo As Boolean = CustomersBindingSource.Item(0).Item("YesFieldName")

    If
    YesNo = True Then
       radYes.Checked = False
       radNo.Checked = True
    Else
       radYes.Checked = True
       radNo.Checked = False
    End If

    Hope that helps



  • Radiobutton Databinding Driving Me Nuts