public class MyPairCollection : CollectionBase { public MyPair this[ int index ] { get { return (MyPair)List[index]; } set { List[index] = value; } } public int Add( MyPair value ) { return( List.Add( value ) ); } } public class MyPair { private string m_MyDisplay = ""; private int m_MyValue = 0; public MyPair() { this.MyDisplay = ""; this.MyValue = 0; } public string MyDisplay { get {return this.m_MyDisplay;} set {this.m_MyDisplay = value;} } public int MyValue { get {return this.m_MyValue;} set {this.m_MyValue = value;} } }And then, to add the data and bind the Combo:
MyPairCollection o = new MyPairCollection(); o.Add(new MyPair()); o[0].MyDisplay = "One"; o[0].MyValue = 1; o.Add(new MyPair()); o[1].MyDisplay = "Two"; o[1].MyValue = 2; this.cboCode.DataSource = null; this.cboCode.DataSource = o; this.cboCode.DisplayMember = "MyDisplay"; this.cboCode.ValueMember = "MyValue";I think this ends up being a lot more work than creating a DataTable, unless I missed something and did it wrong (which is entirely possible).
>>DataTable dt = new DataTable(); >>dt.Columns.Add("MyDisplay", typeof(string)); >>dt.Columns.Add("MyValue", typeof(int)); >> >>DataRow row = dt.NewRow(); >>row["MyDisplay"] = "One"; >>row["MyValue"] = 1; >>dt.Rows.Add(row); >> >>row = dt.NewRow(); >>row["MyDisplay"] = "Two"; >>row["MyValue"] = 2; >>dt.Rows.Add(row); >> >>// etc.etc. >> >>MyCombo.DataSource = dt; >>MyCombo.DisplayMember = "MyDisplay"; >>MyCombo.ValueMember = "MyValue"; >>>>