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 9013025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:09:03+00:00 2026-06-16T03:09:03+00:00

Following is my code which is binding a List() to a ComboBox control. I

  • 0

Following is my code which is binding a List() to a ComboBox control. I tried to add few items to the CombBox control using Insert() method which is not allowed as it has been assigned to a datasource. So, how can I get the datasouce back to a new variable (say, var colours2) from cmbColour.DataSource which is returning an object. Thanx !

var colours= new Dictionary<string, string>
            {
              {"1x","Green"},
              {"2x","Red"},
              {"3y","Blue"},
              {"4y","Black"}
            }.ToList();

cmbColour.ValueMember = "Key";
cmbColour.DisplayMember = "Value";
cmbColour.DataSource = colours;

var colours2 = //how can I get the DataSource back 
  • 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-16T03:09:05+00:00Added an answer on June 16, 2026 at 3:09 am

    The following code will return a new dictionary containing the same data you bound to the combo box.

    var list = (List<KeyValuePair<String, String>>)cmbColor.DataSource;
    
    var dictionary = list.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
    

    The property DataSource will return the same instance you assigned but because it is typed Object you have to cast it back to the actual type before you can access any members.

    But why don’t you just keep the original dictionary? And it is definitely supported to modify a list bound to a data source – that is the whole point of data binding.

    I guess my answer does not really solve your actual problem, only what you think what your problem is. Maybe you can give some additional information about what you are trying to achieve and I or someone else will be able to help you with your underlying problem.

    UPDATE

    This should work for your scenario – I stick with the user example.

    public class User
    {
        public String Id { get; set; }
    
        public String Name { get; set; }
    }
    

    And the code for the form.

    public partial class MainForm : Form
    {
        private readonly BindingList<User> recentlyAddedUsers = new BindingList<User>();
    
        private void MainFormLoad(Object sender, EventArgs e)
        {
            this.comboBoxRecentlyAddedUsers.DataSource = this.recentlyAddedUsers;
    
            this.comboBoxRecentlyAddedUsers.ValueMember = "Id";
            this.comboBoxRecentlyAddedUsers.DisplayMember = "Name";
    
            var recentlyAddedUsersFromService = this.GetRecentlyAddedUsers();
    
            foreach (var user in recentlyAddedUsersFromService)
            {
                this.recentlyAddedUsers.Add(user);
            }
        }
    
        private void ButtonAddNewUserClick(Object sender, EventArgs e)
        {
            var newUser = new User();
    
            newUser.Id = this.textBoxUserId.Text;
            newUser.Name = this.textBoxUserName.Text;
    
            this.SaveNewUser(newUser);
    
            this.recentlyAddedUsers.RemoveAt(0);
            this.recentlyAddedUsers.Insert(newUser);
        }
    
        private List<User> GetRecentlyAddedUsers()
        {
            // Get a list of recently added users from the database.
        }
    
        private void SaveNewUser(User user)
        {
            // Save the new user to the database.
        }
    }
    

    Note the usage of BindingList<T> – this will notify the combo box about any changes to the list. A simple List<T> would also work but then you have to explicitly tell the combo box to refresh the data binding.

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

Sidebar

Related Questions

I have the following code which utilises Guava's Files.readLines() method: List<String> strings = Lists.newArrayList();
I have the following code which takes List of boolean as a parameter then
I am using an autocomplete box which is binded to a list in code-behind.
I have the following code which definitely returns a proper data result if I
I have the following code which is working, I was wondering if this can
I have the following code which is used to upload large files (~6MB) to
i have the following code which switches some fullscreen-background-images (fadeOut, fadeIn). setInterval(function() { var
I have the following code which is fine if I give invalid parameters (though,
I have the following code which will generate two pdf files containing plots to
I have the following code which creates a simple window with two buttons which

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.