There are two (if not more) possible ways:
One is to add a typed dataset to your project and manually create a table with the respective columns. Then, create code to fill that table and add it as a datasource to your column using a BindingSource.
You can also use an object data source
Declare a class (e.g. MyDataClass
) that contains public members (must be a read/write property IIRC. If that doesn't work, create public member variables) for ID and value. Declare a generic list in your form like
private List<MyDataClass> m_dataSource = new List<MyDataClass>();
This list will be used as the data source for the column. Fill the list with one instance of MyDataClass
for each value you want to be displayed in the combo box.
Then, create a new object data source in your project for the MyDataClass
class. I'm using a German VS2008, so I don't know the exact names of the menu items. It should be "Data > Show DataSources" and in the datasource tool-window: Add new datasource. In the following dialog, select "Object" and select the class MyDataClass
.
You will then get a new data source that contains members for the properties declared above. Add a BindingSource to your form and one instance of the object data source. Assign that to the BindingSource (this is so that you can use the designer for selecting fields, etc.). Assign the BindingSource as DataSource for your column and select the ValueMember
and DataMember
properties accordingly.
Then, in code, after loading the data into the generic list, assign m_dataSource
to the BindingSource's DataSource property:
bindingSource.DataSource = m_dataSource;
Please remember that I'm writing this from my memory - there may be more steps involved, but it should work similar to what I've described.