ComponentOne True DBGrid for WinForms
Object Model / Working with Objects and Collections / Working with Collections
In This Topic
    Working with Collections
    In This Topic

    A C1TrueDBGrid object has eight separate collections that govern its diverse objects. Each of these collections has an associated property within the C1TrueDBGrid object that returns the collection object. This prevents the need for the developer to enter the entire collection name when using the grid in code. The following table outlines these mappings:

    Collection Associated Property
    C1DataColumnCollection Columns property
    C1DisplayColumnCollection DisplayColumns property
    GridStyleCollection Styles property
    SelectedColumnCollection SelectedCols property
    SelectedRowCollection SelectedRows property
    SplitCollection Splits property
    ValueItemCollection Values property

    By default, the SplitCollection object contains one Split object. The GridStyleCollection object contains ten default Style objects: Normal, Heading, Footing, Selected, Caption, HighlightRow, EvenRow, OddRow, RecordSelector,and FilterBar.

    Reference an object in a collection using its zero-based index. Read or set the Split object's properties as follows:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Read a Split object property.
    variable = Me.C1TrueDBGrid1.Splits(0).Property
     
    ' Set a Split object property.
    Me.C1TrueDBGrid1.Splits(0).Property = variable
    

    To write code in C#

    C#
    Copy Code
    // Read a Split object property.
    variable = this.c1TrueDBGrid1.Splits[0].Property;
     
    // Set a Split object property.
    this.c1TrueDBGrid1.Splits[0].Property = variable;
    

    Create a reference to an object in a collection using the collection's Item method. The following code creates a reference to a grid's default Split object:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Split0 as a Split object.
    Dim Split0 As C1.Win.C1TrueDBGrid.Split
     
    ' Set Split0 to reference the first Split in the collection.
    Split0 = Me.C1TrueDBGrid1.Splits(0)
    

    To write code in C#

    C#
    Copy Code
    // Declare Split0 as Split object.
    C1.Win.C1TrueDBGrid.Split Split0;
     
    // Set Split0 to reference the first Split in the collection.
    Split0 = this.c1TrueDBGrid1.Splits[0];
    

    Note the use of the namespace qualifier in the preceding example. Using the namespace qualifier is recommended in order to resolve potential naming conflicts with other controls. For example, if another control is used in the same project that also defines an object named Split, the True DBGrid for WinForms namespace qualifier is required, as is the namespace qualifier for the other control.

    Since the Item method is implicit for collections, it can be omitted:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Split0 as a Split object.
    Dim Split0 As C1.Win.C1TrueDBGrid.Split
     
    ' Set Split0 to reference the first Split in the collection.
    Split0 = Me.C1TrueDBGrid1.Splits(0)
    

    To write code in C#

    C#
    Copy Code
    // Declare Split0 as Split object.
    C1.Win.C1TrueDBGrid.Split Split0;
     
    // Set Split0 to reference the first Split in the collection.
    Split0 = this.c1TrueDBGrid1.Splits[0];
    

    Use Split0 to read or set the Split object's properties or to execute its methods:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Read a Split object property.
    variable = Split0.Property
     
    ' Set a Split object property.
    Split0.Property = variable
     
    ' Execute a Split object method.
    Split0.Method (arg1, arg2, ...)
    

    To write code in C#

    C#
    Copy Code
    // Read a Split object property.
    variable = Split0.Property; 
     
    // Set a Split object property.
    Split0.Property = variable; 
     
    // Execute a Split object method.
    Split0.Method (arg1, arg2, ...);
    

    Very often, you need to read and set more than one of an object's properties. For example:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Read a Split object's properties.
    variable1 = Me.C1TrueDBGrid1.Splits(0,0).Property1
    variable2 = Me.C1TrueDBGrid1.Splits(0,0).Property2
     
    ' Set a Split object's properties.
    Me.C1TrueDBGrid1.Splits(0,0).Property1 = variable1
    Me.C1TrueDBGrid1.Splits(0,0).Property2 = variable2
    

    To write code in C#

    C#
    Copy Code
    // Read a Split object's properties.
    variable1 = this.c1TrueDBGrid1.Splits[0,0].Property1;
    variable2 = this.c1TrueDBGrid1.Splits[0,0].Property2;
     
    // Set a Split object's properties.
    this.c1TrueDBGrid1.Splits[0,0].Property1 = variable1;
    this.c1TrueDBGrid1.Splits[0,0].Property2 = variable2;
    

    This code is very inefficient because the amount of times the object C1TrueDBGrid1.Splits(0,0) is accessed. It is more efficient to create a single reference to the object up front and use it repeatedly:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Split0 as a Split.
    Dim Split0 As C1TrueDBGrid.Split
     
    ' Set Split0 to reference the first Split in the collection.
    Split0 = Me.C1TrueDBGrid1.Splits.Item(0,0)
     
    ' Read a Split object's properties.
    variable1 = Split0.Property1
    variable2 = Split0.Property2
     
    ' Set a Split object's properties.
    Split0.Property1 = variable1
    Split0.Property2 = variable2
    

    To write code in C#

    C#
    Copy Code
    // Declare Split0 as Split object.
    C1TrueDBGrid.Split Split0;
     
    // Set Split0 to reference the first Split in the collection.
    Split0 = this.c1TrueDBGrid1.Splits[0,0];
     
    // Read a Split object's properties.
    variable1 = Split0.Property1;
    variable2 = Split0.Property2;
     
    // Set a Split object's properties.
    Split0.Property1 = variable1;
    Split0.Property2 = variable2;
    

    This code is much more efficient and also easier to read. If the Visual Studio application accesses collection objects frequently, the performance of your code can be improved significantly by adhering to these guidelines.

    Similarly, this technique can be applied to other objects and collections of True DBGrid, and of Visual Studio in general. Of particular importance to the grid are the C1DataColumn and C1DataColumnCollection objects (also applies to C1DisplayColumn object):

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Declare Cols as a Columns collection object, then set it to reference C1TrueDBGrid1's C1DataColumnCollection object.
    Dim Cols As C1.Win.C1TrueDBGrid.C1DataColumnCollection
    Cols = Me.C1TrueDBGrid1.Columns
     
    ' Declare Col0 as a C1DataColumn object, then set it to referencethe first Column object in the collection.
    Dim Col0 As New C1.Win.C1TrueDBGrid.C1DataColumn
    Col0 = Cols(0)
     
    ' Read and set the C1DataColumn object's Property1.
    variable1 = Col0.Property1
    Col0.Property1 = variable1
     
    ' Execute the C1DataColumn object's Method1 (declared as a Sub).
    Col0.Method1 (arg1, arg2, ...)
     
    ' Execute the C1DataColumn object's Method2 (declared as aFunction).
    variable2 = Col0.Method2(arg1)
    

    To write code in C#

    C#
    Copy Code
    // Declare Cols as a Columns collection object, then set it to reference C1TrueDBGrid1's C1DataColumnCollection object.
    C1.Win.C1TrueDBGrid.C1DataColumnCollection Cols;
    Cols = this.c1TrueDBGrid1.Columns;
     
    // Declare Col0 as a C1DataColumn object, then set it to referencethe first Column object in the collection.
    C1.Win.C1TrueDBGrid.C1DataColumn Col0 = new C1TrueDBGrid.DataColumn();
    Col0 = Cols[0];
     
    // Read and set the C1DataColumn object's Property1.
    variable1 = Col0.Property1;
    Col0.Property1 = variable1;
     
    // Execute the C1DataColumn object's Method1 (declared as a Sub).
    Col0.Method1 (arg1, arg2, ...);
     
    // Execute the C1DataColumn object's Method2 (declared as a Function).
    variable2 = Col0.Method2(arg1);
    

    Visual Basic also provides an efficient With statement for setting multiple properties of an object without explicitly assigning it to a variable. For example, the following code sets multiple properties of the first column of a grid (recall that collections are zero-based):

    To write code in Visual Basic

    Visual Basic
    Copy Code
    With Me.C1TrueDBGrid1.Columns(0)
        .Property1 = variable1
        .Property2 = variable2
    End With
    

    To write code in C#

    C#
    Copy Code
    this.c1TrueDBGrid1.Columns[0].Property1 = variable1;
    this.c1TrueDBGrid1.Columns[0].Property2 = variable2;
    
    See Also