In my program i have 2 ComboBoxes as DropDown Lists. I would like to add items to the second ComboBox only after an item from first ComboBox has been selected.
So far i have this:
InitializeComponent();
comboBox1.Items.Add("Category1");
comboBox1.Items.Add("Category2");
comboBox1.Items.Add("Category3");
comboBox1.SelectedValueChanged += new EventHandler(comboBox1_TextChanged);
private void comboBox1_TextChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedText.Equals("Category 1"))
{
DataTable cat = dataTableAdapter.GetByCategory("category1");
foreach (DataRow row in cat.Rows)
{
comboBox2.Items.Add(row.ItemArray[1]);
}
}
}
A note from MSDN about using ComboBox.SelectedText and the DropDownList style:
If DropDownStyle is set to DropDownList, the return value is an empty string ("").So perhaps you’ll have to use the SelectedIndex or SelectedItem property instead (or change the style of ComboBox to one of the other types).