Adding blank row to Combo Box

H

Hemang Shah

Hello

I'm sure others have asked this before, now its my turn!

Combo box with "DropDownStyle" is set as "DropDownList"

Once a user selects a value, how can they select null again ?

Or if there are 4 combo boxes on the form, once someone selects the first
time and saves the data, all the combo boxes, first item is selected by
default.

I want to:
1) Give the user the option to select a blank value
2) Not create the illusion of wrong data being selected.

I guess if I could add a blank row on top of the combo box it would fix my
situation, how ever some code I tried gives an exception:

cmbPrevOccupation.Items.Insert( 0, " ");

Exception:

Cannot modify the items collection when the datasource property is set



I guess I have to add the value to the dataview itself ?



Any help would be appreciated



thanks



HS
 
W

William \(Bill\) Vaughn

I've done this by executing a UNION with the SQL that populates the list
SELECT State, StateCode FROM ValidStates
UNION '<None>', '??'
ORDER BY State
This returns a rowset that includes the "none" item. It sorts to the top so
it appears first in the list.

hth

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
G

Guest

The ComboBox, as is, doesn't handle a lot of stuff easily. I have a
sub-classed ComboBox (in fact, I recommend sub-classing all the UI controls
and only use sub-classes in your Forms). Anyway, you can handle this sort of
behavior in your Combo sub-class by a KeyDown event handler that sets
this.SelectedIndex = -1 when the Delete key is pressed.

Also, I think that if you DataBind the SelectedValue of your Combos to a
column in a DataSet, you should avoid the problem of the combos getting
re-set.

