By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,833 Members | 2,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,833 IT Pros & Developers. It's quick & easy.

comboBox binding question(with different DisplayMember & ValueMemb

P: n/a
Hi, there,

I meet a problem about comboBox binding.

--------------------
Database: Northwind
Tables: 1) Products 2) Categories
I create a form (named "form1") to edit the record from Products table.

dsProducts is a DataSet object to storage the data from Products and
Categories tables, and the Foreign Reference relationship between
Products(CategoryID) and Categories(CategoryID).
i.e.
dsProducts.Tables("products") is the data of table "Products"
dsProducts.Tables("categories") is the data of table "Categories"
The relationship name of the two tables is "product_category"

There is a comboBox named "cboCategories", binding to Categories table, and
set the DisplayMember as "CategoryName", ValueMember as "CategoryID". The
binding will be finished when the form1 is loaded.

\\\\\
'........ other codes

Private Sub form1_load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
..... ' others codes
With cboCategories
.DisplayMember = "CategoryName"
.ValueMember = "CategoryID"
.DataSource = Me.dsUser_Roles.Tables("categoryies")
End With
..... ' others codes
Set_binding()
..... ' others codes

End sub

'........ other codes

Private Sub Set_binding()
' TextBox binding: txtProductName <-> ProductName field of Products table
txtProductName.DataBindings.Add(New Binding("Text", dsProducts,
"products.ProductName"))

' TextBox binding: txtQuantityPerUnit<-> QuantityPerUnit field of
Products table
txtQuantityPerUnit.DataBindings.Add(New Binding("Text", dsProducts,
"products.QuantityPerUnit"))

' TextBox binding: txtUnitPrice <-> UnitPrice field of Products table
txtUnitPrice.DataBindings.Add(New Binding("Text", dsProducts,
"products.UnitPrice"))
' cboCategories binding: cboCategories <-> CategoryID field of Products
table
cboCategories.DataBindings.Add(New Binding("Text", dsProducts,
"products.CategoryID"))

End Sub

'........ other codes

\\\\\\\\\

When I navigate the data in products, a new item with the CategoryID value
from Products table is added, not the CategoryName. I want to show the
related CategoryName other than CategoryID when navigating the data in
Products table.

How to solve the problem?

Thanks a lot.

Bruce
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a

Hello Bruce,

Try binding to the SelectedValue property instead of Text:

| cboCategories.DataBindings.Add(New Binding("SelectedValue", dsProducts,
| "products.CategoryID"))

hope that helps

Steve Stein
VB Team

This posting is provided "AS IS" with no warranties and confers no rights.

--------------------
| Thread-Topic: comboBox binding question(with different DisplayMember &
ValueMemb
| thread-index: AcTwPS9cTv2vuhERRwWfBWyk+RjYIg==
| X-WBNR-Posting-Host: 64.229.237.74
| From: "=?Utf-8?B?QnJ1Y2U=?=" <Br***@discussions.microsoft.com>
| Subject: comboBox binding question(with different DisplayMember &
ValueMemb
| Date: Sat, 1 Jan 2005 12:05:05 -0800
| Lines: 80
| Message-ID: <B6**********************************@microsoft.co m>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="Utf-8"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Content-Class: urn:content-classes:message
| Importance: normal
| Priority: normal
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.1.29
| Path: cpmsftngxa10.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: cpmsftngxa10.phx.gbl microsoft.public.dotnet.languages.vb:251510
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| Hi, there,
|
| I meet a problem about comboBox binding.
|
| --------------------
| Database: Northwind
| Tables: 1) Products 2) Categories
|
|
| I create a form (named "form1") to edit the record from Products table.
|
| dsProducts is a DataSet object to storage the data from Products and
| Categories tables, and the Foreign Reference relationship between
| Products(CategoryID) and Categories(CategoryID).
| i.e.
| dsProducts.Tables("products") is the data of table "Products"
| dsProducts.Tables("categories") is the data of table "Categories"
| The relationship name of the two tables is "product_category"
|
| There is a comboBox named "cboCategories", binding to Categories table,
and
| set the DisplayMember as "CategoryName", ValueMember as "CategoryID". The
| binding will be finished when the form1 is loaded.
|
| \\\\\
| '........ other codes
|
| Private Sub form1_load(ByVal sender As System.Object, ByVal e As
| System.EventArgs) Handles MyBase.Load
| ..... ' others codes
| With cboCategories
| .DisplayMember = "CategoryName"
| .ValueMember = "CategoryID"
| .DataSource = Me.dsUser_Roles.Tables("categoryies")
| End With
| ..... ' others codes
| Set_binding()
| ..... ' others codes
|
| End sub
|
| '........ other codes
|
| Private Sub Set_binding()
| ' TextBox binding: txtProductName <-> ProductName field of Products
table
| txtProductName.DataBindings.Add(New Binding("Text", dsProducts,
| "products.ProductName"))
|
| ' TextBox binding: txtQuantityPerUnit<-> QuantityPerUnit field of
| Products table
| txtQuantityPerUnit.DataBindings.Add(New Binding("Text", dsProducts,
| "products.QuantityPerUnit"))
|
| ' TextBox binding: txtUnitPrice <-> UnitPrice field of Products table
| txtUnitPrice.DataBindings.Add(New Binding("Text", dsProducts,
| "products.UnitPrice"))
|
|
| ' cboCategories binding: cboCategories <-> CategoryID field of Products
| table
| cboCategories.DataBindings.Add(New Binding("Text", dsProducts,
| "products.CategoryID"))
|
| End Sub
|
| '........ other codes
|
| \\\\\\\\\
|
| When I navigate the data in products, a new item with the CategoryID
value
| from Products table is added, not the CategoryName. I want to show the
| related CategoryName other than CategoryID when navigating the data in
| Products table.
|
| How to solve the problem?
|
| Thanks a lot.
|
| Bruce
|
|
|

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.