Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Bound Combo Box to Lookup Table

Status
Not open for further replies.

jgoodman00

Programmer
Joined
Jan 23, 2001
Messages
1,510
I am trying to implement bound combo boxes for a data form.

I have created a dataset with the required data & lookup tables. I have created the required relationships.

I then added a combo box to my form:
For DataSource I specified my lookup table.
For DisplayMember I specified the 'text' column for my lookup table.
For ValueMember I specied the ID column for my lookup table.
Under data-bindings, SelectedValue I specified the datacolumn in my main table.

N.B. The form was created using the data-form wizard.

Now, when I load the form it is bound correctly, & if a value is present it displays correctly, changing as records are navigated. However my problem occurs when I attempt to update a value:
The value entered updates correctly & is inserted into the underlying table. However, when I navigate to the next record, if no value is present, the combo seems to default to the first entry in the list, rather than being null. Also, if I remove the value for a record & then update (using UpdateDataSet), the combo will again default to the first value in the list rather than null.


Any suggestions?

James Goodman MCSE, MCDBA
 
Have been looking into this in more detail:

The following is the code the wizard automatically produced for LoadDataSet:
Public Sub LoadDataSet()
'Create a new dataset to hold the records returned from the call to FillDataSet.
'A temporary dataset is used because filling the existing dataset would
'require the databindings to be rebound.
Dim objDataSetTemp As NWDB.tblReleve
objDataSetTemp = New NWDB.tblReleve()
Try
'Attempt to fill the temporary dataset.
Me.FillDataSet(objDataSetTemp)
Catch eFillDataSet As System.Exception
'Add your error handling code here.
Throw eFillDataSet
End Try
Try
'Empty the old records from the dataset.
objtblReleve.Clear()
'Merge the records into the main dataset.
objtblReleve.Merge(objDataSetTemp)
Catch eLoadMerge As System.Exception
'Add your error handling code here.
Throw eLoadMerge
End Try


I added a watch to the combo box in question (I only have one at present but it applies to all), & it is the objtblReleve.Merge(objDataSetTemp) which seems to cause the problem. At this point, the combo box goes from nothing to the first value in the list (1).


James Goodman MCSE, MCDBA
 
Ok this is not limited to the merge method.

I followed the example for a bound combo box from msdn (
This uses the Northwind DB.

If you set the value of CustomerID to Null for a record, & then load the form it demonstrates this phenomenon. If the modified record is loaded first, the combo will be blank. However, if the user navigates to the next (or previous) record, & then back to the record, the combo will show the first entry in the list.


The completed example can be found here:


James Goodman MCSE, MCDBA
 
My woes have continued with another problem I noticed today:

I have a bound combo using a lookup table which is behaving as expected with the exception of the first record.

In the list of rows for my combo, the first is '.'. This is because of another issue I had with a bound combo box, whereby nulls were populated with values (A known bug with MS).

If my first record has a value other than period, when the user first loads the form it is displayed as a period, & subsequently my dataset is marked as HavingChanges (another problem which has now become clear).

This is somewhat frustrating, as the application is complete bar this somewhat significant problem.

For information, under data-bindings, my SelectedValue is set to my master table, & the correct column, which should subsequently display the correct record.

Any suggestions?

James Goodman MCSE, MCDBA
 
Solved.

It would appear to be a problem with the time the binding is created. If you create the bindings at design time, then I believe they are created before the data-adapters are filled, & subsequently the wrong data is displayed. By creating the binding at runtime, after the data-adapters have been filled it seems to work...

James Goodman MCSE, MCDBA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top