Update the combo box automatically when the first combo box gets some value

I have two combined fields. I insert one value into the first combo box, and now I want my second combo box to update its value according to the first. How can I do it?

+4
source share
4 answers

Handle the SelectedIndexChanged event for the first ComboBox , then update the second combo box based on the SelectedItem for the first ComboBox .

Quick example (no error handling when choosing SelectedItem):

 public partial class Form1 : Form { private string[] comboBox1Range = new[] { "A", "B", "C", "D" }; private string[] comboBox2RangeA = new[] { "A1", "A2", "A3", "A4" }; private string[] comboBox2RangeB = new[] { "B1", "B2", "B3", "B4" }; private string[] comboBox2RangeC = new[] { "C1", "C2", "C3", "C4" }; private string[] comboBox2RangeD = new[] { "D1", "D2", "D3", "D4" }; public Form1() { InitializeComponent(); comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged; comboBox1.Items.AddRange(comboBox1Range); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { string selectedValue = comboBox1.SelectedItem as string; switch (selectedValue) { case "A": comboBox2.Items.Clear(); comboBox2.Items.AddRange(comboBox2RangeA); break; case "B": comboBox2.Items.Clear(); comboBox2.Items.AddRange(comboBox2RangeB); break; case "C": comboBox2.Items.Clear(); comboBox2.Items.AddRange(comboBox2RangeC); break; case "D": comboBox2.Items.Clear(); comboBox2.Items.AddRange(comboBox2RangeD); break; } } } 
+9
source

Subscribe to the first value of the modified combobox value and fill in the second:

 combobox1.SelectedIndexChanged+= new EventHandler(ListBox1_SelectedIndexChanged); private combobox1_SelectedIndexChanged(object sender, EventArgs e) { // do stuff with combobox2 } 

or

 combobox1.SelectedValueChanged += new EventHandler(ListBox1_SelectedValueChanged); private combobox1_SelectedValueChanged(object sender, EventArgs e) { // do stuff with combobox2 } 

Population:

 combobox2.Items.Add(new object()); combobox2.Items.Add(new ListItem("caption", "value")); // etc 

Find the existing item:

 var index = combobox2.FindStringExact(combobox1.SelectedText); if (index != -1) comobox2.SelectedItem = combobox2.Items[index]; 
+2
source

Sample code, Winform with two comboBoxes beatboxes for two tables in a dataset. The "lstCountries" table is a list of current countries. The "lstState" table is a list of all states / provinces for all countries.

LstCountries table (Int32 CountryID, row CountryName) lstStates table (Int32 StateID, Int32 CountryID, row StateName)

In this code, I populate cboState based on the selected cboCountry value while both combodropdowns are bound to data tables retrieved from the database.

 // Load data from database (not shown) // _dataSet.Tables["lstCountries"] datasource for cboCountry // _dataSet.Tables["lstStates"] datasource for cboState // // cboCountry - comboDropDown - List on countries // cboState = comboDropDown - List of states // Use boolean bloading to prevent setting datasource for cboState when cboCountry is intially loaded. void cboCountry_SelectedIndexChanged(object sender, EventArgs e) { ComboBox cbo = (sender as ComboBox); if (cbo.SelectedIndex > -1 && !bloading) { Int32 countryID = Convert.ToInt32(((System.Data.DataRowView)(cbo.SelectedItem)).Row.ItemArray[0].ToString()); cboState.Text = ""; DataView view = _dataSet.Tables["lstStates"].DefaultView; view.RowFilter = string.Format("CountryID={0}", countryID); DataTable table = view.ToTable(); cboState.DataSource = table; cboState.SelectedIndex = -1; } 
0
source

I want to select Ders this cmbDers and then cmbKonu, SQL related, like this Country-City. But I get a data type mismatch. What is the problem?

Loading the form OleDbDataAdapter adp = new OleDbDataAdapter ("select * from Ders", baglanti); DataTable dt = new DataTable (); baglanti.Open (); adp.Fill (dt); cmbDers.DataSource = dt; cmbDers.DisplayMember = "DersAd"; cmbDers.ValueMember = "DersID"; baglanti.Close ();

 private void cmbDers_SelectedIndexChanged(object sender, EventArgs e) { DataTable dt = new DataTable(); OleDbDataAdapter adp = new OleDbDataAdapter ("select * from Konu where DersID = '" +cmbDers.SelectedItem+"'",baglanti); adp.Fill(dt); cmbKonu.DataSource = dt; cmbKonu.DisplayMember = "KonuAd"; cmbKonu.ValueMem`enter code here`ber = "KonuID"; baglanti.Close();`enter code here` 
0
source

All Articles