Click here to Skip to main content
12,631,606 members (32,260 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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 19-Dec-12 5:03am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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...
  Permalink  
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?
Krunal Rohit 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
Krunal Rohit 19-Dec-12 10:39am
   
Its possible...
shaikh-adil 19-Dec-12 10:45am
   
can you guide me to achieve
please if you can
shaikh-adil 19-Dec-12 10:36am
   
wow, you have almost every link for every quetion ready????
you are indian google, paaji
:D
Krunal Rohit 19-Dec-12 10:39am
   
No man.. I know all this stuff.. so easily I can get it... and ya I'm not a sir paaji.. Just a 20 years old student.. :) :)
shaikh-adil 19-Dec-12 10:42am
   
why you are lying, you are doing BCA and you are only 20 years??
i am doing my graduation in mumbai, BSc.IT 3rd year and i am 21.
so how can you be 20
Krunal Rohit 19-Dec-12 10:45am
   
I'm at my final year.. Its my last year.. Only one and almost a half semester is remaining.. :)
shaikh-adil 19-Dec-12 10:47am
   
same here, 5th sem is over and 6th semester will be at march start or in feb. of b.sc IT
you have done your H.Sc and joined BCA course?
Krunal Rohit 19-Dec-12 10:50am
   
please contact me on following addresses.. Because here its not good to talk like this... hope you'll understand :) :)
Krunal Rohit 19-Dec-12 10:47am
   
my dob is 11 dec 1992 so it could be possible... If you want to know more about me,
gmail : kunalrohit92@gmail.com
skype : kunal.rohit2
fb : k.rohit2
shaikh-adil 19-Dec-12 10:58am
   
can you guide me to achieve please if you can
for column binding and changing the column as per the first combobox?
please
Krunal Rohit 19-Dec-12 10:59am
   
I need to work out on that first..
shaikh-adil 19-Dec-12 11:04am
   
okay bro, thanks for working on that.
and tell me please if you get the answer
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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[^]
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 );
                }
            }
        }
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100