2
votes

The problem is strange, I have a combobox which is correctly filled with items through the DataSource properties (it binds to a bindingSource item, which contains a displaymember and a valuemember), this is a simple combobox which allows me to select suppliers by choosing between their names and not their ids. The problem is that when I change SelectedValue because I want an option set as the default one, I discovered that the List property is empty (I inherited the combobox class in another one), However when the combobox is shown, I have items inside it, so maybe the problem is related to WHEN data is really loaded from the datasource (maybe it's loaded only after displaying it?). When should I change my SelectedValue so?There isn't any "Shown" event in the control

Thanks for any suggestions

Update 1: I tested in your way but it's not this the case (the key is a long but even if I cast it it's not working), in fact the interesting thing is that I have a setter that allows me to set SelectedValue, even if doesn't throw exceptions, in the next MessageBox the Items.Count give me 0 as value, which is very strange, because when the combobox is shown on the form it has all values i wanted. Here is the code for my setter:

    public object InputValue
    {
        get
        {
            return SelectedValue;
        }
        set
        { 
            SelectedValue = ((long)value);
            MessageBox.Show(this.Items.Count.ToString());
        }
    }
3
See my comment on King King's answer: stackoverflow.com/questions/18343855/…RLH

3 Answers

1
votes

You're not the only person who has experienced this problem, it's good to hear that it's not just me. What is not happening is something to do with type conversion - if you explicitly cast your SelectedValue to the exact type in the data source, you'll find that you are able to set the value successfully.

For example:

comboBox1.ValueMember = "Value";
comboBox1.DisplayMember = "Key";
object ds = new KeyValuePair<string, long>[] {
    new KeyValuePair<string,long>("a",0),
    new KeyValuePair<string,long>("b",1),
    new KeyValuePair<string,long>("c",2),
    new KeyValuePair<string,long>("d",3),
    new KeyValuePair<string,long>("e",4)
};
comboBox1.DataSource = ds;
comboBox1.SelectedValue = 3;        // this won't work
comboBox1.SelectedValue = (long)3;  // this will work

For whatever reason, even though '3' as a long is equal to '3' as an int, the ComboBox refuses to play ball unless you get the type precisely correct.

I can theorise that this has something to do with the fact that the WinForms ComboBox is a wrapper for the COM version, and different behaviour is exhibited after the native control has been created.

I hope this helps with your particular problem!

1
votes

I solved the problem, in fact as I thought the Items collection is filled "around" first invalidation of the combobox, so I simply stored the value submitted in as SelectedValue before the first invalidation in a temporary variable and then I just set it under the Invalidated event.

0
votes

i have this problem and i use BindingSource.Current property with CurrentChanged() event of the binding source and it work correctly