ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Working with Collections

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:

·      Visual Basic

' Read a Split object property.

variable = Me.C1TrueDBGrid1.Splits(0).Property

 

' Set a Split object property.

Me.C1TrueDBGrid1.Splits(0).Property = variable

·      C#

// Read a Split object property.

variable = this.C1TrueDBGrid1.Splits[0].Property;

 

// Set a Split object property.

this.C1TrueDBGrid1.Splits[0].Property = variable;

·      Delphi

// Read a Split object property.

variable := Self.C1TrueDBGrid1.Splits[0].Property;

 

// Set a Split object property.

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

·      Visual Basic

' 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)

·      C#

// 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];

·      Delphi

// Declare Split0 as a Split object.

var Split0: C1.Win.C1TrueDBGrid.Split;

 

// Set Split0 to reference the first Split in the collection.

Split0 := Self.C1TrueDBGrid1.Splits.Item[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 C1TrueDBGrid 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:

·      Visual Basic

' 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)

·      C#

// 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];

·      Delphi

// Declare Split0 as a Split object.

var Split0: C1.Win.C1TrueDBGrid.Split;

 

// Set Split0 to reference the first Split in the collection.

Split0 := Self.C1TrueDBGrid1.Splits[0];

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

·      Visual Basic

' 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, ...)

·      C#

// 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, ...);

·      Delphi

// 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:

·      Visual Basic

' Read a Split object's properties.

variable1 = Me.TrueDBGrid1.Splits(0,0).Property1

variable2 = Me.TrueDBGrid1.Splits(0,0).Property2

 

' Set a Split object's properties.

Me.TrueDBGrid1.Splits(0,0).Property1 = variable1

Me.TrueDBGrid1.Splits(0,0).Property2 = variable2

·      C#

// Read a Split object's properties.

variable1 = this.TrueDBGrid1.Splits[0,0].Property1;

variable2 = this.TrueDBGrid1.Splits[0,0].Property2;

 

// Set a Split object's properties.

this.TrueDBGrid1.Splits[0,0].Property1 = variable1;

this.TrueDBGrid1.Splits[0,0].Property2 = variable2;

·      Delphi

// Read a Split object's properties.

variable1 := Self.TrueDBGrid1.Splits[0,0].Property1;

variable2 := Self.TrueDBGrid1.Splits[0,0].Property2;

 

// Set a Split object's properties.

Self.TrueDBGrid1.Splits[0,0].Property1 := variable1;

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

·      Visual Basic

' 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

·      C#

// 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;

·      Delphi

// Declare Split0 as a Split.

var Split0: C1TrueDBGrid.Split;

 

// Set Split0 to reference the first Split in the collection.

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

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):

·      Visual Basic

' 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 reference the 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 a Function).

variable2 = Col0.Method2(arg1)

·      C#

// 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 reference the 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);

·      Delphi

// Declare Cols as a Columns collection object, then set it to reference C1TrueDBGrid1's C1DataColumnCollection object.

// Declare Col0 as a DataColumn object, then set it to reference the first Column object in the collection.

var Cols: C1TrueDBGrid.C1DataColumnCollection;

    Col0: C1TrueDBGrid.C1DataColumn;

 

Cols := Self.C1TrueDBGrid1.Columns;

Col0 := Cols[0];

 

// Read and set the DataColumn object's Property1.

variable1 := Col0.Property1;

Col0.Property1 := variable1;

 

// Execute the DataColumn object's Method1 (declared as a Sub).

Col0.Method1 (arg1, arg2, ...);

 

// Execute the DataColumn object's Method2 (declared as a Function).

variable2 := Col0.Method2(arg1);

Visual Basic and Delphi also provide 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):

·      Visual Basic

With Me.C1TrueDBGrid1.Columns(0)

    .Property1 = variable1

    .Property2 = variable2

End With

·      C#

this.C1TrueDBGrid1.Columns[0].Property1 = variable1;

this.C1TrueDBGrid1.Columns[0].Property2 = variable2;

·      Delphi

with Self.C1TrueDBGrid1.Columns[0]

begin

  Property1 := variable1;

  Property2 := variable2;

end;


Adding Members

Removing Members

Working with the Count Property


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