so my program goes like this:
combobox1 controls conbobox2, so if I choose an item in combobox1, combobox2 will be populated by my query from my database.
the problem is that, whenever i try to choose another item from combobox1, combobox2 will only stack the items from the database, and does not clear the items from previous selection.
all i want is that, whenever i choose a new item from combobox1 combobox2 will update its contents (deleting the previous items and populating it with the new items from my database)
i tried "this.combobox2.Items.Clear();" but the error "Items collection cannot be modified when the DataSource property is set." will show up.
heres my code:
private void comboUnitType_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboUnitType.SelectedIndex.Equals(0))
{
this.comboUnitAvailable.Show();
this.comboUnitAvailable.Items.Clear();
Connect = new SqlConnection(sqlConnStatement);
Connect.Open();
daAddReserve = new SqlDataAdapter(
"SELECT Equipment_Name FROM Equipment WHERE Equipment_Type = 'CPU'", Connect);
cbAddReserve = new SqlCommandBuilder(daAddReserve);
daAddReserve.Fill(dsLCD, "Equipment");
comboUnitAvailable.DataSource = dsLCD.Tables[0];
comboUnitAvailable.DisplayMember = "Equipment_Name";
Connect.Close();
}
else if (comboUnitType.SelectedIndex.Equals(1))
{
this.comboUnitAvailable.Show();
this.comboUnitAvailable.Items.Clear();
Connect = new SqlConnection(sqlConnStatement);
Connect.Open();
daAddReserve = new SqlDataAdapter(
"SELECT Equipment_Name FROM Equipment WHERE Equipment_Type = 'Laptop'", Connect);
cbAddReserve = new SqlCommandBuilder(daAddReserve);
daAddReserve.Fill(dsLCD, "Equipment");
comboUnitAvailable.DataSource = dsLCD.Tables[0];
comboUnitAvailable.DisplayMember = "Equipment_Name";
Connect.Close();
}
else if (comboUnitType.SelectedIndex.Equals(2))
{
this.comboUnitAvailable.Show();
this.comboUnitAvailable.Items.Clear();
Connect = new SqlConnection(sqlConnStatement);
Connect.Open();
daAddReserve = new SqlDataAdapter(
"SELECT Equipment_Name FROM Equipment WHERE Equipment_Type = 'OHP'", Connect);
cbAddReserve = new SqlCommandBuilder(daAddReserve);
daAddReserve.Fill(dsLCD, "Equipment");
comboUnitAvailable.DataSource = dsLCD.Tables[0];
comboUnitAvailable.DisplayMember = "Equipment_Name";
Connect.Close();
}
thanks in advance.