FunctionX Practical Learning Logo

Combo Box Data Binding


Introduction

This is an example of how to fill a combo box with data from a table.

Practical LearningPractical Learning: Opening the Query Analyzer

  1. First complete the lesson on regular data binding and open it
  2. Create a new table in Design view as follows:
     
    Column Name Data Type Length Addition Properties
    GenderID int   Set Primary Key
    Identity: Yes
    Gender varchar 20 Allow Nulls: Unchecked
  3. Save it as Genders and fill it up as follows:
     
    GenderID Gender
      Unknown
      Female
      Male
  4. Close the table
  5. Open the Persons table in Design view and, in the first empty column, create a new field as follows:
     
    Column Name Data Type Length Addition Properties
    PersonID      
    FirstName      
    LastName      
    GenderID int    
  6. Save the table as Persons
  7. Right-click GenderID and click Relationships...
  8. Set the Primary Key Table and the Foreign Key Table fields as GenderID
     
  9. Click Close
    You can fill out the table with a few records
     
 

Creating a Data Bound Combo Box

.

Practical LearningPractical Learning: Creating a Database Using the Query Analyzer

  1. Open the ExoADO1 application
  2. Add a new combo box to the form as follows:
     
    People Form
    Control Name Text Additional Properties
    Label   Gender:  
    ComboBox cboGender   DropDownStyle: DropDownList
  3. To update the data source, right-click sqlDataAdapter1 and click Configure Data Adapter...
  4. Click Next
  5. In the Data Adapter Configuration Wizard, in the combo box, select Server.People.dbo and click Next
  6. Accept the default Use SQL Statements radio button and click Next
  7. Change the statement to
     
    SELECT PersonID, FirstName, LastName, GenderID FROM Persons
  8. Click Next and click Finish
  9. To create a data source for the combo box, from the Data section of the Toolbox, click SqlDataAdapter and click the form
  10. Click Next
  11. In the combo box, select Server.People.dbo and click Next
  12. Accept the default Use SQL Statements radio button and click Next
  13. Click Query Builder...
  14. From the Tables property page of the Add Table dialog box, click Genders. Click Add and click Close
  15. In the upper section of the Query Builder window, click the GenderID and the Gender check boxes
  16. Click OK
  17. Click Next and click Finish
  18. To create a data set for the genders, right-click sqlDataAdapter2 and click Generate Dataset...
  19. Accept the Existing radio button and accept the Genders check box
  20. Click OK
  21. On the form, click the combo box
  22. In the Properties window, set the properties as follows:
    DataSource: dsPersons1.Genders
    DisplayMember: Gender
    ValueMember: GenderID
    DataBindings -> SelectedValue: dsPersons1 - Persons.GenderID
  23. Double-click an empty area of the form and change the Load event as follows:
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.SqlDataAdapter1.Fill(Me.DsPersons1, "Persons")
            Me.SqlDataAdapter2.Fill(Me.DsPersons1, "Genders")
        End Sub
  24. Press Ctrl + F5 to test the application
 

Home Copyright © 2004-2012, FunctionX