ComponentOne True DBGrid for .NET (2.0) Search HelpCentral 

Group Rows by Custom Setting

This topic demonstrates how to use the GroupIntervalEnum.Custom member in C1TrueDBGrid.

1.   Start a new .NET project.

2.   Open the Toolbox and add a C1TrueDBGrid control to the form.

3.   Open the C1TrueDBGrid Tasks menu, click the drop-down arrow in the Choose Data Source box,  and click Add Project Data Source. In the adapter’s Data Source Configuration Wizard, either select a connection to NWind.mdb or create a new connection to this database. On the Choose your database objects page of the wizard, select all fields in the Products table and type "Products" into the DataSet name box, and then finish out the wizard.

4.   Add the following code to the Form_Load event:

·      Visual Basic

Me.ProductsTableAdapter.Fill(Me.Products._Products)

·      C#

this.ProductsTableAdapter.Fill(this.Products._Products);

·      Delphi

Self.ProductsTableAdapter.Fill(Self.Products._Products);

5.   Set the DataView property to DataViewEnum.GroupBy.

In the Designer

In the C1TrueDBGrid Tasks menu, select GroupBy from the Data Layout drop-down.

In Code

Add the following code to the Form_Load event:

·      Visual Basic

Me.C1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy

·      C#

this.c1TrueDBGrid1.DataView = C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;

·      Delphi

Self.C1TrueDBGrid1.DataView := C1.Win.C1TrueDBGrid.DataViewEnum.GroupBy;

6.   Open the C1TrueDBGrid Designer by selecting Designer from the C1TrueDBGrid Tasks menu.

7.   Select the UnitPrice column by clicking on it in the right pane.

The column can also be selected by choosing UnitPrice from the drop-down list in the toolbar.

8.   Set the Interval property to GroupIntervalEnum.Custom.

In the Designer

Locate the Interval property in the left pane of the TrueDBGrid Designer and set it to custom.

In Code

Add the following code to the Form_Load event:

·      Visual Basic

' Set the GroupInfo.Interval of the grid to Custom.

Me.C1TrueDBGrid1.Columns("UnitPrice").GroupInfo.Interval = C1.Win.C1TrueDBGrid.GroupIntervalEnum.Custom

·      C#

// Set the GroupInfo.Interval of the grid to Custom.

this.c1TrueDBGrid1.Columns["UnitPrice"].GroupInfo.Interval = C1.Win.C1TrueDBGrid.GroupIntervalEnum.Custom;

·      Delphi

// Set the GroupInfo.Interval of the gird to Custom.

Self.C1TrueDBGrid1.Columns['UnitPrice'].GroupInfo.Interval := C1.Win.C1TrueDBGrid.GroupIntervalEnum.Custom;

9.   Set the NumberFormat property of the UnitPrice column to Currency.

In the Designer

Locate the NumberFormat property in the left pane of the TrueDBGrid Designer and set it to Currency.

In Code

Add the following code to the Form_Load event:

·      Visual Basic

' Set the UnitPrice column to be displayed as currency.

Me.C1TrueDBGrid1.Columns("UnitPrice").NumberFormat = "Currency"

·      C#

// Set the UnitPrice column to be displayed as currency.

this.c1TrueDBGrid1.Columns["UnitPrice"].NumberFormat = "Currency";

·      Delphi

// Set the UnitPrice column to be displayed as currency.

Self.C1TrueDBGrid1.Columns['UnitPrice'].NumberFormat := 'Currency';

10.  To keep the UnitPrice column visible after grouping by it, set the ColumnVisible property to True.

In the Designer

Locate the ColumnVisible property in the left pane of the TrueDBGrid Designer and set it to  True.

In Code

Add the following to the Form_Load event:

·      Visual Basic

' Keep the UnitPrice column visible while grouping.

Me.C1TrueDBGrid1.Columns("UnitPrice").GroupInfo.ColumnVisible = True

·      C#

// Keep the UnitPrice column visible while grouping.

this.c1TrueDBGrid1.Columns["UnitPrice"].GroupInfo.ColumnVisible = true;

·      Delphi

// Keep the UnitPrice column visible while grouping.

Self.C1TrueDBGrid1.Columns[UnitPrice].GroupInfo.ColumnVisible := True;

11.  Add the following code to the GroupInterval event:

·      Visual Basic

' Get the value from the current grid cell as a Decmimal.

Dim p As Decimal = CType(Me.C1TrueDBGrid1(e.Row, e.Col.DataColumn.DataField), Decimal)

 

' Assign the custom grouping values.

If p > 0 And p < 10 Then

    e.Value = "$0 - $9.99"

ElseIf p >= 10 And p < 20 Then

    e.Value = "$10.00 - $19.99"

ElseIf p >= 20 And p < 40 Then

    e.Value = "$20.00 - $39.99"

ElseIf p >= 40 And p < 60 Then

    e.Value = "$40.00 - $59.99"

ElseIf p >= 60 Then

    e.Value = "$60 And Greater"

End If

·      C#

// Get the value from the current grid cell as a Decimal.

decimal p = ((decimal)this.c1TrueDBGrid1(e.Row,e.Col.DataColumn.DataField));

 

// Assign the custom grouping values.

If (p > 0 && p < 10)

{

    e.Value = "$0 - $9.99";

}

else if (p >= 10 && p < 20)

{

    e.Value = "$10.00 - $19.99";

}

else if (p >= 20 && p < 40)

{

    e.Value = "$20.00 – $39.99";

}

else if (p >= 40 && p < 60)

{

    e.Value = "$40.00 - $59.99";

}

else if (p >= 60)

{

    e.Value – "$60 and Greater";

}

·      Delphi

// Get the value from the current grid cell as a Decimal.

p := (System.Decimal(Self.C1TrueDBGrid1(e.Row, e.Col.DataColumn.DataField)));

 

// Assign the custom grouping values.

&If(((p > 0) and (p < 10)));

e.Value := '$0 - $9.99';

&If(((p >= 10) and (p < 20)));

e.Value := '$10 - $19.99';

&If(((p >= 20) and (p < 40)));

e.Value := '$20 - $39.99';

&If(((p >= 40) and (p < 60)));

e.Value := '$40 - $59.99';

&If(((p > 60)));

e.Value := '$60 and Greater';

In this example, the UnitPrice column is grouped according to a customized range of values.


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