ComponentOne List for WinForms
In This Topic
    Tutorial 4 - Displaying Master-Detail Relationships
    In This Topic

    This tutorial demonstrates how to link multiple C1List and C1Combo controls using the Change event to trigger related actions. This technique is particularly useful for displaying master-detail relationships.

    1. Create a new .NET 2.0 project.
    2. Place a C1Combo control (dbcComp), and a C1List control (dblOpus) on the form (Form1) as shown in this figure.
    3. Go to the DataSource property for dbcComp and select Add Project Data Source from the drop-down. In the adapter's Data Source Configuration Wizard, either select a connection to C1NWind.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select the Last field in the Composer table and type "DataSet1" into the DataSet name box, and then finish out the wizard.
    4. Go to the DataSource property for dblOpus and select Add Project Data Source from the drop-down. In the adapter's Data Source Configuration Wizard, either select a connection to C1NWind.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select all fields in the Opus table and type "DataSet2" into the DataSet name box, and then finish out the wizard.
    5. Set the DisplayMember property of dbcComp to None.
    6. In the Form_Load event, add the following code:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       ' Visual Studio adds these two lines of code to load the data.   
       Me.ComposerTableAdapter.Fill(Me.DataSet1.Composer) 
       Me.OpusTableAdapter.Fill(Me.DataSet2.Opus)  
       Me.dbcComp_RowChange(Nothing, e.Empty)
      

      To write code in C#

      C#
      Copy Code
        // Visual Studio adds these two lines of code to load the data.
       this.ComposerTableAdapter.Fill(this.DataSet1.Composer);     
       this.OpusTableAdapater.Fill(this.DataSet2.Opus);
       this.dbcComp.RowChange += new EventHandler(this.dbcComp_RowChange);
      
    7. Add the following code to the RowChange event of dbcComp:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       Private Sub dbcComp_RowChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles dbcComp.RowChange
           Dim dr As DataRow()
           dr = Me.DataSet2.Opus.Select("Last ='" & Me.dbcComp.Text + "'")     
           Dim tb As New DataSet2.OpusDataTable()     
           tb = New DataSet2.OpusDataTable()     
           Dim item As DataRow     
           For Each item In dr     
               tb.ImportRow(item)     
           Next item     
           Me.dblOpus.DataSource = tb     
       End Sub
      

      To write code in C#

      C#
      Copy Code
       private void dbcComp_RowChange(object sender, System.EventArgs e)     
       {    
           DataRow[] dr = this.dataSet2.Opus.Select("Last ='" + this.dbcComp.Text + "'");     
           DataSet2.OpusDataTable tb = new DataSet2.OpusDataTable();     
           foreach (DataRow item in dr)     
           {     
               tb.ImportRow(item);     
           }     
           this.dblOpus.DataSource = tb;     
       }
      

    Run the program and observe the following:

    Change the current record position of dbcComp by selecting a different composer. Observe that dblOpus (the detail list) displays a new set of compositions each time the row changes in C1Combo1 (the master list).

    Note: When C1Combo has the focus, press F4 to open the drop-down box.

    To end the program, press the End button on the toolbar. This concludes the tutorial.