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: Binding a Combo Box

  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
     
  10. Open the ExoADO1 application
  11. Add a new combo box to the form as follows:
     
    People Form
    Control Name Text Additional Properties
    Label   Gender:  
    ComboBox cboGender   DropDownStyle: DropDownList
  12. To update the data source, right-click sqlDataAdapter1 and click Configure Data Adapter...
  13. Click Next
  14. In the Data Adapter Configuration Wizard, in the combo box, select Server.People.dbo and click Next
  15. Accept the default Use SQL Statements radio button and click Next
  16. Change the statement to
     
    SELECT PersonID, FirstName, LastName, GenderID FROM Persons
  17. Click Next and click Finish
  18. To create a data source for the combo box, from the Data section of the Toolbox, click SqlDataAdapter and click the form
  19. Click Next
  20. In the combo box, select Server.People.dbo and click Next
  21. Accept the default Use SQL Statements radio button and click Next
  22. Click Query Builder...
  23. From the Tables property page of the Add Table dialog box, click Genders. Click Add and click Close
  24. In the upper section of the Query Builder window, click the GenderID and the Gender check boxes
  25. Click OK
  26. Click Next and click Finish
  27. To create a data set for the genders, right-click sqlDataAdapter2 and click Generate Dataset...
  28. Accept the Existing radio button and accept the Genders check box
  29. Click OK
  30. On the form, click the combo box
  31. In the Properties window, set the properties as follows:
    DataSource: dsPersons1.Genders
    DisplayMember: Gender
    ValueMember: GenderID
    DataBindings -> SelectedValue: dsPersons1 - Persons.GenderID
  32. Double-click an empty area of the form and change the Load event as follows:
     
    private: System::Void Form1_Load(System::Object *  sender, 
    			System::EventArgs *  e)
    {
    	sqlDataAdapter2->Fill(this->dsPersons1, "Genders");
    	sqlDataAdapter1->Fill(dsPersons1, "Persons");
    }
  33. Press Ctrl + F5 to test the application
 
 

Home Copyright © 2004-2012, FunctionX