ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Working with the Count Property

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

·      Visual Basic

' Set a variable equal to the number of Splits in C1TrueDBGrid1.

variable = Me.C1TrueDBGrid1.Splits.Count

·      C#

// Set a variable equal to the number of Splits in C1TrueDBGrid1.

variable = this.C1TrueDBGrid1.Splits.Count;

·      Delphi

// Set a variable equal to the number of Splits in C1TrueDBGrid1.

variable := Self.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:

·      Visual Basic

For n = 0 To Me.C1TrueDBGrid1.Columns.Count - 1

    Debug.WriteLine(Me.C1TrueDBGrid1.Columns(n).Caption)

Next n

·      C#

for (n = 0; n < this.c1TrueDBGrid1.Columns.Count; n++)

{

           Console.WriteLine(this.c1TrueDBGrid1.Columns[n].Caption);

}

·      Delphi

for n := 0 to Self.C1TrueDBGrid1.Columns.Count – 1 do

  Debug.WriteLine(Self.C1TrueDBGrid1.Columns[n].Caption);

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

·      Visual Basic

' 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.TrueDBGrid1.Columns.Insert(NumCols, C)

 

' Make the new column visible, since columns created at run time are invisible by default.

Me.TrueDBGrid1.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

·      C#

// 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.TrueDBGrid1.Columns.Insert(NumCols, C);

 

// Make the new column visible, since columns created at run time are invisible by default.

this.TrueDBGrid1.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);

}

·      Delphi

var NumCols: Integer;

  C: C1TrueDBGrid.C1DataColumn;

 

// Determine how many columns there are.

NumCols := Self.C1TrueDBGrid1.Columns.Count;

 

// Append a column to the end of the Columns collection.

Self.TrueDBGrid1.Columns.Insert(NumCols, C);

 

// Make the new column visible, since columns created at run time are invisible by default.

Self.TrueDBGrid1.Columns[NumCols].Visible := True;

 

// The following loop removes all columns from the grid.

while (Self.C1TrueDBGrid1.Columns.Count > 0) do

  Self.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:

·      Visual Basic

Dim C As C1TrueDBGrid.C1DataColumn

For Each C In Me.C1TrueDBGrid1.Columns

    Debug.WriteLine(C.Caption)

Next S

·      C#

C1TrueDBGrid.C1DataColumn c;

foreach (c In this.C1TrueDBGrid1.Columns)

{

    Console.WriteLine(c);

}

·      Delphi

var C: C1TrueDBGrid.C1DataColumn;

    i: Integer;

 

for i := 0 to Self.C1TrueDBGrid1.Columns.Count-1 do

begin

  C := Self.C1TrueDBGrid1.Columns[i];

  Debug.WriteLine(C.Caption);

end;

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


Send comments about this topic to ComponentOne.
Copyright © ComponentOne LLC. All rights reserved.