True DBGrid for WinForms | ComponentOne
In This Topic
    Working with Objects and Collection
    In This Topic

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

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

    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:

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

    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:

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

    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;
    

    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:

    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;
    

    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 and C1DataColumnCollection objects (also applies to C1DisplayColumn object):

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

    C#
    Copy Code
    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 objects to the ValueItemCollection object:

    C#
    Copy Code
    // Create a ValueItem object.
    C1TrueDBGrid.ValueItem v = new C1TrueDBGrid.ValueItem();
    this.c1TrueDBGrid1.Columns[0].ValueItems.Values.Add(v);
    

    This code adds a ValueItem object to the ValueItemCollection of C1TrueDBGrid1. Alternatively, create a ValueItem object with index 1 with the Insert method:

    C#
    Copy Code
    // 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 object. InsertHorizontalSplit / RemoveHorizontalSplit and InsertVerticalSplit / RemoveVerticalSplit 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 method:

    C#
    Copy Code
    // 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:

    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:

    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:

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

    C#
    Copy Code
    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.