I have created a ComboBox on a Windows Forms form. It is Databound to a column in a TableAdapter and the DataSource is a manually created KeyValuePair List. The problem is that when the form is displayed; the ValueMember is displayed in the ComboBox, not the DisplayMember. If you click the drop down, the Key List Values are displayed. When you make a selection, in the OnValidating method, the SelectedItem is -1.
I believe that the ComboBox is setup correctly. What have I done wrong?
this.cboFormat.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.BindingSource, "Format", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
InitializeComponent();
List<KeyValuePair<int, string>> loFormat = new List<KeyValuePair<int, string>>();
loFormat.Add(new KeyValuePair<int, string>(1, "Format 1"));
loFormat.Add(new KeyValuePair<int, string>(2, "Format 2"));
loFormat.Add(new KeyValuePair<int, string>(3, "Format 3"));
this.cboFormat.DataSource = new BindingSource(loFormat, null);
this.cboFormat.DisplayMember = "Value";
this.cboFormat.ValueMember = "Key";
Problem solved:
I found that if the Column in the DataBinding is an int, but the Value from the List is a string, then the problem above resulted. I changed the Databinding to a view that displayed the text from the lookup table the int tied to. The Key of the List would be the int from the lookup table. If that makes any sense.
SELECT DisplayMember FROM LookupTable AS LT INNER JOIN DataTable AS DT ON LT.Id = DT.LookupId.
Then the KeyValuePair worked as expected.