473,323 Members | 1,537 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,323 software developers and data experts.

Bound Combo Box Problems

I have what I think should be a simple question regarding a bound combo box.
My first table is Dealers:
DealerID - primary key
DealerName
BillToLocation

My second table is Locations:
LocationID - primary key
LocationDescription
DealerID - DealerID of the dealer this location belongs to

I want a bound combo box that when on a dealer record it displays the
LocationDescription for the dealer record's BillToLocation. But I want the
user to be able to change the BillToLocation to one of the dealers locations
in the Locations table.

At this point I have a combo box that shows only that Dealer's locations in
the drop down list, will update the BillToLocation when it's selected and
saved, but unfortunately it doesn't display correctly as I navigate through
the records using bmDealers.Position++ or bmDealers.Position--;
Interestingly enough, if I load a particular dealer record, it displays the
correct LocationDescription, but if navigate away and then back, it gets out
of sync.

Could someone give me some guidance as to what I'm doing wrong? Thank you.

Here is the binding code:
dsCoinTableAdapters.DealersTableAdapter daDealers = new
dsCoinTableAdapters.DealersTableAdapter();
dtDealers = ds.Dealers;
daDealers.Fill(dtDealers);

dsCoinTableAdapters.LocationsTableAdapter daLocations = new
dsCoinTableAdapters.LocationsTableAdapter();
dtLocations = ds.Locations;
daLocations.Fill(dtLocations);

DataColumn pdc = dtDealers.DealerIDColumn;
DataColumn cdc = dtLocations.DealerIDColumn;

DataRelation dr = new DataRelation("DealerToLocations", pdc, cdc);
ds.Relations.Add(dr);

bmDealers = this.BindingContext[ds, "Dealers"];

drDealer = dtDealers.FindByDealerID(nDealerID);
bmDealers.Position = dtDealers.Rows.IndexOf(drDealer);

txtDealerID.DataBindings.Add(new Binding("Text", ds,
"Dealers.DealerID"));
txtDealerName.DataBindings.Add(new Binding("Text", ds,
"Dealers.DealerName"));

cboBillToLocation.DataSource = ds;
cboBillToLocation.DisplayMember =
"Dealers.DealerToLocations.LocationDescription ";
cboBillToLocation.ValueMember =
"Dealers.DealerToLocations.LocationID";
cboBillToLocation.DataBindings.Add(new Binding("SelectedValue",
ds, "Dealers.BillToLocation"));

Nov 19 '06 #1
3 4180
Can anyone help here? It's pretty important and I can't find any examples
anywhere.

Thanks,

Bill

"Bill" wrote:
I have what I think should be a simple question regarding a bound combo box.
My first table is Dealers:
DealerID - primary key
DealerName
BillToLocation

My second table is Locations:
LocationID - primary key
LocationDescription
DealerID - DealerID of the dealer this location belongs to

I want a bound combo box that when on a dealer record it displays the
LocationDescription for the dealer record's BillToLocation. But I want the
user to be able to change the BillToLocation to one of the dealers locations
in the Locations table.

At this point I have a combo box that shows only that Dealer's locations in
the drop down list, will update the BillToLocation when it's selected and
saved, but unfortunately it doesn't display correctly as I navigate through
the records using bmDealers.Position++ or bmDealers.Position--;
Interestingly enough, if I load a particular dealer record, it displays the
correct LocationDescription, but if navigate away and then back, it gets out
of sync.

Could someone give me some guidance as to what I'm doing wrong? Thank you.

Here is the binding code:
dsCoinTableAdapters.DealersTableAdapter daDealers = new
dsCoinTableAdapters.DealersTableAdapter();
dtDealers = ds.Dealers;
daDealers.Fill(dtDealers);

dsCoinTableAdapters.LocationsTableAdapter daLocations = new
dsCoinTableAdapters.LocationsTableAdapter();
dtLocations = ds.Locations;
daLocations.Fill(dtLocations);

DataColumn pdc = dtDealers.DealerIDColumn;
DataColumn cdc = dtLocations.DealerIDColumn;

DataRelation dr = new DataRelation("DealerToLocations", pdc, cdc);
ds.Relations.Add(dr);

bmDealers = this.BindingContext[ds, "Dealers"];

drDealer = dtDealers.FindByDealerID(nDealerID);
bmDealers.Position = dtDealers.Rows.IndexOf(drDealer);

txtDealerID.DataBindings.Add(new Binding("Text", ds,
"Dealers.DealerID"));
txtDealerName.DataBindings.Add(new Binding("Text", ds,
"Dealers.DealerName"));

