Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Can anyone help me with how to populate a combobox with data once another combobox has been selected? I'm confused as to how the selectedindexchanged events work. I am using C# with Visual Studio 2010/2012.
Posted
Updated 24-Jun-21 19:57pm
Comments
Member 15261963 25-Jun-21 3:58am    
Combobox change system date and time ?
Plz give me answer...

suppose we have 2 tables one containing records of student and other containing their subjects

we have 2 combo box one for students and other for their subjects

'''' giving value to first combobox
C#
dataset ds = new dataset() ;
string querry = "select studentid,studentname from tbstudent";
sqlcommand cmd = new sqlcommand(querry,connection);
dataadapter da = new sqldataadapter(cmd);
combobox1.valuemember = "studentid";
combobox1.displaymember = "studentname";
combobox1.datasource = ds.tables(0);



now on combobox1 selected index event

C#
dataset ds = new dataset() ;
 string querry = "select subjectid,subjectname from tbsubject where studentid = combobox1.selectedvalue";
 sqlcommand cmd = new sqlcommand(querry,connection);
 dataadapter da = new sqldataadapter(cmd);
 combobox2.valuemember = "subjectid";
 combobox2.displaymember = "subjectname ";
 combobox2.datasource = ds.tables(0);
 
Share this answer
 
v3
Hi ,
Check this
C#
private void Form1_Load(object sender, EventArgs e)
        {
         DataTable dt  = new DataTable();
        using (SqlConnection Cn = new SqlConnection(Properties.Settings.Default.con))
        {
            using (SqlCommand Cmd = new SqlCommand("SELECT * FROM  Orders  where oid = @oid", Cn))
            {
                Cn.Open();            
               SqlDataAdapter  adpt = new SqlDataAdapter(Cmd);
                adpt.Fill(dt);
                comboBox1.DataSource = dt;
                comboBox1.DisplayMember="Orders";
                comboBox1.ValueMember="id";
            }
        }
     }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        DataTable dt  = new DataTable();
        using (SqlConnection Cn = new SqlConnection(Properties.Settings.Default.con))
        {
            using (SqlCommand Cmd = new SqlCommand("SELECT * FROM  OrdersDetails  where oid = @oid", Cn))
            {
                Cn.Open();
                Cmd.Parameters.AddWithValue("@oid" ,Convert.ToInt32( comboBox1.SelectedValue));
               SqlDataAdapter  adpt = new SqlDataAdapter(Cmd);
                adpt.Fill(dt);
                comboBox2.DataSource = dt;
                comboBox2.DisplayMember="amount";
                comboBox2.ValueMember="id";
            }

        }
      
    }

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
Аslam Iqbal 11-Sep-12 4:39am    
You got my 5:)^2
Mohamed Mitwalli 11-Sep-12 4:43am    
Thanks Aslam :)
__TR__ 11-Sep-12 8:32am    
+5
Mohamed Mitwalli 11-Sep-12 8:36am    
Thanks TR :)
 
Share this answer
 
Comments
ridoy 11-Sep-12 1:20am    
owao!plenty of links..+5
Prasad_Kulkarni 11-Sep-12 2:01am    
Thank you Ridoy!
Mohamed Mitwalli 11-Sep-12 1:25am    
5+ For web :)
Prasad_Kulkarni 11-Sep-12 2:02am    
Thank you Mohamed!
__TR__ 11-Sep-12 8:32am    
+5
Here is some sample code:
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   comboBox2.Items.Clear();
   if (comboBox1.SelectedItem.ToString() == "1")
   {
      comboBox2.Items.Add("Hello");
      comboBox2.Items.Add("World");
   }
   else
   {
      comboBox2.Items.Add("Test1");
      comboBox2.Items.Add("Test2");
   }
}

just subscribe to the first combobox SelectedIndexChanged event and in the event handler change the content of the second combo box.
 
Share this answer
 
Comments
ridoy 11-Sep-12 1:20am    
good one..+5
Mohamed Mitwalli 11-Sep-12 1:22am    
5+

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