472,964 Members | 2,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,964 software developers and data experts.

Bound ComboBox for browsing _and_ editing?

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
0 1965

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: Phil | last post by:
My form uses a dataset containing two tables, a base table (Contact) and a lookup table (Insurer). My combobox is bound as follows: DataSource = datasetContact DisplayMember =...
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate...
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
by: jjsavage | last post by:
Hi all, I've got a DataGridView bound to a DataTable, and I need to make some of the columns into ComboBoxColumns. I've tried some of the solutions listed here, but I can't seem to make them...
by: njb35 | last post by:
Hi all I'm beginning my foray from VBA into VB 2005 Express, and enjoying some of the efficiencies it provides! I'm stuck with some dataset handling however that I _think_ can be automated but...
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.