cboBillToLocation.DataSource = ds;
cboBillToLocation.DisplayMember =
"Dealers.DealerToLocations.LocationDescription ";
cboBillToLocation.ValueMember =
"Dealers.DealerToLocations.LocationID";
cboBillToLocation.DataBindings.Add(new Binding("SelectedValue",
ds, "Dealers.BillToLocation"));
Nov 20 '06 #2
Hi,

"Bill" <Bi**@discussions.microsoft.comwrote in message
news:97**********************************@microsof t.com...
>I have what I think should be a simple question regarding a bound combo
box.
My first table is Dealers:
DealerID - primary key
DealerName
BillToLocation

My second table is Locations:
LocationID - primary key
LocationDescription
DealerID - DealerID of the dealer this location belongs to

I want a bound combo box that when on a dealer record it displays the
LocationDescription for the dealer record's BillToLocation. But I want
the
user to be able to change the BillToLocation to one of the dealers
locations
in the Locations table.

At this point I have a combo box that shows only that Dealer's locations
in
the drop down list, will update the BillToLocation when it's selected and
saved, but unfortunately it doesn't display correctly as I navigate
through
the records using bmDealers.Position++ or bmDealers.Position--;
Interestingly enough, if I load a particular dealer record, it displays
the
correct LocationDescription, but if navigate away and then back, it gets
out
of sync.

Could someone give me some guidance as to what I'm doing wrong?
Problem :

The CurrencyManager (Dealers) maintains a position, when this position
changes then:
* the CurrencyManager will force all bindings to read value from datasource
to control,
* it will then fire CurrentItemChanged (among others), this event is handled
by:
- the RelatedCurrencyManager (Locations) and it will update the child
list.

So, SelectedValue is set *before* the child list is updated and updating a
child list causes a ListChanged(Reset) which makes the ComboBox always
select the first item. We need to get an event after the child list is
updated, then we can set SelectedValue.

This is possible by adding an eventhandler to
CurrencyManager.CurrentItemChanged after the RelatedCurrencyManager is
created (after binding to the child list).

See updated code:

CurrencyManager cmDealers;

private void FormLoad(...)
{
dsCoinTableAdapters.DealersTableAdapter daDealers = new
dsCoinTableAdapters.DealersTableAdapter();
dtDealers = ds.Dealers;
daDealers.Fill(dtDealers);

dsCoinTableAdapters.LocationsTableAdapter daLocations = new
dsCoinTableAdapters.LocationsTableAdapter();
dtLocations = ds.Locations;
daLocations.Fill(dtLocations);

DataColumn pdc = dtDealers.DealerIDColumn;
DataColumn cdc = dtLocations.DealerIDColumn;
ds.Relations.Add("DealerToLocations", pdc, cdc);

// bm is really a CurrencyManager
cmDealers = (CurrencyManager) this.BindingContext[ds, "Dealers"];

cmDealers.Position =
((IBindingList)cmDealers.List).Find(cmDealers.GetI temProperties().Find("DealerID",
false), nDealerID);

// binding parent list
txtDealerID.DataBindings.Add("Text", ds, "Dealers.DealerID");
txtDealerName.DataBindings.Add("Text", ds, "Dealers.DealerName"));

// binding child list
cboBillToLocation.DataSource = ds;
cboBillToLocation.DisplayMember =
"Dealers.DealerToLocations.LocationDescription ";
cboBillToLocation.ValueMember = "Dealers.DealerToLocations.LocationID";

cboBillToLocation.DataBindings.Add("SelectedValue" , ds,
"Dealers.BillToLocation");

// after binding the child list
cmDealers.CurrentItemChanged+= new EventHandler(
cmDealers_CurrentItemChanged );
}

private void cmDealers_CurrentItemChanged(object sender, EventArgs e)
{
// because we subscribed to this event *after binding to the child list*,
this
// will be invoked after the child list is updated, read the value from
source
cboBillToLocation.DataBindings["SelectedValue"].ReadValue();
}
HTH,
Greetings
Nov 21 '06 #3
Bart,

Thank you so much. This will be a big help. I had conceded to coding a
klunky workaround, so this will be much better.

Thanks again,

Bill

"Bart Mermuys" wrote:
Hi,

"Bill" <Bi**@discussions.microsoft.comwrote in message
news:97**********************************@microsof t.com...
I have what I think should be a simple question regarding a bound combo
box.
My first table is Dealers:
DealerID - primary key
DealerName
BillToLocation

My second table is Locations:
LocationID - primary key
LocationDescription
DealerID - DealerID of the dealer this location belongs to

