Uploaded to WindowsClient.net by  admin on 05-10-2007

You can fill a ComboBox with non-string items such as business objects.  By default, the ComboBox will call ToString() on each of the items to generate the display text (visible text).  Rather than rely on ToString(), you can have the ComboBox use one of the properties on the contained objects (business object) as the display text.  To do this, set the ComboBox.DisplayMember to the property name you want to use for the display text.  You can also set the ValueMember to a property on the contained objects that represents the object unique key.  You can then use the ComboBox “SelectedValue” property to set the ComboBox selected item via object key.

Sample: ComboBox DisplayMember and ValueMember (VS 2005) (VS Project: ComboBoxBinding)

 

 

A common use of a ComboBox in a data bound application is as a lookup based UI control.  From a database perspective, a Lookup control is used to provide the “lookup” values for a foreign key.  For example, assume you have a customer table with a “StateID” where the “StateID” is a foreign key into a “States” table.  From a UI perspective, you’d like to allow the user to select a State via a ComboBox.  To do this, you use Complex Binding to bind the ComboBox to the States table (setting the ComboBox.DisplayMember and ComboBox.ValueMember) and use Simple List Binding to bind the ComboBox SelectedValue to the Customer “StateID”.

Sample: ComboBox as a Lookup table (VS 2005) (VS Project: ComboBoxBinding)

 

 

When data bound, the Windows Forms ComboBox does not provide a general way to add a “null” or “not selected” value to its items list.  The only generally supported way to do this is to add a “null” item to your data source list.  Building on the previous states example, this would require an additional “null” row to be manually added to the States table.

Sample: Adding a “null” value to a Lookup based ComboBox (VS 2005) (VS Project: ComboBoxBinding)

 

 

ComboBox binding does not directly support data source property concatenation however you can use the ComboBox Format event to concatenate multiple data source properties.

Sample: Concatenating data source properties using the Format event (VS 2005) (VS Project: ComboBoxBinding)

 

 

 

C#
Edit|Remove
/* Need to add a using statement for System.Globalization           */ 
/* Bind a ComboBox (culturesCB) to a current framework cultures     */ 
this.culturesCB.ValueMember = "LCID"this.culturesCB.DataSource = CultureInfo.GetCultures(CultureTypes.FrameworkCultures); 
 
/* Set the currently selected item in the ComboBox                  */ 
this.culturesCB.SelectedValue = CultureInfo.CurrentCulture.LCID; 
 
/* Concatenate the IetfLanguageTag and DisplayName properties       */ 
this.culturesCB.Format += delegate(object cb, ListControlConvertEventArgs args) 
{ 
    CultureInfo ci = (args.ListItem as CultureInfo); 
 
    if (null != ci) 
    { 
        args.Value = string.Format("{0}: {1}", ci.IetfLanguageTag, ci.DisplayName); 
    } 
}; 
 
 
 

 

C#
Edit|Remove
/* Create a new Customer                                */ 
Customer cust = new Customer("Joe"null); 
 
/* Add null value                                       */ 
DataRow row = statesTable.NewRow(); 
 
/* Enter a null row (ComboBox will show blank)          */ 
row["Name"] = ""; 
row["Code"] = DBNull.Value; 
 
/* Add the row to DataTable                             */ 
statesTable.Rows.Add(row); 
 
/* Bind the States ComboBox to the states DataTable     */ 
this.statesCB.DisplayMember = "Name"this.statesCB.ValueMember = "Code"this.statesCB.DataSource = statesTable; 
 
/* Bind the ComboBox SelectedValue to the customer      */ 
/* business object                                      */ 
this.statesCB.DataBindings.Add("SelectedValue", cust, "StateID"true); 
 
 
 

 

C#
Edit|Remove
/* Create a new Customer (customer has “Name”           */ 
/* and “StateID” properties)                            */ 
Customer cust = new Customer("Joe""WA"); 
 
/* Bind the States ComboBox to the states DataTable     */ 
/* Display the "Name" property in the ComboBox          */ 
/* Use the "Code" property as the "Key"                 * 
 
/* StatesTable contains US state information            */ 
/* Name = full state name                               */ 
/* Code = state unique key                              */ 
this.statesCB.DisplayMember = "Name"this.statesCB.ValueMember = "Code"this.statesCB.DataSource = statesTable; 
 
/* Set the default States ComboBox selected value       */ 
/* to the Customer's StateID ("WA")                     */ 
/* This will make the ComboBox show "Washington"        */ 
/* If the States ComboBox is changed, the Customer      */ 
/* StateCode will be automatically updated              */ 
this.statesCB.DataBindings.Add("SelectedValue", cust, "StateID"true); 
 
 
 

 

C#
Edit|Remove
/* Need to add a using statement for System.Globalization           */ 
/* Bind a ComboBox (culturesCB) to a current framework cultures     */ 
this.culturesCB.DisplayMember = "EnglishName"this.culturesCB.ValueMember = "LCID"this.culturesCB.DataSource = CultureInfo.GetCultures(CultureTypes.FrameworkCultures); 
 
/* Set the currently selected item in the ComboBox                  */ 
this.culturesCB.SelectedValue = CultureInfo.CurrentCulture.LCID;