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.
In order to complete this walkthrough, you will need the following:
- Access to the Microsoft Access Northwind database.
- Visual Studio 2005/2008/2010 Application.
To create new web application and use custom aggregate function
- Bind WebGrid
to AccessDataSource.
- Open WebGrid Designer-Advanced-WebGrid-LayoutSettings and set
the ColumnFooters to Yes.
- Group a column by "ReorderLevel" column by using GroupColumns.
- Set the AggregateFunction to "Custom" and
FooterText to "Count =" to the UnitsOnOrder column.
- Add custom aggregate code into the WebGrid1_CustomAggregate server
side event.
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;
}
|
- Compile and run the WebForm.