给GridView分组统计的Helper类,功能很强大

来源:互联网 发布:mac开机出现五国 编辑:程序博客网 时间:2024/05/21 15:42

资料链接

http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm

 

 

GridView: Creating groups and summaries with 2 lines of code

Site about multilingual website development launched: www.MultiLanguageWebSites.com

GridView has a lot of improvements over the DataGrid but it still lacks some very important features. A recurring requirement not available in the GridView is to create groups and summaries. To create summaries we can easily code the RowDataBound event. Grouping is a more complex task, and involves more coding and debugging. But when we need to combine these two features the things really start to get messy. With this in mind I have implemented the GridViewHelper, as the name suggests, a helper class to aid the creation of groups and summaries.

[Online sample]

[Download source code]

Using the GridViewHelper

Below we will see some GridViewHelper samples. First we show the grid to which the groups and summaries will be created. The sample data comes from Northwind database, with a few modifications:

ShipRegionShipNameOrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,00NMOld World Delicatessen10922Teatime Chocolate Biscuits35US$ 4,50US$ 157,50NMOld World Delicatessen10925Inlagd Sill25US$ 19,00US$ 475,00NMOld World Delicatessen10925Filo Mix12US$ 7,00US$ 84,00ORSave-a-lot Markets10935Chai21US$ 18,00US$ 378,00ORSave-a-lot Markets10935Carnarvon Tigers4US$ 62,50US$ 250,00ORSave-a-lot Markets10935Tunnbröd8US$ 9,00US$ 72,00ORGourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00ORSplit Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,00ORSplit Rail Beer & Ale10961Lakkalikööri60US$ 18,00US$ 1.080,00ORLazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00NMOld World Delicatessen10981Côte de Blaye60US$ 263,50US$ 15.810,00NMWhite Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,00NMWhite Clover Markets10989Queso Cabrales15US$ 21,00US$ 315,00NMWhite Clover Markets10989Camembert Pierrot4US$ 9,65US$ 38,60


To create a summary for the ItemTotal column we need only the promised 2 lines of code:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Sum);
}


First we create the GridViewHelper setting the grid in which it will act in the constructor. Then we register the summary specifying the column name and the summary operation to be performed. The result is below:

ShipRegionShipNameOrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,00NMOld World Delicatessen10922Teatime Chocolate Biscuits35US$ 4,50US$ 157,50NMOld World Delicatessen10925Inlagd Sill25US$ 19,00US$ 475,00NMOld World Delicatessen10925Filo Mix12US$ 7,00US$ 84,00ORSave-a-lot Markets10935Chai21US$ 18,00US$ 378,00ORSave-a-lot Markets10935Carnarvon Tigers4US$ 62,50US$ 250,00ORSave-a-lot Markets10935Tunnbröd8US$ 9,00US$ 72,00ORGourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00ORSplit Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,00ORSplit Rail Beer & Ale10961Lakkalikööri60US$ 18,00US$ 1.080,00ORLazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00NMOld World Delicatessen10981Côte de Blaye60US$ 263,50US$ 15.810,00NMWhite Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,00NMWhite Clover Markets10989Queso Cabrales15US$ 21,00US$ 315,00NMWhite Clover Markets10989Camembert Pierrot4US$ 9,65US$ 38,60 US$ 20.550,10

 

In this sample a new line was added to display the summary. Another option is to use the footer row to display the summary instead of creating a new one. When a new row is added to the grid, only the required cells to display the summarized columns are created. Using the footer, all the cells are created. In case of group summaries, generation of all cells or only the needed cells is a group attribute.

Now we will create a group. The code is shown below:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterGroup("ShipRegion"truetrue);
    
helper.ApplyGroupSort();
}


The first parameter of RegisterGroup method defines the columns to which the group must be created. It's also possible to create a composite group, consisting of an array of columns. The second parameter specifies if the group is automatic. In this case a new row will be created automatically for the group header. The third parameter specifies if the group columns must be hidden. The ApplyGroupSort method sets the sort expression of the grid as being the group columns, in this case, ShipRegion. This is required to grouping works properly, except if the data comes ordered from database.

In the above sample the column ShipRegion have been hidden:

ShipNameOrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,00Old World Delicatessen10922Teatime Chocolate Biscuits35US$ 4,50US$ 157,50Old World Delicatessen10925Inlagd Sill25US$ 19,00US$ 475,00Old World Delicatessen10925Filo Mix12US$ 7,00US$ 84,00Old World Delicatessen10981Côte de Blaye60US$ 263,50US$ 15.810,00White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,00White Clover Markets10989Queso Cabrales15US$ 21,00US$ 315,00White Clover Markets10989Camembert Pierrot4US$ 9,65US$ 38,60ORSave-a-lot Markets10935Chai21US$ 18,00US$ 378,00Save-a-lot Markets10935Carnarvon Tigers4US$ 62,50US$ 250,00Save-a-lot Markets10935Tunnbröd8US$ 9,00US$ 72,00Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,00Split Rail Beer & Ale10961Lakkalikööri60US$ 18,00US$ 1.080,00Lazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00


Let's make something more interesting, let's add a summary to the created group. We need just one more line to register the summary to the group:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterGroup("ShipRegion"truetrue);
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Sum, "ShipRegion");
    
helper.ApplyGroupSort();
}


This time, the RegisterSummary method takes another parameter. The parameter specifies the name of the group to which the summary must be created. Group name is automatically generated from the group column names. If the group has only one column, group name will be the name of that column. If the group has more than one column, the group name will be the ordered concatenation of the columns that composes the group, joined with a plus sign ("+"): "ShipRegion+ShipName".

We can see below the grid with grouping and a summary for the group:

ShipNameOrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,00Old World Delicatessen10922Teatime Chocolate Biscuits35US$ 4,50US$ 157,50Old World Delicatessen10925Inlagd Sill25US$ 19,00US$ 475,00Old World Delicatessen10925Filo Mix12US$ 7,00US$ 84,00Old World Delicatessen10981Côte de Blaye60US$ 263,50US$ 15.810,00White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,00White Clover Markets10989Queso Cabrales15US$ 21,00US$ 315,00White Clover Markets10989Camembert Pierrot4US$ 9,65US$ 38,60 US$ 18.465,10ORSave-a-lot Markets10935Chai21US$ 18,00US$ 378,00Save-a-lot Markets10935Carnarvon Tigers4US$ 62,50US$ 250,00Save-a-lot Markets10935Tunnbröd8US$ 9,00US$ 72,00Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,00Split Rail Beer & Ale10961Lakkalikööri60US$ 18,00US$ 1.080,00Lazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00 US$ 2.085,00


It's possible to create more than one group in the grid, simulating a hierarchical grouping, as seen below:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterGroup("ShipRegion"truetrue);
    
helper.RegisterGroup("ShipName"truetrue);
    
helper.ApplyGroupSort();
}


Result:

OrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,0010922Teatime Chocolate Biscuits35US$ 4,50US$ 157,5010925Inlagd Sill25US$ 19,00US$ 475,0010925Filo Mix12US$ 7,00US$ 84,0010981Côte de Blaye60US$ 263,50US$ 15.810,00White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,0010989Queso Cabrales15US$ 21,00US$ 315,0010989Camembert Pierrot4US$ 9,65US$ 38,60ORLazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,0010961Lakkalikööri60US$ 18,00US$ 1.080,00Save-a-lot Markets10935Chai21US$ 18,00US$ 378,0010935Carnarvon Tigers4US$ 62,50US$ 250,0010935Tunnbröd8US$ 9,00US$ 72,00


Visualization is compromised when there is more than one group. GridViewHelper has events to allow easy implementation of visual or functional adjusts. The list of events follows below:

GroupStartOccurs when a new group starts, it means, when new values are found in the group column.GroupEndOccurs in the last row of the groupGroupHeaderOccurs when an automatic header row is added for the group. The event is not triggered if the group is not automatic.GroupSummaryOccurs when the summary row is generated for the group. The event is not triggered if the group is not automatic, but will be triggered if the group is a suppression group (will be seen later on).GeneralSummaryOccurs after the general summaries be calculated. If the summary is automatic the event occurs after the summary row be added and after the summary values be placed in the row.FooterDataBoundOccurs in the footer databinding.


With a few more lines of code we can improve the visual aspect of the grid:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterGroup("ShipRegion"truetrue);
    
helper.RegisterGroup("ShipName"truetrue);
    
helper.GroupHeader += new GroupEvent(helper_GroupHeader);
    
helper.ApplyGroupSort();
}
    
