Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8539793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:27:04+00:00 2026-06-11T11:27:04+00:00

Having a problem with this Winforms project. Trying to use the SelectedIndexChanged event on

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:27:06+00:00Added an answer on June 11, 2026 at 11:27 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having this problem. I'm trying to build an UITableView with data from a
I'm having trouble thinking of a solution to this problem that doesn't involve a
I am having problem with this javascript function. If I want to submit a
I am having problem getting this piece of code to run. The class is
I'm not a MySQL expert and am having problem with this... So, I have
I have this problem having to display my jasper report on my grails application.
I've having this problem with some JavaScript code. I've simplified it in the error
I'm having this problem where I keep getting this error in my console: *
I'm having this problem with the grails liferay-plugin: localhost_liferay_portlet_WEB_INF_grails_app_views_test_view_gsp: 17: expecting anything but ''\n'';
We are having this problem with a controller right now; the controller looks like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.