I don't recommend DataBinding Combos to a Property, as I've discovered what
I think is a bug when the user hits the Delete key and then tries to tab out
of the Combo (they can't) ... I'm not sure if that's just because of the way
I've implemented some things in my sub-class, but I've not been able to find
a workaround, so I just avoid DataBinding Combos to Properties.

~~Bonnie
 
H

Hemang Shah

My Data Source is a dataView

I know Bill has an excellent article on adding columns to a dataview.

But what happens if the user selects <none> and saves it?

Right now I got away with adding a blank item to the lookup table. Although
I know it is not the best way to do it, it works for now.

Any further insight would be helfful.

Thanks
 
G

Guest

Hernang,
I know Bill has an excellent article on adding columns to a dataview.

Well, that may be so, but I'm not sure what that has to do with this
problem. You don't need to add any columns.
But what happens if the user selects <none> and saves it?

That was why I didn't recommend that approach ... Bill did, not me. You
need to wipe-out the value in the Combo if the user doesn't want it. That's
why I suggested setting the .SelectedIndex to -1 when the user presses the
Delete key to clear out the Combo. It's a bit more complicated than that, but
that's the place to start ... as I said, you need sub-class your ComboBox
first, and then go from there.

BTW ... you *are* talking about WinForms here and not WebForms, right?

~~Bonnie
 
B

Beth Massi [Architect MVP]

Hemang Shah said:
My Data Source is a dataView

I know Bill has an excellent article on adding columns to a dataview.

But what happens if the user selects <none> and saves it?

Well that depends on the way you've set up your databinding. If you have a
lookup table something like this:

Lookup Table:
Columns: ID, Descrip, Abbrev
Row 0 : 1, "Name1", "Value1"
Row 1 : 2, "Name2", "Value2"
Row 2 : null , "<none>", null

(You can add the "blank" row either from a UNION or just add the row
directly to the DataTable.)

Then set up the databinding something like this:

Combobox1.Datasource = MyLookupView
Combobox1.DisplayMember = "Descrip"

'- Then depending on what you are storing in the table, when they select the
combo you could use the ID,
Combobox1.ValueMember = "ID"
' or the abbreviation as the value of the combobox.
Combobox1.ValueMember = "Abbrev"

'-- Now set up the databinding to the field in the dataset you are editing:
Combobox1.Databindings.Add("SelectedValue", MyDataset, "MyTable.MyField")

That means that when the value is selected in the Combobox, put that into
MyTable.MyField. (That column must allow nulls). Don't forget to call
EndCurrentEdit on the Currency Manager at some point so that the value is
written to the dataset. You can do that from a SelectedIndexChanged event
handler.

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.BindingContext(MyDataset, "MyTable").EndCurrentEdit()
End Sub

This is not a bad way to do it at all.

HTH,
-B
Right now I got away with adding a blank item to the lookup table.
Although I know it is not the best way to do it, it works for now.

Any further insight would be helfful.

Thanks
"Bonnie Berent [C# MVP]" <[email protected]>
wrote in message
The ComboBox, as is, doesn't handle a lot of stuff easily. I have a
sub-classed ComboBox (in fact, I recommend sub-classing all the UI
controls
and only use sub-classes in your Forms). Anyway, you can handle this
sort of
behavior in your Combo sub-class by a KeyDown event handler that sets
this.SelectedIndex = -1 when the Delete key is pressed.

Also, I think that if you DataBind the SelectedValue of your Combos to a
column in a DataSet, you should avoid the problem of the combos getting
re-set.

I don't recommend DataBinding Combos to a Property, as I've discovered
what
I think is a bug when the user hits the Delete key and then tries to tab
out
of the Combo (they can't) ... I'm not sure if that's just because of the
way
I've implemented some things in my sub-class, but I've not been able to
find
a workaround, so I just avoid DataBinding Combos to Properties.

~~Bonnie
 
H

Hemang Shah

Hello Beth

That is what I ended up doing, Adding a blank row to the lookup table.

However my ID is also the primary key, so I cannot set it as blank.

So right now the database is actually storing that blank value!

Is there a way to create a DataView with a Union query ?

All my looksup are DataView from 1 lookup table with some criteria.
(e.g the filter in DV is type = sex, for male / female, another dataview
would have filter "Type = State, for all the states & so on).

So I would have to create a blank value for every critiria in the look up
table.
The union would work gr8, but I don't know how to set a union with the
DataView.

Any Ideas ?

Thanks

Hemang



Beth Massi said:
Hemang Shah said:
My Data Source is a dataView

I know Bill has an excellent article on adding columns to a dataview.

But what happens if the user selects <none> and saves it?

Well that depends on the way you've set up your databinding. If you have a
lookup table something like this:

Lookup Table:
Columns: ID, Descrip, Abbrev
Row 0 : 1, "Name1", "Value1"
Row 1 : 2, "Name2", "Value2"
Row 2 : null , "<none>", null

(You can add the "blank" row either from a UNION or just add the row
directly to the DataTable.)

Then set up the databinding something like this:

Combobox1.Datasource = MyLookupView
Combobox1.DisplayMember = "Descrip"

'- Then depending on what you are storing in the table, when they select
the combo you could use the ID,
Combobox1.ValueMember = "ID"
' or the abbreviation as the value of the combobox.
Combobox1.ValueMember = "Abbrev"

'-- Now set up the databinding to the field in the dataset you are
editing:
Combobox1.Databindings.Add("SelectedValue", MyDataset, "MyTable.MyField")

That means that when the value is selected in the Combobox, put that into
MyTable.MyField. (That column must allow nulls). Don't forget to call
EndCurrentEdit on the Currency Manager at some point so that the value is
written to the dataset. You can do that from a SelectedIndexChanged event
handler.

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Me.BindingContext(MyDataset, "MyTable").EndCurrentEdit()
End Sub

This is not a bad way to do it at all.

HTH,
-B
Right now I got away with adding a blank item to the lookup table.
Although I know it is not the best way to do it, it works for now.

Any further insight would be helfful.

Thanks
"Bonnie Berent [C# MVP]" <[email protected]>
wrote in message
The ComboBox, as is, doesn't handle a lot of stuff easily. I have a
sub-classed ComboBox (in fact, I recommend sub-classing all the UI
controls
and only use sub-classes in your Forms). Anyway, you can handle this
sort of
behavior in your Combo sub-class by a KeyDown event handler that sets
this.SelectedIndex = -1 when the Delete key is pressed.

Also, I think that if you DataBind the SelectedValue of your Combos to a
column in a DataSet, you should avoid the problem of the combos getting
re-set.

I don't recommend DataBinding Combos to a Property, as I've discovered
what
I think is a bug when the user hits the Delete key and then tries to tab
out
of the Combo (they can't) ... I'm not sure if that's just because of the
way
I've implemented some things in my sub-class, but I've not been able to
find
a workaround, so I just avoid DataBinding Combos to Properties.

~~Bonnie



:

Hello

I'm sure others have asked this before, now its my turn!

Combo box with "DropDownStyle" is set as "DropDownList"

Once a user selects a value, how can they select null again ?

Or if there are 4 combo boxes on the form, once someone selects the
first
time and saves the data, all the combo boxes, first item is selected by
default.

I want to:
1) Give the user the option to select a blank value
2) Not create the illusion of wrong data being selected.

I guess if I could add a blank row on top of the combo box it would fix
my
situation, how ever some code I tried gives an exception:

cmbPrevOccupation.Items.Insert( 0, " ");

Exception:

Cannot modify the items collection when the datasource property is set



I guess I have to add the value to the dataview itself ?



Any help would be appreciated



thanks



HS
 
G

Guest

I still think it would have been better to do as I suggested and handle the
Delete key in the Combo sub-class, but if you still want to do it by adding a
blank row to your table, then you can overcome your problem (having to create
a blank entry for every criteria in your view), by including the blank row in
your View's RowFilter. IOW, for your RowFilter = "Type = State OR ID = 0" or
whatever your blank row value is. Then you only need one blank row.

~~Bonnie
 
H

Hemang Shah

Bonnie

to be honest I did not understand that del key thing.

I'm not deleting anything in the combo box.

All I want is that when the form appears, the box should have a blank item.

Because when the form is bound, if you update one item and save, all the
combo boxes populates the first item.

So where does the del key come in? The customer never touches the del key.

sorry if its a stupid question.
 

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