By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,696 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,696 IT Pros & Developers. It's quick & easy.

How do I add a blank item to a combobox that is databound to a dataset?

P: n/a
I fill a table in a dataset with values that will be used by a combobox
for the combobox's items. The combobox is a drop down list that only
allows the user to select from the list but not enter any new values.
What I need to do is include a blank row in the list so that when it is
selected, the SelectedValue property returns a Null. I don't want to
add a record in the database with some field set to null because this
would have to be repeated for several languages since the values in the
list are language dependent. How can I add a blank item since the
combobox is bound to the dataset?

Thanks for your insight.
Johann Blake

Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Johann Blake" <jo*********@yahoo.com> schrieb im Newsbeitrag
news:11**********************@g43g2000cwa.googlegr oups.com...
I fill a table in a dataset with values that will be used by a combobox
for the combobox's items. The combobox is a drop down list that only
allows the user to select from the list but not enter any new values.
What I need to do is include a blank row in the list so that when it is
selected, the SelectedValue property returns a Null. I don't want to
add a record in the database with some field set to null because this
would have to be repeated for several languages since the values in the
list are language dependent. How can I add a blank item since the
combobox is bound to the dataset?

Thanks for your insight.
Johann Blake

You can add an additional row to the DataSet after filling it from the
database.

hth
Christof
Nov 17 '05 #2

P: n/a
The problem with that solution is that when the user selects a
different language (with a separate combobox), the list for the
combobox that is suppose to include a blank line will not include the
blank line. This is because this combobox is bound to a table whose
RowFilter is set to the language the user chooses. This would mean
having to add a blank row for each language selected. Any other ideas?

Johann

Nov 17 '05 #3

P: n/a
If you want to continue to use the DataView as your data source, then no,
there is not going to be a more elegant solution. You can either databind
the combo or manually populate the combo, not both. By using databinding to
populate the combo, you are ensuring that the combo cannot get it's data any
other way. Since you are using a DataView as the data source, the data
*must* be in the underlying table. So your options are fairly limited:

1) Manually populate the combo each time a user selects a different
language, which would allow you to add a blank row in code. You could
iterate through the rows in the DataView after adding the blank row.
2) Continue to bind the combo to the DataView, and add a blank row to the
underlying DataTable for each language.

If you just want to ensure that no item is selected when the combo is
populated, you can just set the SelectedIndex property to -1. However, I am
guessing you need the blank row to allow the user to "clear" the selection,
so setting the SelectedIndex probably won't help.

How are you using the blank row? If it is just to ensure that nothing is
selected when
"Johann Blake" <jo*********@yahoo.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
The problem with that solution is that when the user selects a
different language (with a separate combobox), the list for the
combobox that is suppose to include a blank line will not include the
blank line. This is because this combobox is bound to a table whose
RowFilter is set to the language the user chooses. This would mean
having to add a blank row for each language selected. Any other ideas?

Johann

Nov 17 '05 #4

P: n/a
"Johann Blake" <jo*********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
How can I add a blank item since the combobox is bound to the dataset?

After the data is bound, can't you just run

<comboBox>.Items.Insert(0, "");
Nov 17 '05 #5

P: n/a
Did you try this yourself? I think you will find it does not work (it
definitely does not work on any of the machines to which I have access). You
will get the following error:

"An unhandled exception of type 'System.ArgumentException' occurred in
system.windows.forms.dll
Additional information: Cannot modify the Items collection when the
DataSource property is set."

And if you try to Insert (or Add) to the item collection before setting the
data source, your work is undone once the data source is set.

"Mark Rae" <ma**@mark-N-O-S-P-A-M-rae.co.uk> wrote in message
news:e0**************@TK2MSFTNGP14.phx.gbl...
"Johann Blake" <jo*********@yahoo.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
How can I add a blank item since the combobox is bound to the dataset?

After the data is bound, can't you just run

<comboBox>.Items.Insert(0, "");

Nov 17 '05 #6

P: n/a
I decided to add a single row to the DataView and leave its TextID set
to null. However, to avoid adding an additional record for each
language, I simply set the RowFilter to include the null record:

myDataView.RowFilter = "LanguageID = " + id + " Or (TextID is null)";

Johann

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.