views:

1319

answers:

4

I am using a System.Windows.Forms.Combobox bound to Category table as search criteria. I need to have a value “All” or empty string that will be selected when user does not want to use this criteria. Since combo is bound, each time it is clicked, value added by combo1.Text = “All” is erased. Obviously, I can’t add “All” category to database. What is the best way to accomplish this?

+1  A: 

You would have to add another item:

ComboBox1.Items.Insert(0,"All");
Konstantinos
I am getting an error: Items collection cannot be modified when the DataSource property is set.
Dan
i was afraid that would happen.. if you set your items directly from a dataset to the datasource its locking the Items property.. One possible solution would be to iterate throught your collection of items in your dataset and add them via Items.Insert
Konstantinos
I tried populating manually, using ComboBox1.Items.Insert(0,"All"), but then I receive SelectedValue as Nothing. I can cast Item back to original object but this is a lot of code for something that simple...
Dan
+1  A: 

Either manually add the All entry to the bound dataset after the other values have been loaded, or unbind the combo and instead iterate through the data to populate it. It's just a search combo, so you don't really need all the benefits of binding.

CodeByMoonlight
I tried populating manually, using ComboBox1.Items.Insert(0,"All"), but then I am receiving null SelectedValue.
Dan
That's why i said add it to the dataset rather than the combobox.
CodeByMoonlight
A: 
  1. If you are using sql server then you can add extra value to the select command for the All option
  2. Or you can add All option by using comboBox1.Items.Insert(0,"All") to add new item as zero index. after binding the control

Hope that will help.

Asim Sajjad
In second option, this means I can't use the designer to set binding options? I have placed the ComboBox1.Items.Insert(0,"All") in Form Load but I am getting the "Items collection cannot be modified when the DataSource property is set" error.
Dan
I think you should use the first option, when you select the item then you can add the all option there, Adding new items is available in the web dropdownlist control.
Asim Sajjad
A: 

I always 'manually' add an item to the data source before data binding. This avoids having 'artificial' data in your data source's query, so if you use the query for something other than a dropdown, you only get real data.

ProfK