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