السبت، 22 أكتوبر 2011

Combo Box Data Binding With VB .Net

Introduction

Combo box data binding allows a combo box on a form to display a list that is external to the source of data of its form. For example, you can have a combo box that displays a fixed list of values that you create from the Items property of the combo box. For a relational database, you may want the combo box to display a list of values from a column of another table. To make this possible, you must link the combo box to the items of the other table.
To follow this example, you should have Microsoft SQL Sever 2005 and Microsoft Visual Studio 2005.
Practical Learning: Binding a Combo Box 


  1. To get the database used in this exercise start the Microsoft SQL Server Management Studio and log in to the server
  2. On the main Menu click File -> New -> Query With Current Connection
  3. To create the database, type the following:
     
    CREATE DATABASE StudentBinding;
    GO
    
    USE StudentBinding;
    GO
    CREATE TABLE Genders
    (
        GenderID int identity(1,1) PRIMARY KEY,
        Gender varchar(20)
    );
    GO
    INSERT INTO Genders(Gender) VALUES('Female');
    GO
    INSERT INTO Genders(Gender) VALUES('Male');
    GO
    INSERT INTO Genders(Gender) VALUES('Unknown');
    GO
    
    CREATE TABLE Students
    (
        StudentID int identity(1,1) PRIMARY KEY,
        FirstName varchar(20),
        LastName varchar(20) NOT NULL,
        GenderID int
    );
    GO
    INSERT INTO Students(FirstName, LastName, GenderID)
    VALUES('Helene', 'Mukoko', 1);
    GO
    INSERT INTO Students(FirstName, LastName, GenderID)
    VALUES('Gertrude', 'Wachs', 3);
    GO
    INSERT INTO Students(FirstName, LastName, GenderID)
    VALUES('Peter', 'Lansome', 2);
    GO
    INSERT INTO Students(FirstName, LastName, GenderID)
    VALUES('Paul', 'Motto', 2);
    GO
    INSERT INTO Students(FirstName, LastName, GenderID)
    VALUES('Hermine', 'Ngaleu', 1);
    GO
  4. Press F5 to execute
  5. Start Microsoft Visual Studio 2005
  6. To create a new application, on the main menu, click File -> New -> Project ...
  7. In the Projects list, select Visual C++ and, in the Templates list, click Windows Forms Application
  8. Set the Name to StudentBinder1 and click OK
  9. On the main menu, click Data -> Add New Data Source
  10. In the first page of the wizard, make sure Database is selected and click Next
  11. In the second page of the wizard, click New Connection
  12. For the server name, type (local)
  13. For the Database Name, select StudentBinding

  14. Click OK
  15. In the second page of the wizard, make sure ServerName.StudentBing.dbo is selected in the combo box and click Next
  16. In the third page of the wizard, click the Tables check (that will select both tables of our database)
  17. Set the DatasetName to dsStudents

  18. Click Finish
  19. From the Data Source window, click Students and click the arrow of its combo box to select Details
  20. Drag Students and drop it on the form
  21. Delete the GenderID textBox and customize the design of the form as you see fit
  22. On the Toolbox, in the Data section, click BindingSource and drop it on the form
  23. Change its properties as follows:
    Name: bsGenders
    DataSource: dsStudents
    DataMember: Genders
  24. Add a combo box to the form and specify its properties as follows:
     
    NameDataSourceDisplayMemberValueMemberDataBindings -> SelectedValue
    cboGendersbsGendersGenderGenderIDStudentsBindingSource - GenderID
  25. Execute the application to see the result

ليست هناك تعليقات:

إرسال تعليق