473,404 Members | 2,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 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 1984

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

Similar topics

2
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 =...
19
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...
3
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...
3
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...
5
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...
1
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. -- .....
4
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...
5
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.