内容摘自:http://www.agrinei.com/gridviewhelper/gridviewhelper_en.htm
GridView在DataGrid上有很多改进,但仍然缺少一些非常重要的特性。GridView中不可使用的重复需求是创建组和汇总。要创建总结,我们可以轻松地编写RowDataBound事件。分组是一个更复杂的任务,需要更多的编码和调试。但当我们需要结合这两个特性时,事情就会变得一团糟。考虑到这一点,我已经实现了GridViewHelper,顾名思义,它是帮助创建组和总结的助手类。
使用GridViewHelper
下面我们将看到一些GridViewHelper示例。首先,我们展示将创建组和汇总的网格。样本数据来自Northwind数据库,有一些修改:
ShipRegion | ShipName | OrderId | ProductName | Quantity | UnitPrice | ItemTotal |
---|---|---|---|---|---|---|
NM | Old World Delicatessen | 10922 | Alice Mutton | 15 | US$ 39,00 | US$ 585,00 |
NM | Old World Delicatessen | 10922 | Teatime Chocolate Biscuits | 35 | US$ 4,50 | US$ 157,50 |
NM | Old World Delicatessen | 10925 | Inlagd Sill | 25 | US$ 19,00 | US$ 475,00 |
NM | Old World Delicatessen | 10925 | Filo Mix | 12 | US$ 7,00 | US$ 84,00 |
OR | Save-a-lot Markets | 10935 | Chai | 21 | US$ 18,00 | US$ 378,00 |
OR | Save-a-lot Markets | 10935 | Carnarvon Tigers | 4 | US$ 62,50 | US$ 250,00 |
OR | Save-a-lot Markets | 10935 | Tunnbröd | 8 | US$ 9,00 | US$ 72,00 |
OR | Gourmet Lanchonetes | 10959 | Rhönbräu Klosterbier | 20 | US$ 7,75 | US$ 155,00 |
OR | Split Rail Beer & Ale | 10961 | Filo Mix | 6 | US$ 7,00 | US$ 42,00 |
OR | Split Rail Beer & Ale | 10961 | Lakkalikööri | 60 | US$ 18,00 | US$ 1.080,00 |
OR | Lazy K Kountry Store | 10969 | Spegesild | 9 | US$ 12,00 | US$ 108,00 |
NM | Old World Delicatessen | 10981 | Côte de Blaye | 60 | US$ 263,50 | US$ 15.810,00 |
NM | White Clover Markets | 10989 | Grandma's Boysenberry Spread | 40 | US$ 25,00 | US$ 1.000,00 |
NM | White Clover Markets | 10989 | Queso Cabrales | 15 | US$ 21,00 | US$ 315,00 |
NM | White Clover Markets | 10989 | Camembert Pierrot | 4 | US$ 9,65 | US$ 38,60 |
ShipRegion | ShipName | OrderId | ProductName | Quantity | UnitPrice | ItemTotal |
---|---|---|---|---|---|---|
NM | Old World Delicatessen | 10922 | Alice Mutton | 15 | US$ 39,00 | US$ 585,00 |
NM | Old World Delicatessen | 10922 | Teatime Chocolate Biscuits | 35 | US$ 4,50 | US$ 157,50 |
NM | Old World Delicatessen | 10925 | Inlagd Sill | 25 | US$ 19,00 | US$ 475,00 |
NM | Old World Delicatessen | 10925 | Filo Mix | 12 | US$ 7,00 | US$ 84,00 |
OR | Save-a-lot Markets | 10935 | Chai | 21 | US$ 18,00 | US$ 378,00 |
OR | Save-a-lot Markets | 10935 | Carnarvon Tigers | 4 | US$ 62,50 | US$ 250,00 |
OR | Save-a-lot Markets | 10935 | Tunnbröd | 8 | US$ 9,00 | US$ 72,00 |
OR | Gourmet Lanchonetes | 10959 | Rhönbräu Klosterbier | 20 | US$ 7,75 | US$ 155,00 |
OR | Split Rail Beer & Ale | 10961 | Filo Mix | 6 | US$ 7,00 | US$ 42,00 |
OR | Split Rail Beer & Ale | 10961 | Lakkalikööri | 60 | US$ 18,00 | US$ 1.080,00 |
OR | Lazy K Kountry Store | 10969 | Spegesild | 9 | US$ 12,00 | US$ 108,00 |
NM | Old World Delicatessen | 10981 | Côte de Blaye | 60 | US$ 263,50 | US$ 15.810,00 |
NM | White Clover Markets | 10989 | Grandma's Boysenberry Spread | 40 | US$ 25,00 | US$ 1.000,00 |
NM | White Clover Markets | 10989 | Queso Cabrales | 15 | US$ 21,00 | US$ 315,00 |
NM | White Clover Markets | 10989 | Camembert Pierrot | 4 | US$ 9,65 | US$ 38,60 |
要为ItemTotal列创建一个摘要,我们只需要承诺的2行代码:
{
GridViewHelper helper = new GridViewHelper( this .GridView1) ;
helper.RegisterSummary( "ItemTotal" , SummaryOperation.Sum) ;
}
首先,我们创建GridViewHelper设置网格,在该网格中它将在构造函数中执行。然后,我们注册摘要,说明要执行的列名和摘要操作。下面的结果是:
ShipRegion | ShipName | OrderId | ProductName | Quantity | UnitPrice | ItemTotal |
---|---|---|---|---|---|---|
NM | Old World Delicatessen | 10922 | Alice Mutton | 15 | US$ 39,00 | US$ 585,00 |
NM | Old World Delicatessen | 10922 | Teatime Chocolate Biscuits | 35 | US$ 4,50 | US$ 157,50 |
NM | Old World Delicatessen | 10925 | Inlagd Sill | 25 | US$ 19,00 | US$ 475,00 |
NM | Old World Delicatessen | 10925 | Filo Mix | 12 | US$ 7,00 | US$ 84,00 |
OR | Save-a-lot Markets | 10935 | Chai | 21 | US$ 18,00 | US$ 378,00 |
OR | Save-a-lot Markets | 10935 | Carnarvon Tigers | 4 | US$ 62,50 | US$ 250,00 |
OR | Save-a-lot Markets | 10935 | Tunnbröd | 8 | US$ 9,00 | US$ 72,00 |
OR | Gourmet Lanchonetes | 10959 | Rhönbräu Klosterbier | 20 | US$ 7,75 | US$ 155,00 |
OR | Split Rail Beer & Ale | 10961 | Filo Mix | 6 | US$ 7,00 | US$ 42,00 |
OR | Split Rail Beer & Ale | 10961 | Lakkalikööri | 60 | US$ 18,00 | US$ 1.080,00 |
OR | Lazy K Kountry Store | 10969 | Spegesild | 9 | US$ 12,00 | US$ 108,00 |
NM | Old World Delicatessen | 10981 | Côte de Blaye | 60 | US$ 263,50 | US$ 15.810,00 |
NM | White Clover Markets | 10989 | Grandma's Boysenberry Spread | 40 | US$ 25,00 | US$ 1.000,00 |
NM | White Clover Markets | 10989 | Queso Cabrales | 15 | US$ 21,00 | US$ 315,00 |
NM | White Clover Markets | 10989 | Camembert Pierrot | 4 | US$ 9,65 | US$ 38,60 |
US$ 20.550,10 |
在这个示例中,添加了一条新行来显示摘要。另一种方法是使用footer row来显示摘要,而不是创建一个新的。当在网格中添加一个新行时,只创建需要显示汇总列的所需单元格。使用footer,创建所有的单元格。在组摘要的情况下,所有单元的生成或仅需要的单元格是一个组属性。
现在我们将创建一个组。代码如下:
{
GridViewHelper helper = new GridViewHelper( this .GridView1) ;
helper.RegisterGroup( "ShipRegion", true , true ) ;
helper.ApplyGroupSort() ;
}
在上面的样本中,ShipRegion的专栏已经被隐藏了:
ShipName | OrderId | ProductName | Quantity | UnitPrice | ItemTotal |
---|---|---|---|---|---|
NM | |||||
Old World Delicatessen | 10922 | Alice Mutton | 15 | US$ 39,00 | US$ 585,00 |
Old World Delicatessen | 10922 | Teatime Chocolate Biscuits | 35 | US$ 4,50 | US$ 157,50 |
Old World Delicatessen | 10925 | Inlagd Sill | 25 | US$ 19,00 | US$ 475,00 |
Old World Delicatessen | 10925 | Filo Mix | 12 | US$ 7,00 | US$ 84,00 |
Old World Delicatessen | 10981 | Côte de Blaye | 60 | US$ 263,50 | US$ 15.810,00 |
White Clover Markets | 10989 | Grandma's Boysenberry Spread | 40 | US$ 25,00 | US$ 1.000,00 |
White Clover Markets | 10989 | Queso Cabrales | 15 | US$ 21,00 | US$ 315,00 |
White Clover Markets | 10989 | Camembert Pierrot | 4 | US$ 9,65 | US$ 38,60 |
OR | |||||
Save-a-lot Markets | 10935 | Chai | 21 | US$ 18,00 | US$ 378,00 |
Save-a-lot Markets | 10935 | Carnarvon Tigers | 4 | US$ 62,50 | US$ 250,00 |
Save-a-lot Markets | 10935 | Tunnbröd | 8 | US$ 9,00 | US$ 72,00 |
Gourmet Lanchonetes | 10959 | Rhönbräu Klosterbier | 20 | US$ 7,75 | US$ 155,00 |
Split Rail Beer & Ale | 10961 | Filo Mix | 6 | US$ 7,00 | US$ 42,00 |
Split Rail Beer & Ale | 10961 | Lakkalikööri | 60 | US$ 18,00 | US$ 1.080,00 |
Lazy K Kountry Store | 10969 | Spegesild | 9 | US$ 12,00 | US$ 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:
{
GridViewHelper helper = new GridViewHelper( this .GridView1) ;
helper.RegisterGroup( "ShipRegion" , true , true ) ;
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:
ShipName | OrderId | ProductName | Quantity | UnitPrice | ItemTotal |
---|---|---|---|---|---|
NM | |||||
Old World Delicatessen | 10922 | Alice Mutton | 15 | US$ 39,00 | US$ 585,00 |
Old World Delicatessen | 10922 | Teatime Chocolate Biscuits | 35 | US$ 4,50 | US$ 157,50 |
Old World Delicatessen | 10925 | Inlagd Sill | 25 | US$ 19,00 | US$ 475,00 |
Old World Delicatessen | 10925 | Filo Mix | 12 | US$ 7,00 | US$ 84,00 |
Old World Delicatessen | 10981 | Côte de Blaye | 60 | US$ 263,50 | US$ 15.810,00 |
White Clover Markets | 10989 | Grandma's Boysenberry Spread | 40 | US$ 25,00 | US$ 1.000,00 |
White Clover Markets | 10989 | Queso Cabrales | 15 | US$ 21,00 | US$ 315,00 |
White Clover Markets | 10989 | Camembert Pierrot | 4 | US$ 9,65 | US$ 38,60 |
US$ 18.465,10 | |||||
OR | |||||
Save-a-lot Markets | 10935 | Chai | 21 | US$ 18,00 | US$ 378,00 |
Save-a-lot Markets | 10935 | Carnarvon Tigers | 4 | US$ 62,50 | US$ 250,00 |
Save-a-lot Markets | 10935 | Tunnbröd | 8 | US$ 9,00 | US$ 72,00 |
Gourmet Lanchonetes | 10959 | Rhönbräu Klosterbier | 20 | US$ 7,75 | US$ 155,00 |
Split Rail Beer & Ale | 10961 | Filo Mix | 6 | US$ 7,00 | US$ 42,00 |
Split Rail Beer & Ale | 10961 | Lakkalikööri | 60 | US$ 18,00 | US$ 1.080,00 |
Lazy K Kountry Store | 10969 | Spegesild | 9 | US$ 12,00 | US$ 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:
{
GridViewHelper helper = new GridViewHelper( this .GridView1) ;
helper.RegisterGroup( "ShipRegion" , true , true ) ;
helper.RegisterGroup( "ShipName" , true , true ) ;
helper.ApplyGroupSort() ;
}
Result:
OrderId | ProductName | Quantity | UnitPrice | ItemTotal |
---|---|---|---|---|
NM | ||||
Old World Delicatessen | ||||
10922 | Alice Mutton | 15 | US$ 39,00 | US$ 585,00 |
10922 | Teatime Chocolate Biscuits | 35 | US$ 4,50 | US$ 157,50 |
10925 | Inlagd Sill | 25 | US$ 19,00 | US$ 475,00 |
10925 | Filo Mix | 12 | US$ 7,00 | US$ 84,00 |
10981 | Côte de Blaye | 60 | US$ 263,50 | US$ 15.810,00 |
White Clover Markets | ||||
10989 | Grandma's Boysenberry Spread | 40 | US$ 25,00 | US$ 1.000,00 |
10989 | Queso Cabrales | 15 | US$ 21,00 | US$ 315,00 |
10989 | Camembert Pierrot | 4 | US$ 9,65 | US$ 38,60 |
OR | ||||
Lazy K Kountry Store | ||||
10969 | Spegesild | 9 | US$ 12,00 | US$ 108,00 |
Gourmet Lanchonetes | ||||
10959 | Rhönbräu Klosterbier | 20 | US$ 7,75 | US$ 155,00 |
Split Rail Beer & Ale | ||||
10961 | Filo Mix | 6 | US$ 7,00 | US$ 42,00 |
10961 | Lakkalikööri | 60 | US$ 18,00 | US$ 1.080,00 |
Save-a-lot Markets | ||||
10935 | Chai | 21 | US$ 18,00 | US$ 378,00 |
10935 | Carnarvon Tigers | 4 | US$ 62,50 | US$ 250,00 |
10935 | Tunnbröd | 8 | US$ 9,00 | US$ 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:
GroupStart | Occurs when a new group starts, it means, when new values are found in the group column. |
GroupEnd | Occurs in the last row of the group |
GroupHeader | Occurs when an automatic header row is added for the group. The event is not triggered if the group is not automatic. |
GroupSummary | Occurs 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). |
GeneralSummary | Occurs after t |