I have a comboBox that is populated by an array. When one of the choices is no longer available, I'd like to remove that item from the comboBox.
I tried

if (firstClassAvailable == 0)
                    {
                        this.comboBox1.Items.Remove("First");                        
                    }

which throws this exception

System.ArgumentException: Items collection cannot be modified when the DataSource property is set.

what's the proper way to do this?

The comboBox is bound to a data source with

this.comboBox1.DataSource = flightClassAr;

where flightClassAr is a string array with just two elements, "First" and "Business"
I thought I could just declare a single string variable
ie. string myNewVariable = "Business" and execute

this.comboBox1.DataSource = myNewVariable;

but that doesn't work either.

Thanks for your time in advance.

Jim

You need to use List<T> instead of string array.

List<String> data = new List<string>()
        {
            "First",
            "Second",
            "Third",
            "Fourth"
        };
        private void button1_Click(object sender, EventArgs e)
        {
            data.RemoveAt(comboBox1.SelectedIndex); // or  data.Remove(comboBox1.Text);
            comboBox1.DataSource = null;
            comboBox1.DataSource = data;
        }

        private void TestOne_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource =data ;
        }

Thanks! I've only been working with C# (or any flavor of C) for about 6 weeks now. I have not covered the List<T> command in any of my books yet, or if I did, I read right over it without it sinking in. I've been dividing my time between reading as much text as I can and hands on projects in VS, but there's a lot, lot, lot to cover.

Jim

@revjim44

Once you learn Generics, I'm sure that you will never think of arrays.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.