Click here to Skip to main content
15,901,373 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have combo box in windows form that data source has set to a List<myobj>. When I added object to this list do I need to reset the combo box datasourse.
Posted
Updated 4-May-11 19:35pm
v2

Well, it seems to me that you have all of the required tools to TRY it to see if you can do it.

Failing that, this is how I did it:

C#
public partial class Form1 : Form
{
    private List<string> myStrings = new List<string>();

    //--------------------------------------------------------------------------------
    public Form1()
    {
        myStrings.Add("1");
        myStrings.Add("2");
        myStrings.Add("3");
        myStrings.Add("4");
        myStrings.Add("5");

        InitializeComponent();

        comboBox1.DataSource = myStrings;
    }

    //--------------------------------------------------------------------------------
    private void button1_Click(object sender, EventArgs e)
    {
        comboBox1.DataSource = null;

        myStrings.Add("6");

        comboBox1.DataSource = myStrings;
    }

}


Seriously, it ain't rocket surgery...
 
Share this answer
 
v2
Comments
Tarun.K.S 5-May-11 14:27pm    
If it was in WPF, ObservableCollection would be the best choice, without the need to set the DataSource again. 5+
#realJSOP 5-May-11 14:31pm    
But he said it was WinForms, so what's the point of even bringing WPF into the discussion?
yesotaso 5-May-11 14:28pm    
Rocket surgery... Hmm is surgery a science or art? :)
Sergey Alexandrovich Kryukov 5-May-11 16:03pm    
This is a creative combination of the memes "This is a rocket science" and "This is a brain surgery". Much more funny then "brain science" which is something that (almost) really exist.

John rocks!
--SA
Hi there,

You need to state whether this is for a Windows Forms or Web Forms application.

Regardless, what you need to is set the "DataSource" property of the combo box each time the list is updated. If this is a web application, you need to call "DataBind" method after setting the data source.

Hope this helps :) Regards
 
Share this answer
 
Comments
#realJSOP 5-May-11 14:08pm    
Read the question - he DID state the platform.
Ed Nutting 5-May-11 14:15pm    
My 4 - would have been 5 if you had read the question carefully ;P
CodeHawkz 5-May-11 16:02pm    
Lolz :) He updated the question. If you look carefully, you'll see I posted this 1 hour earlier than he posted :)

Points I do not care much (rather at all), all I am trying is to help people. Thanks anyways :)
Item collection can't be modified when the DataSource property is set for a combBox. So, once we set DataSource property of dataset it doesn't updates its items thereafter.

But if we use comBoBox.Items.add() to add elements to it. Then it is posible easily.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900