# Working with Objects and Collection

## Content



**True DBGrid for WinForms** is a control that was developed using the latest .NET technologies. The **True DBGrid for WinForms** controls and their programmable components are all .NET objects designed according to Microsoft specifications. If you're already familiar with the Microsoft .NET object and collection models, you'll have no problem using **True DBGrid for WinForms**.

If you're new to Visual Studio, please read the following sections, which illustrates how to manipulate **True DBGrid for WinForms** objects in code. Although individual objects are designed to perform different tasks, the techniques used to manipulate them are the same. Once you have mastered these common programming constructs, using Visual Studio controls will be quite easy. Regardless of your experience level, this will provide a thumbnail sketch of all **True DBGrid for WinForms** objects and collections.

A **True DBGrid** object is created when the TrueDB Grid control is placed on a form. A [TrueDBGrid](/componentone/docs/win/online-truedbgrid/) object has eight separate collections that govern its diverse objects. Each of these collections has an associated property within the [C1TrueDBGrid](/componentone/docs/win/online-truedbgrid/) 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](/componentone/docs/win/online-truedbgrid/) | [Columns](/componentone/docs/win/online-truedbgrid/) property |
| [C1DisplayColumnCollection](/componentone/docs/win/online-truedbgrid/) | [DisplayColumns](/componentone/docs/win/online-truedbgrid/) property |
| [GridStyleCollection](/componentone/docs/win/online-truedbgrid/) | [Styles](/componentone/docs/win/online-truedbgrid/) property |
| [SelectedColumnCollection](/componentone/docs/win/online-truedbgrid/) | [SelectedCols](/componentone/docs/win/online-truedbgrid/) property |
| [SelectedRowCollection](/componentone/docs/win/online-truedbgrid/) | [SelectedRows](/componentone/docs/win/online-truedbgrid/) property |
| [SplitCollection](/componentone/docs/win/online-truedbgrid/) | [Splits](/componentone/docs/win/online-truedbgrid/) property |
| [ValueItemCollection](/componentone/docs/win/online-truedbgrid/) | [Values](/componentone/docs/win/online-truedbgrid/) property |

By default, the [SplitCollection](/componentone/docs/win/online-truedbgrid/) object contains one [Split](/componentone/docs/win/online-truedbgrid/) object. The [GridStyleCollection](/componentone/docs/win/online-truedbgrid/) object contains ten default [Style](/componentone/docs/win/online-truedbgrid/) objects: **Normal**, **Heading**, **Footing**, **Selected**, **Caption**, **HighlightRow**, **EvenRow**, **OddRow**, **RecordSelector**,and **FilterBar**.

You can refer an object in a collection using its zero-based index. Read or set the [Split](/componentone/docs/win/online-truedbgrid/) object's properties as follows:

```csharp
// 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](/componentone/docs/win/online-truedbgrid/) object:

```csharp
// 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 above code example. Adding the namespace qualifier is highly recommended as it helps in resolving the potential naming conflicts with other controls.

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

```csharp
// 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](/componentone/docs/win/online-truedbgrid/) object's properties or to execute its methods:

```csharp
// 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 may need to read and set more than one of an object's properties. For example:

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

But note that the above code may turn inefficient because of the number of times the object C1TrueDBGrid1.Splits(0,0) is accessed. To enhance efficiency, you can create a single reference to the object up front and use it repeatedly:

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

As you can observe from the preceding example, the code lines are much effective and easier to read as well. If the Visual Studio application accesses collection objects frequently, the performance of your code can be improved significantly by adhering to such 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](/componentone/docs/win/online-truedbgrid/) and [C1DataColumnCollection](/componentone/docs/win/online-truedbgrid/) objects (also applies to [C1DisplayColumn](/componentone/docs/win/online-truedbgrid/) object):

```csharp
// 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);
```

Now, the following code sets multiple properties of the first column of a grid (recall that collections are zero-based):

```csharp
this.c1TrueDBGrid1.Columns[0].Property1 = variable1;
this.c1TrueDBGrid1.Columns[0].Property2 = variable2;
```

## Adding Members

To create and add an object to a collection, use the collection's **Add** method. The method takes an object as its only argument. For example, create more valueitems for a column by adding new [ValueItem](/componentone/docs/win/online-truedbgrid/) objects to the [ValueItemCollection](/componentone/docs/win/online-truedbgrid/) object:

```csharp
// Create a ValueItem object.
C1TrueDBGrid.ValueItem v = new C1TrueDBGrid.ValueItem();
this.c1TrueDBGrid1.Columns[0].ValueItems.Values.Add(v);
```

This code adds a [ValueItem](/componentone/docs/win/online-truedbgrid/) object to the [ValueItemCollection](/componentone/docs/win/online-truedbgrid/) of C1TrueDBGrid1. Alternatively, create a [ValueItem](/componentone/docs/win/online-truedbgrid/) object with index 1 with the **Insert** method:

```csharp
// Create a Split object with index 1.
C1TrueDBGrid.ValueItem S;
this.c1TrueDBGrid1.Columns[0].ValueItems.Values.Insert(1, S);
```

The only object that is unable to add or remove members using the **Add** or **RemoveAt** methods is the [Split](/componentone/docs/win/online-truedbgrid/) object. [InsertHorizontalSplit](/componentone/docs/win/online-truedbgrid/) / [RemoveHorizontalSplit](/componentone/docs/win/online-truedbgrid/) and [InsertVerticalSplit](/componentone/docs/win/online-truedbgrid/) / [RemoveVerticalSplit](/componentone/docs/win/online-truedbgrid/) methods of the split object must be used to correctly add or remove Splits. These methods are also available in the grid's right-click context menu at design time.

## Removing Members

Regardless of how a collection implements the **Add** or **Insert** methods, the syntax for removing items is the same. To remove an existing item from a collection, use the [RemoveAt](/componentone/docs/win/online-truedbgrid/) method:

```csharp
// Remove the Split object with index 1.
this.c1TrueDBGrid1.Columns[0].ValueItems.Values.RemoveAt(1);
```

After this statement is executed, all splits with collection indexes greater than 1 will be shifted down by 1 to fill the place of the removed split. Note that the **RemoveAt** method's parameter is the location of the member to be removed.

## Using Count Property

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

```csharp
// 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](/componentone/docs/win/online-truedbgrid/) string of each [C1DataColumn](/componentone/docs/win/online-truedbgrid/) object in a grid:

```csharp
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:

```csharp
// 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 foreach loop statement can be used iterate through the objects in a collection without using the **Count** property:

```csharp
C1TrueDBGrid.C1DataColumn c;
foreach (c In this.c1TrueDBGrid1.Columns)
{
    Console.WriteLine(c);
}
```

In fact, using the foreach loop statement is the easiest way to iterate through the objects in a collection.