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

    Determine the number of objects in a collection using the collection's Count property:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Set a variable equal to the number of Splits in C1TrueDBGrid1.
    variable = Me.C1TrueDBGrid1.Splits.Count
    

    To write code in C#

    C#
    Copy Code
    // Set a variable equal to the number of Splits in C1TrueDBGrid1.
    variable = this.c1TrueDBGrid1.Splits.Count;
    

    Iterate through all objects in a collection using the Count property as in the following example, which prints the Caption string of each C1DataColumn object in a grid:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    For n = 0 To Me.C1TrueDBGrid1.Columns.Count - 1
        Debug.WriteLine(Me.C1TrueDBGrid1.Columns(n).Caption)
    Next n
    

    To write code in C#

    C#
    Copy Code
    for (n = 0; n < this.c1TrueDBGrid1.Columns.Count; n++) 
    {
               Console.WriteLine(this.c1TrueDBGrid1.Columns[n].Caption);
    }
    

    The Count property is also useful for appending and removing columns:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    ' Determine how many columns there are.
    Dim NumCols As Integer
    NumCols = Me.C1TrueDBGrid1.Columns.Count
     
    ' Append a column to the end of the Columns collection.
    Dim C As C1TrueDBGrid.C1DataColumn = New C1TrueDBGrid.C1DataColumn()
    Me.C1TrueDBGrid1.Columns.Insert(NumCols, C)
     
    ' Make the new column visible, since columns created at run timeare invisible by default.
    Me.C1TrueDBGrid1.Splits(0).DisplayColumns(C).Visible = True
     
    ' The following loop removes all columns from the grid.
    While Me.C1TrueDBGrid1.Columns.Count
        Me.C1TrueDBGrid1.Columns.RemoveAt(0)
    End While
    

    To write code in C#

    C#
    Copy Code
    // Determine how many columns there are.
    int NumCols;
    NumCols = this.c1TrueDBGrid1.Columns.Count;
     
    // Append a column to the end of the Columns collection.
    C1TrueDBGrid.C1DataColumn C = new C1TrueDBGrid.C1DataColumn();
    this.c1TrueDBGrid1.Columns.Insert(NumCols, C);
     
    // Make the new column visible, since columns created at run time are invisible by default.
    this.c1TrueDBGrid1.Splits[0].DisplayColumns[C].Visible = true;
     
    // The following loop removes all columns from the grid.
    while ( this.c1TrueDBGrid1.Columns.Count > 0 )
    {
               this.c1TrueDBGrid1.Columns.RemoveAt(0);
    }
    

    An efficient For Each...Next statement that can be used iterate through the objects in a collection without using the Count property:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Dim C As C1TrueDBGrid.C1DataColumn
    For Each C In Me.C1TrueDBGrid1.Columns
        Debug.WriteLine(C.Caption)
    Next S
    

    To write code in C#

    C#
    Copy Code
    C1TrueDBGrid.C1DataColumn c;
    foreach (c In this.c1TrueDBGrid1.Columns)
    {
        Console.WriteLine(c);
    } 
    

    In fact, using the For Each...Next statement is the easiest way to iterate through the objects in a collection.

    See Also