| | |
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 angle array barchart bitmap box broadcast c# capturing check checkbox client combobox control conversion convert csharp custom database datagrid datagridview dataset datetime dbconnection degrees delegate design development disappear draganddrop drawing encryption enum event excel file firefox form format forms function gdi+ httpwebrequest image index input install java label leak libraries list listbox mandelbrot math monodevelop mouseclick msword mysql operator path pause photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox round server sleep socket sql statistics stream string table tcpclientchannel text textbox thread time timer update usercontrol validation virtualization visualbasic visualstudio webbrowser windows winforms wpf xml





