473,714 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.RowSta teFilter = 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 SelectionChange Committed events. My first
attempt forcibly altered the Text property:

private void cbx1_SelectionC hangeCommitted( object sender,
System.EventArg s 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_SelectionC hangeCommitted( object sender,
System.EventArg s e) {
// Chose a new velue?
ComboBox cbx;
cbx = sender as ComboBox;
cbx.Text = cbx.SelectedVal ue.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.SelectedVal ue lacks a value.

SelectedIndex: 1
selectedIndex: -1
SelectedItem: "Option 2" (second string from list)
selectedValueCh angedFired: 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.Sel ectedValueChang ed 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 2015

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

Similar topics

2
3447
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 = Insurer.InsurerName ValueMember = Insurer.InsurerId DataBindings.SelectedValue = Contact.InsurerId It generally works fine. I am able to navigate records and
19
4106
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 the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
3
4270
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 several examples on the web. They all seem to have problems, though that I've been unable to resolve. The most promising example I have found is at:
3
3027
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 download a fully working C# sample with the Northwind.mdb here: www.insightgis.com.au/web/stuff/DataGridCombo.zip
5
4661
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 combo by allowing the user to dynamically type in the new value directly in the combo box whcih in turn updates the lookup table and then when they have finished leaves that particular cell on the grid with the correct value. Seems...
1
2367
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. -- .. http://sf-f.org, weblog and search engine for fans and writers of
4
4616
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 bound to a table as a lookup, drawing values from another table to populate the available selections. This all worked fine in VB6. I have distilled the problem down to a simple form drawing data from the Northwind database for a representative...
5
2054
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 work. They might not be the solutions I'm looking for; I just want to have a few arbitrary values for a combo box and make sure everything in that row is one of those values. My boss won't let me just leave them as text boxes. How do I tell the...
5
2594
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 I can probably code what I want to do the hard way. I've searching around online but can't find an answer to this specific question. Here's the situation: I have a dataset table with 3 fields: one indexed as a primary key and the other two...
0
8798
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8706
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9311
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9171
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7947
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5946
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4719
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3156
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.