Databinding to RadioButton

Posts   
 
    
Posts: 34
Joined: 03-Oct-2005
# Posted on: 03-Oct-2005 13:12:22   

Hi

Possibly I am just being dense, but I am struggling to get this to work.

I have a customer entity, with a boolean property (SQL Server bit field underneath).

I am trying to bind this property to a pair of radio buttons in a panel, using the following code

    optVoipSL.DataBindings.Add("Checked", m_customer, "VoipSLCust")

The other radio button in the panel is not bound

This seems to work "one way" only - checking the optVoipSL radio button sets the property to true, but checking the other radio button unchecks the optVoipSL one, but makes no change to the property of the entity...

Is there a way round this...?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Oct-2005 16:50:50   

Well, if you want to use 2 radio buttons for a boolean field rather than using one checkbox.

Then you have 2 options:

OPTION#1:

Define a new property of the customer entity, let's name it "NOTVoipSLCust", as follows:

public bool NOTVoipSLCust
{ 
      get{return !VoipSLCust;} 
      set{VoipSLCust = !value;}
}

Then bind as follows.

radioButton1.DataBindings.Add("Checked", m_customer, "VoipSLCust"); 
radioButton2.DataBindings.Add("Checked", m_customer, "NOTVoipSLCust"); 

OPTION#2: Derive a control from the RadioButton, define a new property, let's name it "Unchecked" which inversely wraps the "Checked" property as follows

public bool Unchecked 
{ 
   get{return !Checked;} 
   set{Checked = !value;}   
}

Then bind as follows:

radioButton1.DataBindings.Add("Checked", m_customer, "VoipSLCust"); 
radioButton2.DataBindings.Add("Unchecked", m_customer, "VoipSLCust");