1

Having a problem with this Winforms project. Trying to use the SelectedIndexChanged event on a combBox that that was populated with the dictionary propList via:

comboBox1.DataSource = new BindingSource(propList, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";

This is the event itself:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox comboBox = (ComboBox)sender;
    //comboBox.DataSource = null;
    System.Collections.Generic.KeyValuePair<int, string> dummy =
        (System.Collections.Generic.KeyValuePair<int, string>) 
            comboBox.SelectedItem;

    //comboBox1.DataSource = new BindingSource(propList, null);
    PopulateCollars(dummy.Key);
}

It then throws this on the KeyValuePair cast:

Items collection cannot be modified when the DataSource property is set

I know this is the appriopriate cast, since debugging shows that:

SelectedItem  ~~~  object System.Collections.Generic.KeyValuePair<int,string>}

My question is then, why does an explicit cast modify the items collection? Being brought up on C++, where casts do not modify the data they are casting, this doesn't make sense to me.

As a note, using the lines commented out causes a null reference exception since apparently setting the data source to null wipes all members in the comboBox.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Ruud A.
  • 123
  • 2
  • 10
  • 1
    Which line is throwing the error? You have two casts. – Ann L. Sep 07 '12 at 21:49
  • The second, KeyValuePair. Casting sender into ComboBox works fine – Ruud A. Sep 07 '12 at 21:51
  • My first thought is to `var items = comboBox.Items`, `comboBox.DataSource = null`, and *then* retrieve your `dummy` from `items`. I have no answer for your thrown exception, though. Seems like a red herring. – Robert Harvey Sep 07 '12 at 21:51
  • Check `dummy` just to make sure that it's still null (or undefined). The reason I suggest this is that sometimes VS will attribute an error to the wrong line. This way you can confirm that neither the lines above or below are the real culprit. – Ann L. Sep 07 '12 at 21:55
  • @AnnL Checked 'dummy' via debugging, it is indeed undefined – Ruud A. Sep 07 '12 at 22:00
  • Your code is correct except the one `PopulateCollars(dummy.Key);` which we don't know. You must be modifying the `propList` in it. – L.B Sep 07 '12 at 22:00
  • @LB makes a good point: we don't know that this is the first invocation of SelectedIndexChanged, only that at some point the index changed. You could have indirectly caused it to change again through something you did in a successful call of this event handler. – Ann L. Sep 07 '12 at 22:04
  • I've since tried to find the exact line the exception occurs on, and it is in 'PopulateCollars' somewhere. That's on me to fix, thanks for helping me clear it up – Ruud A. Sep 07 '12 at 22:13

1 Answers1

0

By any chance, are you modifying either dummy.Key or dummy itself in PopulateCollars? Since I've never seen a cast change the data of an object either, that routine seems more likely to be the culprit. As I commented above, sometimes VS is just plain wrong about what line threw the error.

ETA: Okay, that's not it. Here's an idea. Examine the stack trace of the exception, and see whether there's any interesting layers between your method and the method that (in the framework) threw the exception. Also make sure that this method is not being recursively invoked through something in PopulateCollars that causes the list to change.

If you are trying to reuse the same combo box with other data while handling a change caused by the combo box, that probably won't work.

Ann L.
  • 13,760
  • 5
  • 35
  • 66
  • Unfortunately, no, 'dummy.Key' is used for a single assignment and nothing else – Ruud A. Sep 07 '12 at 22:04
  • Is it possible you have more than one combo box using the same event handler? – Ann L. Sep 07 '12 at 22:07
  • There is a second comboBox, but it has its own handler. I also since tried to find the exact line the exception occurs on, and it is in PopulateCollars somewhere. Assumptions will be my death someday, thanks for your time – Ruud A. Sep 07 '12 at 22:12