Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 20th, 2008, 10:03 AM
Registered User
 
Join Date: Mar 2008
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Combo Box Complex Binding Example?

I've been developing in Access for over a decade but am trying to make the switch to VB.NET (VS2005) and am having difficulty finding instructions on how to complex-bind a combo box to a dataset or data reader.

I want to bind the valuemember to an ID and the displaymember to a description. Can you help me? Thanks in advance!

Pigparent
Raleigh, North Carolina
USA
Reply With Quote
  #2 (permalink)  
Old June 5th, 2008, 07:27 AM
Authorized User
 
Join Date: May 2008
Location: BELFAST, , .
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Dont thank me this is straight out of a wrox book, Database for beginners i think not sure though i have a few of them


Complex Data Binding
For some controls (such as the TextBox and Label) binding the Text property is good enough. Other
controls, however, do not display a simple textual value.
For example, suppose that you have a database containing a Users table with fields FirstName, LastName,
and UserType. The UserTypes table has fields UserTypeId and UserTypeName. The Users.UserType field
contains a value that should match UserTypes.UserTypeId. The UserTypes.UserTypeName field contains
values such as Programmer, Project Manager, Department Manager, Program Manager, and Lab Director.
When you build a form to display the Users table data, you would like to use a ComboBox to allow the
user to select only the allowed choices Programmer, Project Manager, and so on. However, the Users
table doesn’t store those string values. Instead it stores the UserTypeId value corresponding to the
UserTypeName value that the user selects. When the user picks a UserTypes.UserTypeName value, the
ComboBox should look up the corresponding UserTypes.UserTypeId value and save it in the
Users.UserType field.
Clearly the simple binding strategy used for TextBoxes won’t work here. Binding this control requires two
rather complicated steps: defining the DataSet and binding the control. Each piece of the operation is easy,
but you must do everything correctly. If you miss any detail, the ComboBox won’t work, and Visual Basic’s
error messages probably won’t give you enough information to figure out how to fix the problem.
The first step is building a data connection. Select the Data menu’s Add New Data Source command. Use
the Data Source Configuration Wizard to make a data source that selects both the Users and UserTypes
tables from the database.
Next, create a ComboBox named cboUserType to the form. In the Properties window, select the control’s
DataSource property and click the drop-down arrow on the right. Select the DataSet’s UserTypes table
as shown in Figure 11-30. This tells the ComboBox where to look up values.
When you set this property, Visual Basic also adds a DataSet, BindingSource, and TableAdapter to
the form. These components provide access to the UserTypes table.
Set the ComboBox’s DisplayMember property to the field in the lookup table (specified by the DataSource
property) that the control will display to the user. In this example, the field is UserTypeName.
Set the ComboBox’s ValueMember property to the field in the lookup table that represents the value that
the ComboBox will need to read and write from the database. In this example, that’s the UserTypeId field.
Next, you need to bind the ComboBox to the field that it must read and write in the database. In this example,
that’s the Users table’s UserType field. To simplify this binding, add a new BindingSource to the
form. Change its name to “UsersBindingSource” and set its DataSource property to the
ComputerUsersDataSet as shown in Figure 11-31. Then set the BindingSource’s DataMember property
to the Users table.
356
Chapter 11
Figure 11-30: Set the ComboBox’s DataSource
property to the UserTypes table.
Figure 11-31: Set the BindingSource’s DataSource
to the ComputerUsersDataSet.
357
Database Controls and Objects
The last ComboBox property you need to set is SelectedValue. Click on the ComboBox, open the
Properties window, and expand the (DataBindings) entry at the top. Click the drop-down arrow to the
right of the SelectedValue property and select the field that the control must read and write in the
database. For this example, that’s the UsersBindingSource’s UserType field. Figure 11-32 shows the
Property window setting this property.
Figure 11-32: Set the BindingSource’s SelectedValue
to the UsersBindingSource’s UserType field.
Next, create TextBox controls to display the Users table’s FirstName and LastName fields. In the
Properties window, open their (Data Bindings) items and set their Text properties to the
UserBindingSource’s FirstName and LastName fields.
Finally, to give the user a way to navigate through the data, add a BindingNavigator to the form. Set
this component’s BindingSuorce property to UserBindingSource and the program is ready to run.
Figure 11-33 shows the program in action.
Figure 11-33: At run time, the ComboBox displays
the field bound to its DisplayMember property while
updating the field bound to its SelectedValue property.
358
Chapter 11
The choices allowed by the ComboBox are taken from the values in the UserTypes table’s UserTypeName
field. If you select a new user value from the ComboBox, the control automatically makes the appropriate
change to the Users table.






Reply With Quote
  #3 (permalink)  
Old June 11th, 2008, 12:08 PM
Registered User
 
Join Date: May 2008
Location: Dallas, TX, USA.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I think Chapter 20 in Programmer's Reference to Visual Basic 2008 has the examples your looking for.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding Column Name to a Combo Box. jitu ADO.NET 3 June 27th, 2013 07:35 AM
Combo box to display items from parent combo box Gini Visual Studio 2008 0 June 18th, 2008 12:30 AM
Binding a combo box to update a list view Amethyst1984 General .NET 0 February 28th, 2006 03:04 PM
Weird Combo Box Data Binding Problem kpowers@wppisys.org VB.NET 1 February 12th, 2005 04:52 PM
Weird Combo Box Binding Error kpowers@wppisys.org Pro VB.NET 2002/2003 0 October 8th, 2003 08:38 AM



All times are GMT -4. The time now is 02:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.