Groups | Blog | Home
all groups > vb.net controls > may 2004 >

vb.net controls : ComboBox data binding woes


Ken Tucker [MVP]
5/29/2004 6:30:57 AM
Hi,

The displaymember is case sensitive. Try this instead.

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


Ken
----------------
[quoted text, click to view]

Dany P. Wu
5/29/2004 5:25:33 PM
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.

William Ryan eMVP
5/29/2004 6:14:41 PM
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/
[quoted text, click to view]

Dany P. Wu
5/29/2004 11:48:28 PM
In news:ulDMJgWREHA.2468@tk2msftngp13.phx.gbl,
Ken Tucker [MVP] <vb2ae@bellsouth.net> typed:
[quoted text, click to view]

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.

Dany P. Wu
5/30/2004 6:25:00 PM
In news:ef0lmpcREHA.556@TK2MSFTNGP10.phx.gbl,
William Ryan eMVP <dotnetguru@comcast.nospam.net> typed:
[quoted text, click to view]

Thanks for the suggestion, Bill. Not much of a giggle though :o)
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.

[quoted text, click to view]

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.

AddThis Social Bookmark Button