Click here to Skip to main content
15,887,775 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi, i want to change the list if second combobox if the first combobox selection changes, i have two combobox first contains the names of mobile companies like htc samsung and nokia, and i want to change the list of second combobox when the first combobox selection changes
i have done this


  string[] st2 = new string[] { "Galaxy S", "Galaxy SII", "Galaxy SIII" };
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            if (comboBox1.SelectedIndex == 1 )
            {
                comboBox2.Items = st2 // this is not happing here what can i do?
            }
        }
Posted

C#
string[] samsung = new string[] { "Galaxy S", "Galaxy SII","Galaxy SIII" };
string[] apple = new string[] {"iPhone4", "iPhone4S"};
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            if ("Samsung" == comboBox1.SelectedItem.ToString())
            {
                comboBox2.Items.AddRange(samsung);
            }
            else if("Apple" == comboBox1.SelectedItem.ToString())
            {
                comboBox2.Items.AddRange(apple);
            }
        }


If its not working, let me know...
 
Share this answer
 
v3
Comments
shaikh-adil 19-Dec-12 10:29am    
thank you sir,verymuch thanks if i want to bind the combobox with a database table and update the values according to that is that possible?
thank you thank you sir,
if i want to bind the combobox with a database table and update the values according to that is that possible?
in one column i have the mobile company name and another table i have the model name if company name is selected the the model column will be shown in combobox 2?
is that possible sir?
[no name] 19-Dec-12 10:31am    
Yes it possible..
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/5ca52b03-1819-411d-aec7-b5ddc1e5d368

http://www.c-sharpcorner.com/blogs/6546/databinding-in-combobox-using-c-sharp.aspx
shaikh-adil 19-Dec-12 10:37am    
updated my comment please check it
[no name] 19-Dec-12 10:39am    
Its possible...
shaikh-adil 19-Dec-12 10:45am    
can you guide me to achieve
please if you can
You need to add the strings into the Items collection: How to: Add and Remove Items from a Windows Forms ComboBox, ListBox, or CheckedListBox Control[^]
C#
string[] st2 = new string[] { "Galaxy S", "Galaxy SII", "Galaxy SIII" };

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            if (comboBox1.SelectedIndex == 1 )
            {
                foreach( string st in st2 )
                {
                    comboxBox2.Items.Add( st );
                }
            }
        }
 
Share this answer
 
Comments
shaikh-adil 19-Dec-12 10:33am    
thank you thank you sir,
if i want to bind the combobox with a database table and update the values according to that is that possible?
in one column i have the mobile company name and another table i have the model name if company name is selected the the model column will be shown in combobox 2?
is that possible sir?

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