| | |
combo box keeps adding new items and doesnt delete previous items.
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 4
Reputation:
Solved Threads: 0
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:
thanks in advance.
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.
Have you tried setting the .DataSource of the combobox to null, then clearing it, then binding it? Also, there is probably a more elegant way to write that function, since all 3 if blocks are identical except for the where clause.
-Ryan Hoffman
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
.NET Specialist / Webmaster, Extended64.com.
Please do not email or PM me with support questions. Please direct them to the forums instead.
•
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 3
Dear
in first you should know that you bind the combobox to dataset, then you should deal with the dataSet in clearing and filling it.
DataAdapter.Fill method adds rows incase of the table is created in DataSet, so everytime you select from first combobox then data adapter add the new rows to the table ("Equipment") .
The solution :
add the next code to clear the rows:
There is another thing :
remove this line from the code because this will give you runtime error , no need for this line now:
in first you should know that you bind the combobox to dataset, then you should deal with the dataSet in clearing and filling it.
DataAdapter.Fill method adds rows incase of the table is created in DataSet, so everytime you select from first combobox then data adapter add the new rows to the table ("Equipment") .
The solution :
add the next code to clear the rows:
C# Syntax (Toggle Plain Text)
if (dsLCD.Tables.Count > 0 && dsLCD.Tables[0].Rows.Count > 0) dsLCD.Tables[0].Rows.Clear();
There is another thing :
remove this line from the code because this will give you runtime error , no need for this line now:
C# Syntax (Toggle Plain Text)
this.comboUnitAvailable.Items.Clear();
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Senior Solution Developer
Please rate my answer
![]() |
Similar Threads
- Adding items to a combo box related to data selected in another combo box (C#)
- Vb[2008] Populating combo box from textfile (over 45,000 lines) (VB.NET)
- DataGridView Combo box (VB.NET)
- sending the a combo box to another form (PHP)
- Combo Box (VB.NET)
- Remove an item from a combo box based on the first letter (VB.NET)
- can not update combo box... (VB.NET)
- the combo box (Community Introductions)
- Combo box problem (Java)
- Perplexed: Combo Box Not Holding Data (C#)
Other Threads in the C# Forum
- Previous Thread: design document
- Next Thread: instantiate the generic type within the generic class
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser wia windows winforms wpf xml





