First, create a class containing two members: value and id. Value is what is displayed in the combobox while id is associated with that value. I like to create a “global” class to contain properties and classes that is used through out the project.
In this case, I have a class defined as clsCHTLCommon. In this class, I’ve defined this class:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5: using System.Data;
6: using baseSql;
7:
8: namespace CHTL_HMI
9: {
10: public class clsCHTLCommon
11: {
12:
13:
14: public clsCHTLCommon()
15: {
16: }
17:
18:
19: }
20:
21: public class TrkItems
22: {
23: public string Value { get; set; }
24: public int Id { get; set; }
25: }
26:
27: public class SelectedTrkItems
28: {
29: public string ChargeCoilNumber { get; set; }
30: public int ScheduledCoilKey { get; set; }
31: public int ChargeCoilKey { get; set; }
32: public int CCSN { get; set; }
33: }
34: }
Now, define a list based upon the class and fill it in with TrkItems:
1: List<TrkItems > itemHeadCollection = new List<TrkItems >();
2:
3: for (i = clsGlobalDefs .TrkZone.WELDER; i < clsGlobalDefs.TrkZone .TC1; i++)
4: {
5: TrkItems cboItem = new TrkItems();
6:
7: cboItem.Value = GlobalDefs.L2TrkZoneNames[i - 1];
8: cboItem.Id = i;
9: itemHeadCollection.Add(cboItem);
10:
11: }
Next, set the combobox DataSource to the List and set the DisplayMember to the value data member in the TrkItems class:
1: cboHead.DataSource = itemHeadCollection;
2: cboHead.DisplayMember = "Value";