Intersoft Support Center

Implement Custom Column Aggregation Rules

WebGrid introduces a powerful feature that is able to perform aggregation function on grouped rows as well as all rows totalling. The provided aggregation functions such as Count, Sum, Average, Min and Max are generally applicable to most application's requirements.

During this walkthrough, you will learn how to do the following:

  • Use AccessDataSource control.
  • Use Custom Aggregate function in WebGrid.

 Prerequisites

In order to complete this walkthrough, you will need the following:

  • Access to the Microsoft Access Northwind database.
  • Visual Studio 2005/2008/2010 Application.

 Step-By-Step Instructions

To create new web application and use custom aggregate function

  1. Bind WebGrid to AccessDataSource.
  2. Open WebGrid Designer-Advanced-WebGrid-LayoutSettings and set the ColumnFooters to Yes.



  3. Group a column by "ReorderLevel" column by using GroupColumns.



  4. Set the AggregateFunction to "Custom" and FooterText to "Count =" to the UnitsOnOrder column.



  5. Add custom aggregate code into the WebGrid1_CustomAggregate server side event.

    C# Copy Code
    if (e.Column.DataMember == "UnitsOnOrder") 
    {
       WebGridRowCollection rows = e.Rows;
       int count = 0;
       for (int i=0; i < rows.Count; i++) 
       {
          WebGridCell cell = rows[i].Cells[e.Column.Position];
          if (int.Parse( cell.Value.ToString() ) > 0) count++;
       }
       e.AggregateResult = count;
    }
    

  6. Compile and run the WebForm.
Previous Next