12,064,855 members (42,127 online)
GridView is one of the most popular controls to display data in a tabular format. But for better analysis, it requires to show the data in some summarized form like Group, Pivot, Graphs, Charts, etc. This articles presents a simplified way to group your data with the appropriate aggregate functions so that you can enhance your reports with a great ease. Facility to customize the group header and footer in your own way becomes the essential part when you are grouping your data. The control shown in the article has taken care of this requirement as well. If you are looking for the grouping grid in Asp.Net MVC, please have a look over http://www.codeproject.com/Articles/761687/Grouping-Gridview-in-Asp-Net-MVC .
Below is a screenshot of grouped data in a
GroupGridView control is a custom control derived from
GridView. The primary purpose for this control is to implement the grouping feature in the
GridView. To accommodate the customized group header and footer,
GroupContainer class is created which is derived from
INamingContainer. This class is provided with various aggregate functions that can be applied to each group in your
GridView. The useful methods in
Average(ColumnName): Returns the average of the data present in the group for the given column
Count(ColumnName): Returns the total number of rows present in the group for the given column
First(ColumnName): Returns the first element of the given column in the group
Last(ColumnName): Returns the last element of the given column in the group
Max(ColumnName): Returns the maximum value of the data present in the group for the given column
Min(ColumnName): Returns the minimum value of the data present in the group for the given column
Sum(ColumnName): Returns the sum of data present in the group for the given column
GetValue(DataColumn, PropertyColumn, MatchingValue): Returns the value of the
PropertyColumnis equal to the
The logic behind grouping the data in
GridView can be divided into three parts:
GroupFooterTemplate. This Template needs to be instantiated for each group present in your
DataSource. This has been done by overriding the
OnRowCreated()method of the
GridViewand added "
group" attribute and its value to each row of the table. To align
Footerat proper place, JQuery has been used.
RenderClientSideGroup()method to understand the JQuery code used to
FadeOutthe Group(s). You don't need to learn JQuery to use this control.
Using the code is simple. The control is provided with two additional Template Containers:
GroupFooterTemplate. Both of these Template containers provide you the facility to write your own custom HTML code and use Aggregation logic to format your
Footer of the each group of
Below is the sample aspx markup which you can write to make this control work (don't forget to include JQuery file in your code):
To make this control work, don't forget to set
AllowGrouping property to true. Moreover,
GroupColumnName property must be set to the correct value.
Based on various feedback/emails of the readers, I have included some more options in this version of GroupGridView Control. Some of them are as follows:
Along with grouping the data in
GridView, the sample will also provide the following things to the beginners: