ComboBox data binding woes

D

Dany P. Wu

Hi everyone,
As usual, weekend is tinkering time for students and I'm playing with
combobox databinding for the first time. Previously I have always iterated
through the records and added each item in the rows manually. Just thought
it would be nice to do it properly for a change.

Here's the bit of code I experiment with:
============================================================================
========================
Private Sub Create_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
txtResDate.Text = Date.Today
Dim command1 As New OleDb.OleDbCommand("select code, location from
airport where code in (select
distinct orig from flight)", con)
Dim daOrig As New OleDb.OleDbDataAdapter(command1)
Dim dest As New DataSet
Dim orig As New DataSet
Try
con.Open()
orig.Clear()
daOrig.Fill(orig)
con.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try

With cboOrig
.DataSource = orig.Tables(0).DefaultView
.DisplayMember = "Location"
'.ValueMember = "Code"
End With
End Sub
===========================================================================
It seemed to only partially work. The combobox had the correct number of
items but each item was displayed as "System.Data.DataRowView", instead of
the actual value.

I also had to comment out the setting of the ValueMember property as I got
the error "Could not bind to the new display member." I'm a bit stumped as
to why this doesn't work. I've scrounged around the net for examples, etc.
but they generally point to this type of code. Did I miss something?

Any suggestions would be greatly appreciated.

Cheers,
Dany.
 
K

Ken Tucker [MVP]

Hi,

The displaymember is case sensitive. Try this instead.

With cboOrig
.DataSource = orig.Tables(0).DefaultView
.DisplayMember = "location"
.ValueMember = "code"
End With


Ken
 
D

Dany P. Wu

In
Ken Tucker said:
Hi,

The displaymember is case sensitive. Try this instead.

With cboOrig
.DataSource = orig.Tables(0).DefaultView
.DisplayMember = "location"
.ValueMember = "code"
End With

Thanks for the suggestion Ken. Unfortunately that didn't quite do the trick.

I still get the same error when I ran your suggested code - namely "Could
not bind to the new display member." at the ValueMember line.

When I commented out the setting of the ValueMember, the combobox was
populated with 5 items, which correctly corresponded to the number of rows
that should be returned. Unfortunately each item still display the same
thing, i.e. "System.Data.DataRowView".

This is really puzzling me - I really can't think of why such a simple bit
of code wouldn't work. Any other suggestions?

Cheers,
Dany.
 
W

William Ryan eMVP

Dany:

Just for giggles, set location as the ValueMember too (so it's set for both)
and see if it 'works'. Everythign else here looks fine and I'm wondering if
the word code isn't causing the issue. If it is, changing the alias to
somehting else should fix it but before you do that, see if it will work
with location.

Also, it appears that you have two datasets and one isn't being used. It's
a rare scenario where you need two datasets in one app but as an aside, you
may want to get rid of code you aren't using (forgive my pickyness, I've
been refactoring stuff all day and it sticks out like a sort thumb). Let me
knonw what happens with Valuemember.

Cheers,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
D

Dany P. Wu

In
William Ryan eMVP said:
Dany:

Just for giggles, set location as the ValueMember too (so it's set
for both) and see if it 'works'. Everythign else here looks fine and
I'm wondering if the word code isn't causing the issue. If it is,
changing the alias to somehting else should fix it but before you do
that, see if it will work with location.

Thanks for the suggestion, Bill. Not much of a giggle though :blush:)
Unfortunately everything was the same - same errors, etc. I had to comment
out the ValueMember as it gave the same error as I posted originally.
Also, it appears that you have two datasets and one isn't being used.
It's a rare scenario where you need two datasets in one app but as an
aside, you may want to get rid of code you aren't using (forgive my
pickyness, I've been refactoring stuff all day and it sticks out like
a sort thumb). Let me knonw what happens with Valuemember.

Yup! The original code had two datasets, each one providing data for two
comboboxes. I didn't include both in the posting because it's more or less
identical to the each other.

Cheers,
Dany.
 
T

t14385

try puting
..ValueMember()
before
..DataSource =

and remember
..DisplayMember is case sensitiv


-
t1438
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top