I want a bound combo box that when on a dealer record it displays the
LocationDescription for the dealer record's BillToLocation. But I want
the
user to be able to change the BillToLocation to one of the dealers
locations
in the Locations table.

At this point I have a combo box that shows only that Dealer's locations
in
the drop down list, will update the BillToLocation when it's selected and
saved, but unfortunately it doesn't display correctly as I navigate
through
the records using bmDealers.Position++ or bmDealers.Position--;
Interestingly enough, if I load a particular dealer record, it displays
the
correct LocationDescription, but if navigate away and then back, it gets
out
of sync.

Could someone give me some guidance as to what I'm doing wrong?

Problem :

The CurrencyManager (Dealers) maintains a position, when this position
changes then:
* the CurrencyManager will force all bindings to read value from datasource
to control,
* it will then fire CurrentItemChanged (among others), this event is handled
by:
- the RelatedCurrencyManager (Locations) and it will update the child
list.

So, SelectedValue is set *before* the child list is updated and updating a
child list causes a ListChanged(Reset) which makes the ComboBox always
select the first item. We need to get an event after the child list is
updated, then we can set SelectedValue.

This is possible by adding an eventhandler to
CurrencyManager.CurrentItemChanged after the RelatedCurrencyManager is
created (after binding to the child list).

See updated code:

CurrencyManager cmDealers;

private void FormLoad(...)
{
dsCoinTableAdapters.DealersTableAdapter daDealers = new
dsCoinTableAdapters.DealersTableAdapter();
dtDealers = ds.Dealers;
daDealers.Fill(dtDealers);

dsCoinTableAdapters.LocationsTableAdapter daLocations = new
dsCoinTableAdapters.LocationsTableAdapter();
dtLocations = ds.Locations;
daLocations.Fill(dtLocations);

DataColumn pdc = dtDealers.DealerIDColumn;
DataColumn cdc = dtLocations.DealerIDColumn;
ds.Relations.Add("DealerToLocations", pdc, cdc);

// bm is really a CurrencyManager
cmDealers = (CurrencyManager) this.BindingContext[ds, "Dealers"];

cmDealers.Position =
((IBindingList)cmDealers.List).Find(cmDealers.GetI temProperties().Find("DealerID",
false), nDealerID);

// binding parent list
txtDealerID.DataBindings.Add("Text", ds, "Dealers.DealerID");
txtDealerName.DataBindings.Add("Text", ds, "Dealers.DealerName"));

// binding child list
cboBillToLocation.DataSource = ds;
cboBillToLocation.DisplayMember =
"Dealers.DealerToLocations.LocationDescription ";
cboBillToLocation.ValueMember = "Dealers.DealerToLocations.LocationID";

cboBillToLocation.DataBindings.Add("SelectedValue" , ds,
"Dealers.BillToLocation");

// after binding the child list
cmDealers.CurrentItemChanged+= new EventHandler(
cmDealers_CurrentItemChanged );
}

private void cmDealers_CurrentItemChanged(object sender, EventArgs e)
{
// because we subscribed to this event *after binding to the child list*,
this
// will be invoked after the child list is updated, read the value from
source
cboBillToLocation.DataBindings["SelectedValue"].ReadValue();
}
HTH,
Greetings
Nov 21 '06 #4

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

Similar topics

2
by: Daniel Tan | last post by:
Hi, how can i obtain the value of combobox multiple bound column data ? Eg, if i select combo box, it has more than one field, name and age and id listed. Pls advise, thanks. Regards, Daniel
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...
2
by: Ed via AccessMonster.com | last post by:
Hi all, What I am trying to do is get the value of the second column of a bound combo box in vba. example: Combo box: column 1-ID column 2-SIGN_CODE 1 R-1.gif 2 ...
3
by: Cindi Simonson | last post by:
Hi, I have a form with a combo box containing 4 columns of data. The form also contains 3 print buttons where the goal is to open 3 different reports according to the value in one of the...
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...
4
by: Evan | last post by:
I have a data bound combo box. The combo box displays several items (status code descriptions). After the combo box is bound, I want to set the combo box so that the "Draft" status is displayed by...
1
by: Bill | last post by:
Problem: Combo box data disappears from view when a requery is done See "Background" below for details on tables, forms & controls On a form, I want to use the setting of bound combo box C1...
9
by: Vmusic | last post by:
Hi, I'm using MS Access 2002. I have a form with a combo box built from a query that returns one column, and that one column is the bound column. How do you use VBA to programmatically change...
1
by: _JB_ | last post by:
Hi, I'm having problems changing the selected entry in a combo box when it's Row Source Type is Table/Query. I can do it easily when it's of Value List type by setting the Value property....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.