locked
Radio buttons not binding correctly RRS feed

  • Question

  • Hello there I've encountered a strange problem in my application that involves the binding of radio buttons.  I thought maybe the problem was unique to my application so I created a dumbed down version of the problem and I get the same result.

    I create a custom data set with 3 columns called (one, two three).

    I create an instance of this data set on my form.

    I create 3 radio buttons and bind the checked property to their respective columns.

    When I run the program the radioButton1 is checked.  I then click radioButton 2 setting RB2 to true and RB1 to false.

     

    However if I look at the data set I see that Column 1 is true and so is Column 2 when I would expect column 1 to be false if binding was working correctly.

    Am I incorrect in assuming this?

    • Moved by Neddy Ren Tuesday, February 22, 2011 1:30 AM (From:Visual C# General)
    Monday, February 21, 2011 7:54 PM

Answers

  • Hi CommanderKeen,

    Any code do you use to set the bindings? Below is my code to test the bindings on the RadioButtons:

        DataSource data = new DataSource();
        public Form1()
        {
          InitializeComponent();
          radioButton1.DataBindings.Add(new Binding("Checked", data, "Value1", false, DataSourceUpdateMode.OnPropertyChanged));
          radioButton2.DataBindings.Add(new Binding("Checked", data, "Value2", false, DataSourceUpdateMode.OnPropertyChanged));
          radioButton3.DataBindings.Add(new Binding("Checked", data, "Value3", false, DataSourceUpdateMode.OnPropertyChanged));
        }
    
    ....
    
     public class DataSource
      {
        public bool Value1 { get; set; }
        public bool Value2 { get; set; }
        public bool Value3 { get; set; }
      }
    
    Sincerely,
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jie Bao Wednesday, March 2, 2011 10:43 AM
    Wednesday, February 23, 2011 9:24 AM

All replies

  • Is this a Windows Forms, Web Forms, WPF or silverlight question?

    You probably want to post to the class library forums to get more interest.



    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, February 21, 2011 8:33 PM
  • This is a Windows Forms question.  I will take your advice on posting in a more correct forum thank you.
    Monday, February 21, 2011 8:53 PM
  • The Windows Forms forums are at http://social.msdn.microsoft.com/Forums/is/category/windowsforms

    The following is signature, not part of post
    Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
    Visual C++ MVP
    Monday, February 21, 2011 9:10 PM
  • Hello there I've encountered a strange problem in my application that involves the binding of radio buttons.  I thought maybe the problem was unique to my application so I created a dumbed down version of the problem and I get the same result.

    I create a custom data set with 3 columns called (one, two three).

    I create an instance of this data set on my form.

    I create 3 radio buttons and bind the checked property to their respective columns.

    When I run the program the radioButton1 is checked.  I then click radioButton 2 setting RB2 to true and RB1 to false.

     

    However if I look at the data set I see that Column 1 is true and so is Column 2 when I would expect column 1 to be false if binding was working correctly.

    Am I incorrect in assuming this?

    • Merged by Jie Bao Wednesday, February 23, 2011 9:27 AM duplicate
    Monday, February 21, 2011 11:14 PM
  • Hi CommanderKeen,

    Any code do you use to set the bindings? Below is my code to test the bindings on the RadioButtons:

        DataSource data = new DataSource();
        public Form1()
        {
          InitializeComponent();
          radioButton1.DataBindings.Add(new Binding("Checked", data, "Value1", false, DataSourceUpdateMode.OnPropertyChanged));
          radioButton2.DataBindings.Add(new Binding("Checked", data, "Value2", false, DataSourceUpdateMode.OnPropertyChanged));
          radioButton3.DataBindings.Add(new Binding("Checked", data, "Value3", false, DataSourceUpdateMode.OnPropertyChanged));
        }
    
    ....
    
     public class DataSource
      {
        public bool Value1 { get; set; }
        public bool Value2 { get; set; }
        public bool Value3 { get; set; }
      }
    
    Sincerely,
    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Jie Bao Wednesday, March 2, 2011 10:43 AM
    Wednesday, February 23, 2011 9:24 AM
  • The code I use is like this:

     

     private PASport.Proxy.StrongTypesNS.pdcReceiptDataSet pdcReceipt = new PASport.Proxy.StrongTypesNS.pdcReceiptDataSet );
      public Form1()
      {
       InitializeComponent();
       this.rbtCheque.DataBindings.Add(new System.Windows.Forms.Binding("Checked", this.pdcReceipt, "ttcReceipt.Cheque", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged, "false"));
    this.rbtNoPayment.DataBindings.Add(new System.Windows.Forms.Binding("Checked", this.pdcReceipt, "ttcReceipt.EFT", true));
    }

     

    Where pdcReceipt is a custom DataSet

     

    I will try your code to see if there is a difference. Thank you.

    Thursday, March 3, 2011 10:46 PM
  • CommanderKeenBob_Bao PerkinElmer (MVP)  you are both right.

    I fix your code (apply "false" as last parameter:

        DataSource data = new DataSource();
        public Form1()
        {
          InitializeComponent();
          radioButton1.DataBindings.Add(new Binding("Checked", data, "Value1", false, DataSourceUpdateMode.OnPropertyChanged, false));
          radioButton2.DataBindings.Add(new Binding("Checked", data, "Value2", false, DataSourceUpdateMode.OnPropertyChanged, false));
          radioButton3.DataBindings.Add(new Binding("Checked", data, "Value3", false, DataSourceUpdateMode.OnPropertyChanged, false));
        }



    Saturday, November 30, 2013 11:15 AM
  • Without setting it in another way is the radio button a toggle button. 

    Only one in a container can be on. All others are always off. 

    You describe in fact a checkbox, where every checkbox can have its value. 

    If you want to use radiobuttons in a checkbox way then set the property autocheck property to false.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.radiobutton.autocheck(v=vs.110).aspx

    (Be aware that using a button as checkbox can confuse end users).


    Success
    Cor


    Sunday, December 1, 2013 8:50 AM