ComponentOne List for WinForms
In This Topic
    Tutorial 7 - Displaying Unbound Columns in a Bound List
    In This Topic

    In this tutorial, you will learn how to use the UnboundColumnFetch event to display two fields (First and Last) together in one column.

    1. Follow steps 1 through 4 of Tutorial 1 - Binding C1List to a DataSet to create a project with a C1List control bound to a Data Set.
    2. Declare a new global DataTable object in Form1:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim dtCopy As New DataTable()
      

      To write code in C#

      C#
      Copy Code
      DataTable dtCopy = new DataTable();
      
    3. Add the following code to the Form_Load event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       ' Visual Studio 2005 adds this line of code to load the data.         
       Me.ComposerTableAdapater.Fill(Me.DsComposer.Composer) 
                      
       dtCopy = Me.DsComposer1.Tables(0).Copy()         
       Me.C1List1.Splits(0).DisplayColumns.Item ("Last").Visible = False         
       Me.C1List1.Splits(0).DisplayColumns.Item ("First").Visible = False
      

      To write code in C#

      C#
      Copy Code
       // Visual Studio 2005 adds this line of code to load the data.         
       this.ComposerTableAdapter.Fill(this.DsComposer.Composer);       
               
       dtCopy = this.DsComposer1.Tables[0].Copy();         
       this.c1List1.Splits[0].DisplayColumns["Last"].Visible = false;         
       this.c1List1.Splits[0].DisplayColumns["First"].Visible = false;
      
      Note: The first line fills the dataset, the second line makes a copy of this DataSet, which we will use later to populate the unbound column. The last two lines of code hide the columns with field names First and Last.
    4. To create an unbound column open up the C1List Designer by clicking on the ellipsis button next to the Columns property in the Properties window. Next click the Add button in the left pane to create column, which is added at the bottom of the list. You can maneuver where you would like the column to appear by using dragging the column to where you would like it to appear. Move the new column to the very first column and set its Caption property to Name in the left pane. Notice that we have a value in the Caption field, but no value in the Data field. This is how C1List knows it is an unbound column. C1List now knows to fire the UnboundColumnFetch event.
    5. Open the Split Collection Editor by clicking on the ellipsis button next to the Splits property in the Properties window. Open the C1DisplayColumn Collection Editor by clicking on the ellipsis button next to the DisplayColumns property. In this editor, find the unbound column that we just created, and set its Visible property equal to True. Now our unbound column is visible to the end-user and not just the C1List control.
    6. Add the following code to the UnboundColumnFetch event:

      To write code in Visual Basic

      Visual Basic
      Copy Code
       Private Sub C1List1_UnboundColumnFetch(ByVal sender As Object, ByVal e As C1.Win.C1List.UnboundColumnFetchEventArgs) Handles C1List1.UnboundColumnFetch         
           If (e.Col = 0 And e.Row < dtCopy.Rows.Count) Then         
               e.Value = dtCopy.Rows(e.Row).Item("First") + " " + dtCopy.Rows(e.Row).Item("Last")"         
           End If         
       End Sub
      

      To write code in C#

      C#
      Copy Code
       private void C1List1_UnboundColumnFetch( object sender, C1.Win.C1List.UnboundColumnFetchEventArgs e)         
       {         
           if (e.Col == 0 && e.Row < dtCopy.Rows.Count)         
           {         
               e.Value = dtCopy.Rows[e.Row].["First"] + " " + dtCopy.Rows[e.Row].["Last"]";         
           }         
       }
      
      Note: This code uses dtCopy to gather values, which are set to e.Value, to place them in the unbound column.

    Run the program and observe the following:

    This concludes the tutorial.