S
sklett
I have the need to display a DropDown with 2 options: Yes/No.
I want to bind this control to an entity which represents the field in
question with a bool.
What I've done in the interim is to manually add "YES" and "NO" strings to
the control, then in the SelectedIndexChanged event set the bool value by
performing a string comparison for YES or NO. This is less than elegant.
I've been thinking of way to set the DataSource of the DropDown that would
allow me to bind straight to the SelectedValue property. The only thing I
can come up with is to create a small class like:
class YesNoOption
{
private string _display;
private bool _value;
public YesNoOption(string display, bool value)
{
_value = value;
_display = display;
}
// properties
}
Then create 2 of the above and place in a List.
List<YesNoOption> options = new List<YesNoOption>();
options.Add(new YesNoOption("YES", true));
options.Add(new YesNoOption("NO", false));
comboBoxBindingSource.DataSource = options;
There must be a better way than all of this, no? This seems like a common
scenario that people would deal with but I haven't been able to find much on
the topic.
Any suggestions or experiences with this scenario?
Thanks,
Steve
I want to bind this control to an entity which represents the field in
question with a bool.
What I've done in the interim is to manually add "YES" and "NO" strings to
the control, then in the SelectedIndexChanged event set the bool value by
performing a string comparison for YES or NO. This is less than elegant.
I've been thinking of way to set the DataSource of the DropDown that would
allow me to bind straight to the SelectedValue property. The only thing I
can come up with is to create a small class like:
class YesNoOption
{
private string _display;
private bool _value;
public YesNoOption(string display, bool value)
{
_value = value;
_display = display;
}
// properties
}
Then create 2 of the above and place in a List.
List<YesNoOption> options = new List<YesNoOption>();
options.Add(new YesNoOption("YES", true));
options.Add(new YesNoOption("NO", false));
comboBoxBindingSource.DataSource = options;
There must be a better way than all of this, no? This seems like a common
scenario that people would deal with but I haven't been able to find much on
the topic.
Any suggestions or experiences with this scenario?
Thanks,
Steve