1 2 // insert code here.. 3 Bind a ComboBox to an array of State objects 4 5 namespace Akadia.ComboBoxBinding 6 { 7 8 using System; 9 using System.ComponentModel; 10 using System.Drawing; 11 using System.Windows.Forms; 12 using System.Data; 13 using System.Data.SqlClient; 14 15 public class ComboBoxBinding : System.Windows.Forms.Form 16 { 17 ..... 18 19 // Lookup "Table" with States for the Comobox 20 public struct State 21 { 22 private string _shortName, _longName; 23 24 public State(string longName , string shortName) 25 { 26 this._shortName = shortName; 27 this._longName = longName; 28 } 29 30 public string ShortName { get { return _shortName; } } 31 public string LongName { get { return _longName; } } 32 } 33 34 // Define the Array of States for the DropDown List 35 public State[] States = new State[] { 36 new State("Alabama","AL") 37 ,new State("Alaska","AK") 38 ,new State("Arizona" ,"AZ") 39 ,new State("Arkansas","AR") 40 ,new State("California" ,"CA") 41 ...... 42 } ; 43 44 public ComboBoxBinding() 45 { 46 47 ....... 48 49 // Populate the list 50 comboBoxState.DataSource = States; 51 52 // Define the field to be displayed 53 comboBoxState.DisplayMember = "LongName"; 54 55 // Define the field to be used as the value 56 comboBoxState.ValueMember = "ShortName"; 57 58 // Bind the selected value of the the ComboBox to the 59 // Region field of the current Customer 60 comboBoxState.DataBindings.Add("SelectedValue", customersDataSet1, "Customers.Region"); 61 62 ........ 63 } 64 } 65 } 66
You need to create an account or log in to post comments to this site.