dotnet windows forms databinding:
I'd like to bind a combo box control to an object. i.e. bind an
index number for an array of display strings to my data object.
I've got something that works as far as I've tested it, but it
has to be an overly convoluted method.
I'd really appreciate a more streamlined method to implement this.
My Code:
....
myComboBox.DataSource = myData.ColorDisplay;
myComboBox.DisplayMember = "Txt";
myComboBox.ValueMember = "ID";
....
I then was able to bind the control to my object.
....
myComboBox.DataBindings.Add(new
System.Windows.Forms.Binding("SelectedValue",
this.myDataBindingSource,"ColorValue",true));
....
The class:
....
public class myData
{
public enum myColor_enum { black, white, red };
public class myColors
{
// Data
private myColor_enum iD;
private string txt;
// Methods
public myColor_enum ID
{ get { return iD; } set { iD = value; } }
public string Txt
{ get { return txt; } set { txt = value } }
// Constructor
public myColors(myColor_enum colorID, string text)
{ this.Txt(text); this.ID(colorID); }
}
// Data
private myColors[] colorDisplay =
{
new myColors(myColor_enum.black,"Black"),
new myColors(myColor_enum.white,"White"),
new myColors(myColor_enum.red,"Red")
}
private myColor_enum colorValue;
// Methods
public myColor_enum ColorValue
{ get { return colorValue; } set { colorValue = value; } }
public myColors[] ColorDisplay
{ get { return colorDisplay; }
set { colorDisplay = value; } }
}
....