Bind data to radiobutton

  • Thread starter Thread starter Hemang Shah
  • Start date Start date
H

Hemang Shah

Hello

I cannot seem to figure out what property do you bind the data to in a
radiobutton ?

I thought we would have to do it on the group box, but group box has only
text & tag properly.

The radio button has an additional CheckAligned property, which is to set
the alignment.

So if I have a boolean value in the dataset "Gender" how do I set it using
1 group and 2 radiobuttons ?

Any help would be appreciated.

Thanks

HS
 
I cannot seem to figure out what property do you bind the data to in a
radiobutton ? [...]

So if I have a boolean value in the dataset "Gender" how do I set it
using
1 group and 2 radiobuttons ?

Hi,

bind any boolean property to the "Checked" property of the
radiobuttons; See example below;

Regards akim

private void Form1_Load(object sender, System.EventArgs e)
{
Test test = new Test(false);
radioButton1.DataBindings.Add("Checked", test, "Male");
radioButton2.DataBindings.Add("Checked", test, "Female");
}




public class Test
{
bool male;
public Test(bool male)
{
this.male = male;
}
public bool Male
{
get{return male;}
}
public bool Female
{
get{return !male;}
}
}
 
Thanks akim

What if I have only one boolean datafield in the database say "gender"

Mostly when you give the options of radio button only one value can be
selected at anytime, and thus only 1 value to be stored in the database.

Thanks

akim said:
I cannot seem to figure out what property do you bind the data to in a
radiobutton ? [...]

So if I have a boolean value in the dataset "Gender" how do I set it
using
1 group and 2 radiobuttons ?

Hi,

bind any boolean property to the "Checked" property of the
radiobuttons; See example below;

Regards akim

private void Form1_Load(object sender, System.EventArgs e)
{
Test test = new Test(false);
radioButton1.DataBindings.Add("Checked", test, "Male");
radioButton2.DataBindings.Add("Checked", test, "Female");
}




public class Test
{
bool male;
public Test(bool male)
{
this.male = male;
}
public bool Male
{
get{return male;}
}
public bool Female
{
get{return !male;}
}
}
 
Hi,

never done that before.. sorry. Just an idea (may be a little hackish):
Derive a control from RadioButton. Add a new property
"Unchecked" which inversely wraps the "Checked" property:

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

and bind

radioButton1.DataBindings.Add("Checked", test, "Male");
inverseRadioButton1.DataBindings.Add("Unchecked", test, "Male");

If you find a more elegant way let me know :)

akim
 
Back
Top