Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing all items from a combo box

Status
Not open for further replies.

kimprogrammer

Programmer
Sep 15, 2008
160
CA
Hello
I have three comboboxes set up -divison,department,employee - when I select either divison or division and department I populate the employee combobox with a tableadapter and a parameter.
Now that I'm doing my testing if I select division my employee combobox is populated. But it I go back and reselect a division my employee combobox still has the employee. I would like to clear these

I have found .items.remove(selectedindex) but I'd like to clear them all. I thought maybe something like a removerange but have not found anything about this.

How is this done?
 

Just guessing here, but:
Code:
ComboBox1.Items.Clear()
???

Have fun.

---- Andy
 
Thanks but thats not it

It gives you an error saying
"Items collection cannot be modified when the DataSource property is set.
 

How exactly are you populating the combobox in question?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
All my comboboxes are being populated by tableadapters that have a bindingsource set up table.

I have the division and department table adapter loading in the load form.
the employee box is loaded when I select either division or division then department.

When I select a division - the employee combo box is populated with a division parameter.

I have one division that is really large and needs them to have the department selected as well so the managers see only their employee.

So in my testing I selected a small division and that divisions employees got populated in the combobox. But I went back and selected the larger one - (so the employees have not been repopulated yet because i have not yet selected the department). I was then able to select the employee from the first division I selected- which i could then go and write data to the file.

 
Try the following:

Load the EmployeeAdapter at the form load with the rest ot the table adapters. It should include all the possible Employees.... i.e. no filter.

Then using the SelectedIndexChanged event of the combobox, set the filter of the BindingSource object of the TableAdapter. This filter would be the same as the WHERE clause in a SQL statement without the word WHERE.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
I understand what you mean, But I'm new to and am self teaching myself to do this project. And I've only found one post with the filter and my books don't go into it -

I've moved my table adapter into the load
'TODO: This line of code loads data into the 'HYLTDDataSet.CPY10100' table. You can move, or remove it, as needed.
Me.CPY10100TableAdapter.Fill(Me.HYLTDDataSet.CPY10100)
-------------------------------
My sql statement I was using in the data source was
SELECT PEmployeeID, PEmployeeClass, PInactive, PLastName, PFirstName, PDepartment, PEmployeePaymentMethod, PJobTitle FROM CPY10100 WHERE (PEmployeeClass = @Param1) and PInactive=0
--------------------------

This is my filter. If I dont put "" around pEmployeeClass and PInactive I get error but when I do like the example I saw disallows conversion to string to double.

Because I'm not familiar with this could you explain more about what I don't have set correctly.

CPY10100BindingSource.Filter = "PEmployeeClass" = PClass And "PInactive" = 0
----------------------------------




Thanks for the help
Kim
 
Since you are setting the Filter property via code, the whole thing will be a string. Also have to remember that any string value you are trying to filter on needs to be enclosed in single quotes, as per SQL styntax.

Try:

CPY10100BindingSource.Filter = "PEmployeeClass = '" & PClass & "' And PInactive = 0"

The above assumes that PInactive is a bit or integer field. PEmployeeClass is assumed to be a string value and that PClass is a variable or control on the form holding the value you wish to filter for. if PClass is not a string value, just remove the inside single quotes.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Thank you - that seems to work - it did create another issue thought

Since i had the tableadapters in the select i had a field that was calculated to tell me how many employees there were
lblTotEmpCount.Text = (Me.HYLTDDataSet.CPY10100.Rows.Count).ToString

But since it loaded the whole table it is giving the total for the table and I'd like the total for the division or department. How do I ask for that now?

Thanks for the help
Kim
 
You are looking at the dataset for your record count. Instead, now you should be looking at the count of the items in the combobox....

lblTotEmpCount.Text = Me.Combobox1.Items.Count.ToString

That should do the trick because the combobox should only have a count of the records currently in it.....

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB.NET Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top