private void helper_GroupHeader(string groupName, object[] values, GridViewRow row)
{
    
if ( groupName == "ShipRegion" )
    {
        row.BackColor 
Color.LightGray;
        
row.Cells[0].Text "  " + row.Cells[0].Text;
    
}
    
else if (groupName == "ShipName")
    {
        row.BackColor 
Color.FromArgb(236236236);
        
row.Cells[0].Text "     " + row.Cells[0].Text;
    
}
}


The grid after the cosmetics:

OrderIdProductNameQuantityUnitPriceItemTotal  NM     Old World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,0010922Teatime Chocolate Biscuits35US$ 4,50US$ 157,5010925Inlagd Sill25US$ 19,00US$ 475,0010925Filo Mix12US$ 7,00US$ 84,0010981Côte de Blaye60US$ 263,50US$ 15.810,00     White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,0010989Queso Cabrales15US$ 21,00US$ 315,0010989Camembert Pierrot4US$ 9,65US$ 38,60  OR     Lazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00     Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00     Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,0010961Lakkalikööri60US$ 18,00US$ 1.080,00     Save-a-lot Markets10935Chai21US$ 18,00US$ 378,0010935Carnarvon Tigers4US$ 62,50US$ 250,0010935Tunnbröd8US$ 9,00US$ 72,00

 

More grouping options

There are two more interesting samples. The first presents a composite group. The second defines a suppress group, that has the same behavior of the sql GROUP BY clause. The repeating values are suppressed, and a summary operation is performed on the other columns.

Below we can see the code and the grid appearance for the composite group:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    string
[] cols = new string[2];
    
cols[0"ShipRegion";
    
cols[1"ShipName";
    
helper.RegisterGroup(cols, truetrue);
    
helper.ApplyGroupSort();
}

 

OrderIdProductNameQuantityUnitPriceItemTotalNM - Old World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,0010922Teatime Chocolate Biscuits35US$ 4,50US$ 157,5010925Inlagd Sill25US$ 19,00US$ 475,0010925Filo Mix12US$ 7,00US$ 84,0010981Côte de Blaye60US$ 263,50US$ 15.810,00NM - White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,0010989Queso Cabrales15US$ 21,00US$ 315,0010989Camembert Pierrot4US$ 9,65US$ 38,60OR - Lazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00OR - Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00OR - Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,0010961Lakkalikööri60US$ 18,00US$ 1.080,00OR - Save-a-lot Markets10935Chai21US$ 18,00US$ 378,0010935Carnarvon Tigers4US$ 62,50US$ 250,0010935Tunnbröd8US$ 9,00US$ 72,00


We can add a summary to the group. This time we will define an average operation and add a label to indicate the operation:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    string
[] cols = new string[2];
    
cols[0"ShipRegion";
    
cols[1"ShipName";
    
helper.RegisterGroup(cols, truetrue);
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Avg, "ShipRegion+ShipName");
    
helper.GroupSummary += new GroupEvent(helper_GroupSummary);
    
helper.ApplyGroupSort();
}

private void helper_GroupSummary(string groupName, object[] values, GridViewRow row)
{
    row.Cells[
0].HorizontalAlign HorizontalAlign.Right;
    
row.Cells[0].Text "Average";
}

 

OrderIdProductNameQuantityUnitPriceItemTotalNM - Old World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,0010922Teatime Chocolate Biscuits35US$ 4,50US$ 157,5010925Inlagd Sill25US$ 19,00US$ 475,0010925Filo Mix12US$ 7,00US$ 84,0010981Côte de Blaye60US$ 263,50US$ 15.810,00AverageUS$ 3.422,30NM - White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,0010989Queso Cabrales15US$ 21,00US$ 315,0010989Camembert Pierrot4US$ 9,65US$ 38,60AverageUS$ 451,20OR - Lazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00AverageUS$ 108,00OR - Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00AverageUS$ 155,00OR - Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,0010961Lakkalikööri60US$ 18,00US$ 1.080,00AverageUS$ 561,00OR - Save-a-lot Markets10935Chai21US$ 18,00US$ 378,0010935Carnarvon Tigers4US$ 62,50US$ 250,0010935Tunnbröd8US$ 9,00US$ 72,00AverageUS$ 233,33


The last sample will create a suppress group. It's important to mention that if a suppress group is defined, no other group may be created. In the same way, if there is already a group defined, we can't create a suppress group and an exception will be raised if we try it.

Below we can see the code and the grid appearance for the suppress group:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.SetSuppressGroup("ShipName");
    
helper.RegisterSummary("Quantity", SummaryOperation.Sum, "ShipName");
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Sum, "ShipName");
    
helper.ApplyGroupSort();
}

 

ShipRegionShipNameOrderIdProductNameQuantityUnitPriceItemTotal Lazy K Kountry Store  9 US$ 108,00 Gourmet Lanchonetes  20 US$ 155,00 Old World Delicatessen  147 US$ 17.111,50 White Clover Markets  59 US$ 1.353,60 Split Rail Beer & Ale  66 US$ 1.122,00 Save-a-lot Markets  33 US$ 700,00


No value is displayed for the columns that don't have a summary operation defined. This makes sense because GridViewHelper doesn't know how to proceed to summarize the values found in the group rows to a unique value. This reminds the certain known message:

"Column 'column_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."

It doesn't make sense to display the columns that don't have a summary operation, and to hide them we need to call a method:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    helper.SetSuppressGroup(rdBtnLstGroup.SelectedValue);
    
helper.RegisterSummary("Quantity", SummaryOperation.Sum, "ShipName");
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Sum, "ShipName");
    
helper.SetInvisibleColumnsWithoutGroupSummary();
    
helper.ApplyGroupSort();
}


I know, it's a big big name! The resulting grid can be seen below:

ShipNameQuantityItemTotalLazy K Kountry Store9US$ 108,00Gourmet Lanchonetes20US$ 155,00Old World Delicatessen147US$ 17.111,50White Clover Markets59US$ 1.353,60Split Rail Beer & Ale66US$ 1.122,00Save-a-lot Markets33US$ 700,00

 

Summary operations

GridViewHelper has 3 built-in summary operations: sum, average and row count. A very useful feature is the possibility of define custom summary operations. To achieve this we need to provide two methods to the GridViewHelper. A method will be called for each row found in the grid (or group) and the other will be called to retrieve the result of the summary operation. Below we have a sample of a custom summary operation. The semi-dummy operation will return the minimum value found:

private List<int> mQuantities = new List<int>();

protected void 
Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterSummary("Quantity", SaveQuantity, GetMinQuantity);
}

private void SaveQuantity(string column, string group, object value)
{
    mQuantities.Add(Convert.ToInt32(
value));
}

private object GetMinQuantity(string column, string group)
{
    
int[] qArray = new int[mQuantities.Count];
    
mQuantities.CopyTo(qArray);
    
Array.Sort(qArray);
    return 
qArray[0];
}


In the code above we can see the required methods signatures. Both receive the summarized group and column names. If the summary is not relative to a group, the group parameter will be null. The method that is called for each row found in the grid, receives also the value of the column in the current row.

The resulting grid can be seen below :

ShipRegionShipNameOrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,00NMOld World Delicatessen10922Teatime Chocolate Biscuits35US$ 4,50US$ 157,50NMOld World Delicatessen10925Inlagd Sill25US$ 19,00US$ 475,00NMOld World Delicatessen10925Filo Mix12US$ 7,00US$ 84,00ORSave-a-lot Markets10935Chai21US$ 18,00US$ 378,00ORSave-a-lot Markets10935Carnarvon Tigers4US$ 62,50US$ 250,00ORSave-a-lot Markets10935Tunnbröd8US$ 9,00US$ 72,00ORGourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00ORSplit Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,00ORSplit Rail Beer & Ale10961Lakkalikööri60US$ 18,00US$ 1.080,00ORLazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00NMOld World Delicatessen10981Côte de Blaye60US$ 263,50US$ 15.810,00NMWhite Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,00NMWhite Clover Markets10989Queso Cabrales15US$ 21,00US$ 315,00NMWhite Clover Markets10989Camembert Pierrot4US$ 9,65US$ 38,60 4 

 

Limitations

In one sample we said that we can simulate a hierarchical grouping. Although the grid appears to present a hierarchical grouping, the actual implementation isn't hierarchical. There's no group or subgroup. There are only sequentially registered groups. This becomes a problem if we need to create a summary for an inner group. Below we can see what happens in this situation:

protected void Page_Load(object sender, EventArgs e)
{
    GridViewHelper helper 
= new GridViewHelper(this.GridView1);
    
helper.RegisterGroup("ShipRegion"truetrue);
    
helper.RegisterGroup("ShipName"truetrue);
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Sum, "ShipName");
    
helper.RegisterSummary("ItemTotal", SummaryOperation.Sum);
    
helper.GroupSummary += new GroupEvent(helper_Bug);
    
