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
2009-09-20 00:30:17
I am getting an error: Items collection cannot be modified when the DataSource property is set.
Dan
2009-09-20 00:36:29
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
2009-09-20 01:02:42
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
2009-09-20 01:33:53
+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
2009-09-20 00:30:57
I tried populating manually, using ComboBox1.Items.Insert(0,"All"), but then I am receiving null SelectedValue.
Dan
2009-09-20 00:42:11
That's why i said add it to the dataset rather than the combobox.
CodeByMoonlight
2009-09-20 02:38:47
A:
- If you are using sql server then you can add extra value to the select command for the All option
- 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
2009-09-20 00:31:14
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
2009-09-20 00:43:33
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
2009-09-20 01:17:06
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
2009-09-20 12:36:08