Thursday, 27 June 2013

Complex Data Binding in Windows Forms

Complex Data Binding in Windows Forms

We will build Windows Form that will display all student in a bound ListBox and thestudents's associated marks in a bound DataGrid. A more complex DataSet will be needed that provides the relationship path to traverse from student to marks  using the primary key of student (stdid) to foreign key in marks (stdid). we will have to do data bindings for both the ListBox control and the DataGrid control.

Create a New Project in VS .NET

  1. Create a new project in VS .NET by choosing File, New, and then choosing the Project option..
  2. When the New Project dialog box appears, choose  Visual C# Projects and Windows Applications. Name this project "comlexdatabinding". This creates a default form for you to start from.
Add the Data Connection and Two Data Adapters
we will need to access both the student  table and themarks table, so two data adapters will be created, each populating different controls but using one database connection.
From the Data tab of the Toolbox, drag aOledbDataAdapter object into your form. This will automatically invoke the Data Adapter Configuration Wizard. Both the data connection and the data adapter can be fully configured here.
  1. The wizard starts with the Choose our Data Connection dialog box. If   there  already exist a connection defined in your project,then it will be placed in the dialog box; otherwise, choose to create a new connection and specify the appropriate connection information .
  2. Choose the Use SQL Statements option.
  3. we  will be presented with a Generate the SQL Statements dialog box where you will simply type in a valid SQL statement, or you can use the Query Builder option to formulate the SQL query.such as:
    SELECT stdid,stdname FROM student
  4. Finally, the wizard will show you the tasks that it has done and indicate whether the oledbDataAdapter has been configured successfully.

  1.  we need one more data adapter for access to the mrks  table. Drag another oledbDataAdapter object onto the form.
    1. Again, the wizard starts with the Choose Your Data Connection dialog box.
    2. Choose the Use SQL Statements option.
    3. we  will be presented with a Generate the SQL Statements dialog box where you will type the following query:
      SELECT subid, stdid, subname,mark
      FROM marks
      d .Then the wizard will show we  the tasks that it has done and indicate whether the oledbDataAdapter has been configured successfully .
      
      
      Generate a DataSet
      Now that the OledbDataAdapter and DataConnection objects have been configured and added to the form, you must generate a DataSet and then add an instance of this DataSetto the form.
      1. From the Data menu in Visual Studio, simply choose the Generate Dataset option.
      2.                   
        XML Schema editor—STUDENTDS.xsd schema file.
        Next, drag a Relation object onto the Orders table
         Adding the ListBox and DataGrid Controls
         create the ListBox to display all of the Sudent Names from the Student table.
        1. Drag a ListBox object from the Windows Forms tab of the Toolbox onto the Form.
        2. Press F4 to go right to the properties of this ListBox.
        3. For the DataSource property, you will need to select the dtatsaet1 data source.
        4. For the DisplayMember property, you will select student , expand this node, and select studentname.shows the complete ListBox property.
         the DataGrid control to display all of the Orders that are associated with a particular Company that is selected.
        1. Drag a DataGrid object from the Windows Forms tab of the toolbox onto the Form.
        2. Press F4 to go right to the properties of this DataGrid.
        3. For the DataSource property, you will need to select the student data source.
        4. And, for the DisplayMember property, you will select and expand student, and then select marks
        Just double-click on the form to create a handler for the form's Load event. You will need to clear theDataSet first, and then fill each data adapter that we defined.
        datsaet1.Clear()
        OledbDataAdapter1.Fill(dataset1)
        OledbDataAdapter2.Fill(dataset1)
         

No comments: