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

J

Johann Blake

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
 
C

Christof Nordiek

Johann Blake said:
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
 
J

Johann Blake

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
 
J

Jeremy Williams

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
 
J

Jeremy Williams

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.
 
J

Johann Blake

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top