helper.ApplyGroupSort();
}

private void helper_Bug(string groupName, object[] values, GridViewRow row)
{
    
if (groupName == nullreturn;

    
row.BackColor Color.Bisque;
    
row.Cells[0].HorizontalAlign HorizontalAlign.Center;
    
row.Cells[0].Text "[ Summary for " + groupName + " " + values[0] + " ]";
}

 

OrderIdProductNameQuantityUnitPriceItemTotalNMOld World Delicatessen10922Alice Mutton15US$ 39,00US$ 585,0010922Teatime Chocolate Biscuits35US$ 4,50US$ 157,5010925Inlagd Sill25US$ 19,00US$ 475,0010925Filo Mix12US$ 7,00US$ 84,0010981Côte de Blaye60US$ 263,50US$ 15.810,00[ Summary for ShipName Old World Delicatessen ]US$ 17.111,50White Clover Markets10989Grandma's Boysenberry Spread40US$ 25,00US$ 1.000,0010989Queso Cabrales15US$ 21,00US$ 315,0010989Camembert Pierrot4US$ 9,65US$ 38,60OR[ Summary for ShipName White Clover Markets ]US$ 1.353,60Lazy K Kountry Store10969Spegesild9US$ 12,00US$ 108,00[ Summary for ShipName Lazy K Kountry Store ]US$ 108,00Gourmet Lanchonetes10959Rhönbräu Klosterbier20US$ 7,75US$ 155,00[ Summary for ShipName Gourmet Lanchonetes ]US$ 155,00Split Rail Beer & Ale10961Filo Mix6US$ 7,00US$ 42,0010961Lakkalikööri60US$ 18,00US$ 1.080,00[ Summary for ShipName Split Rail Beer & Ale ]US$ 1.122,00Save-a-lot Markets10935Chai21US$ 18,00US$ 378,0010935Carnarvon Tigers4US$ 62,50US$ 250,0010935Tunnbröd8US$ 9,00US$ 72,00[ Summary for ShipName Save-a-lot Markets ]US$ 700,00 US$ 20.550,10


As we can see, the summary is created after the header of the outer group. This occurs because the event sequence is:

Group1_Start
Group1_End
Group2_Start
Group2_End

To a hierarchical grouping, the event sequence should be:

Group1_Start
Group2_Start
Group2_End
Group1_End

 

Implementation

GridViewHelper was implemented as a standalone class instead of an inherited class. This makes possible to use the GridViewHelper with any GridView, and doesn't force the developer to inherit a specific GridView, what could affect the classes design. There's another four classes in the solution: GridViewSummary, GridViewGroup, GridViewSummaryList and GridViewGroupList. The "list" classes were created to allow access by a string indexer: helper.GeneralSummaries["ItemTotal"].Value.

When the GridViewHelper is created, a reference to the target GridView is saved, and the RowDataBound event is bound to the method that does the hard work:

public GridViewHelper(GridView grd, bool useFooterForGeneralSummaries, SortDirection groupSortDirection)
{
    
this.mGrid grd;
    this
.useFooter useFooterForGeneralSummaries;
    this
.groupSortDir groupSortDirection;
    this
.mGeneralSummaries = new GridViewSummaryList();
    this
.mGroups = new GridViewGroupList();
    this
.mGrid.RowDataBound += new GridViewRowEventHandler(RowDataBoundHandler);
}

Some methods used internally by the GridViewHelper were defined public because they provide some useful features that may be needed for some customizations. There are a few other options that wasn't shown in the samples but that can be easily verified with Visual Studio intellisense.

 

Known issues

Performance might be compromised with the excessive boxing and unboxing of value types. To solve this we could implement the built-in summary operations with generics, but this is not easy as we would like, as can be seen at Using Generics for Calculations. Another possibility: Operator Overloading with Generics. In real life this will not affect the application except if there are a million rows, or if there are thousand of users grouping and summarizing data concurrently.

The online sample keeps the GridView EnableViewState false. This is required because when EnableViewState is true, if the page is in a PostBack the GridView will be rebuilt from the ViewState, and won't trigger the RowDataBound event. We can securely disable the ViewState in ASP.Net 2.0 because the ControlState will still be saved.

Feel free to send comments and to implement more features to the GridViewHelper. Let me know and I will update the article.

Site about multilingual website development launched: www.MultiLanguageWebSites.com

原创粉丝点击