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

Bound ComboBox for browsing _and_ editing?

P: n/a

Can I use a bound ComboBox for both browsing and editing?

I'm working on a small, standalone database application using Visual
C#.NET 2003 and an Access data file.

In order to keep the number of different screens down to a minimum,
I'm trying to use the same Windows Forms for both browsing and for
updating. This works fine for TextBoxes, but I'm running into
problems with my DropDownLists (ComboBoxes).

Thre's nothing special about the (string) lists in these ComboBoxes.
They're set up with DisplayMember="" and ValueMember="", they're
defined inside the VS Designer, and they're a convenience to save
the user from having to remember what the valid choices are; the
choice, once made, would simply be stored into a text field... er,
column. Or, at least, that's how I thought/hoped it would work.

Trouble is, I can't figure out how to bind these ComboBoxes in a way
that lets the user see the existing values as they browse with the
bound TextBoxes and ComboBoxes disabled (Enabled=false), yet allows
editing when these same controls are enabled.

Binding my DataView to the ComboBoxs' "Text" property is tempting.
It works very nicely for browsing; the current contents of the data
columns are displayed properly as the user "scrolls" through the
rows. When the control is enabled (for editing), the DropDownList
displays properly, a choice can be made, and all appears well...
until I attempt to update the database. According to the debugger,
the controls _are_ bound, and a changed ComboBox _does_ have new
(changed) contents in its Text property, but the supposedly bound
data column never gets updated (again, per the debugger's display of
my DataView with DataView.RowStateFilter = CurrentRows). (TextBoxes
bound to the same DataView are working just fine.)

Then there's binding to the ComboBox's SelectedValue Property (or
possibly its SelectedItem -- even after reading the online
documentation I'm not really sure what the difference is between
these two). I tried this, but I lose the "browsing" feature -- when
the ComboBox is disabled all that shows is either blanks or the last
choice made when the control _was_ enabled.

Late this afternoon, after spending another couple of hours with
Google trying to find the keywords for a magical incantation that
would take me directly to an explanation of this problem and a
workaround -- and failing -- I tried something only moderately
perverted: I bound the ComboBoxes' Text property and installed an
event handler for their SelectionChangeCommitted events. My first
attempt forcibly altered the Text property:

private void cbx1_SelectionChangeCommitted(object sender,
System.EventArgs e) {
// Warning: reconstructed from memory
ComboBox cbx = sender as ComboBox;
string s = cbx.Text;
cbx.Text = "";
cbx.Text = s;
}

Then I tried something that _seemed_ a little less klugy:

private void cbx1_SelectionChangeCommitted(object sender,
System.EventArgs e) {
// Chose a new velue?
ComboBox cbx;
cbx = sender as ComboBox;
cbx.Text = cbx.SelectedValue.ToString();
}

This one works... mostly. It occasionally throws a "null reference"
exception complaining that, in spite of the fact that the code is in
an event handler set up for the case that a selection has been made,
cbx.SelectedValue lacks a value.

SelectedIndex: 1
selectedIndex: -1
SelectedItem: "Option 2" (second string from list)
selectedValueChangedFired: true

and the embedded ListControl has:

SelectedIndex = 1
SelectedValue = <undefined value(a.k.a. "Ka-boom!")
I can't be the first person to ever try setting up a form with
ComboBoxes to browse and edit a database. I keep feeling that I'm
missing something simple, something that's so obvious to everyone
that no-one bothers mentioning it.

I did run across a note about VisualBasic ComboBoxes in the VS
online documentation that might be relevant entitled:

Data binding for ComboBox or ListBox is read-only

but (a) it's not clear whether or not this note applies to VC#, and
(b) the note's rather terse workaround merely says to

Add code to the ListControl.SelectedValueChanged event for the
ComboBox or ListBox controls to update the database.

without providing any specifics.

It's very frustrating: with the VS debugger I can see the ComboBox
selection being stored into its Text property, but I can't seem to
home in on where the ball gets dropped, that is, what stops that
value from being stuffed into the DataView's current DataRow.

Has anyone out there been down this road before? I'm hoping someone
familiar with binding and ComboBoxes can either tell me that I'm
going at the problem from a completely wrong direction, or possibly
that my horribly ugly code is about the only way to accomplish what
I'm trying to do.

Any hints, suggestions, or clues will be appreciated.
Frank McKenney
--
A science is any discipline in which a fool of this generation can
go beyond the point reached by a genius of the last generation.
-- Max Gluckman
--
Nov 15 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.