1
votes

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.

2
I tried a Dictionary first with the following and I do not have the problem with the SelectedIndex, but the problem with the ValueMember remained the same; Dictionary<int, string> loFormat = new Dictionary<int, string> { {1, "Format 1"}, {2, "Format 2"}, {3, "Format 3"} }; - ggrewe1959

2 Answers

4
votes

I threw together a window with a ComboBox and a Button. I don't see what you describe at all. My code looks something like this:

    BindingSource bs = new BindingSource();
    public Form1()
    {
        InitializeComponent();
        comboBox1.DataBindings.Add(new Binding("Text", bs, "Format", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
        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"));
        comboBox1.DataSource = new BindingSource(loFormat, null);
        comboBox1.DisplayMember = "Value";
        comboBox1.ValueMember = "Key";
    }

    private void comboBox1_Validating(object sender, CancelEventArgs e)
    {
        Console.WriteLine(comboBox1.SelectedItem);
    }

The validating handler has the correct selected item and it initializes just fine. Maybe it is something else that you haven't show in your code snippet.

0
votes

Why are you using a List<KeyValuePair<int,string>> instead of a Dictionary<int,string>?

Anyway, I do believe your problem is located in the fact that since the binding is done against a List of KeyValuePair; the code will think that it should look for the values of DisplayMember in the List (that is, not the KeyValuePair) and finds the Value being the KeyValue and doesn't find any Key, so it does some odd parsing (READ: not sure about why you get a -1). Think switching from List<KeyValuePair<int,string>> to a dictionary might just solve it.