Grouping Data

You can use field formatting to group data. Suppose you have a bound list of products and you want to group all of the items ordered within a year together. You can use the Field Settings dialog box at run time or code. In this example, we'll use a C1OlapPage control bound to the C1Nwind.mdb installed with the product.

To group data by the year at run time:

1.   Add the following fields to the grid view by selecting them in the C1OlapPanel area of the C1OlapPage: OrderDate, Product, and Sales. Click the Olap Grid tab, if necessary, to view the grid.

2.   Right-click the Order Date field under Row Fields and select Field Settings. The Field Settings dialog box appears.

3.   Make sure Select All is selected on the Filter tab.

4.   Click the Format tab and select Custom.

5.   Enter "yyyy" in the Custom Format text box and click OK.

The following images show the grid before grouping and after grouping. In the Before Grouping image, the OrderDates are not grouped. In the After Grouping image, the products are grouped together by the year they were purchased.

 

Before Grouping

 

After Grouping

To group data in code:

You can also group data in code. Here is the code that would be used for the example above:

using C1.Olap;

using System.Data.OleDb;

 

namespace WindowsFormsApplication1

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            // get data

            var da = new OleDbDataAdapter("select * from invoices", GetConnectionString());

            var dt = new DataTable();

            da.Fill(dt);

 

            // bind to olap page

            this.c1OlapPage1.DataSource = dt;

 

            // build view

            var olap = this.c1OlapPage1.OlapEngine;

            olap.ValueFields.Add("UnitPrice");

            olap.RowFields.Add("OrderDate", "ProductName");

 

            // format order date to group data

            var field = olap.Fields["OrderDate"];

            field.Format = "yyyy";

        }

        static string GetConnectionString()

        {

            string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal) + @"\ComponentOne Samples\Common";

            string conn = @"provider=microsoft.jet.oledb.4.0;data source={0}\c1nwind.mdb;";

            return string.Format(conn, path);

        }

    }

}


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.