Paul Stovell gets development questions and he's not afraid to answer them. "I recieved an email this morning as from Steve Pietrek, a Delphi programmer moving to .NET (great choice Steve!). His question essentially asked how would he go about binding a Windows Forms ComboBox to an enumeration such as this:
public enum CustomerType {
Type1 = 1,
Type2 = 2,
Type3 = 3
}
Fortunately, I figured out how to do this in Trial Balance about a week ago, so I thought I’d share my approach here.
The values in an enumeration (Type1, Type2, Type3 in this
"One of the most frequent questions I get regarding data binding is how to hook up a combo box with a data bound list of selections from a related table or collection, and have selections in that combo box set a corresponding value in the related collection. For example, in the following form, the main collection is the Products table from Northwind. The textboxes are displaying a couple of the columns from that table, and the combo box is displaying the supplier name from the related Suppliers table. Products contains a foreign key column with a
This article is pretty extensive with examples and links to more information. Here is a sample to address a common issue:
"Binding to a ComboBox or ListBox. This sample demonstrates binding data to a ComboBox. Binding data to a ListBox follows the same model.
To bind data to the list of items that are displayed, set the DataSource and DisplayMember properties of the ComboBox. The DisplayMember property is used to determine which property of the State object to display in the ComboBox. For example, the following code binds a ComboBox to an array of
Every once in a while I need to bind an enumerated type to a Windows Forms control, usually a ComboBox. There are lots of articles here on The CodeProject that present various ways to do this, each with their own pros and cons. However, they are generally more complicated than necessary, and in some cases, require a lot of work on either the developer implementing the enum, the developer using it, or both.
The Simple Way
The simplest is to use the Enum.GetValues() method, setting its result to the DataSource property of the ComboBox. If you have the
Introduction
Every once in a while I need to bind an enumerated type to a Windows Forms control, usually a ComboBox. There are lots of articles here on The CodeProject that present various ways to do this, each with their own pros and cons. However, they are generally more complicated than necessary, and in some cases, require a lot of work on either the developer implementing the enum, the developer using it, or both.
The Simple Way
The simplest is to use the Enum.GetValues() method, setting its result to the DataSource property of the ComboBox.