Reader Level:
ARTICLE

Grouping in ASP.NET Gridview

Posted by Prabhat Kumar Articles | ASP.NET Programming January 29, 2013
In this article, I will explain how to implement grouping and calculate subtotal and grand total for each group in asp.net gridview.
  • 2
  • 0
  • 49508
Download Files:
 

Introduction

In this article, I will explain how to implement a grouping and calculate subtotal and a grand total for each group in ASP.Net GridView. To do this I used the RowCreated and RowDatabound events of a GridView. I used an XML file as the data source.

Objective

To understand grouping in an ASP.Net GridView.

Using code

UI

Figure 2.jpg

Items are grouped based on the customer name. For each customer, a total has been calculated and at the end a grand total has been calculated.

CODE

The main logic is in the RowCreated and RowDataBound events of the GridView. While iterating through all the rows I am catching the customer id and checking the other rows. When the customer id has been changed I am displaying the subtotal and resetting the subtotal. In the same way I am calculating the grand total. To display in a group format I am adding one row in a GridView once the customer id has been changed. In this row I display the subtotal. To display the heading I am adding a new row to the GridView. Please refer to the attachment to see the complete code.

Design

In design I just bound XML data source attributes. Please refer attachment to see complete code.

  <asp:GridView ID="grdViewOrders" CssClass="serh-grid" runat="server" AutoGenerateColumns="False"

                        TabIndex="1" Width="100%" CellPadding="4" ForeColor="Black" GridLines="Vertical"

                        OnRowDataBound="grdViewOrders_RowDataBound" OnRowCommand="grdViewOrders_RowCommand"

                        OnRowCreated="grdViewOrders_RowCreated" BackColor="White" BorderColor="#DEDFDE"

                        BorderStyle="None" BorderWidth="1px">

                        <Columns>

                            <asp:BoundField DataField="OrderID" HeaderText="OrderID" SortExpression="OrderID">

                            </asp:BoundField>

                            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />

                            <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />

                            <asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />

                            <asp:BoundField DataField="Discount" HeaderText="Discount" SortExpression="Discount" />

                            <asp:BoundField DataField="Amount" HeaderText="Amount" SortExpression="Amount" />

                        </Columns>

                        <FooterStyle BackColor="#CCCC99" />

                        <SelectedRowStyle CssClass="grid-sltrow" />

                        <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" BorderStyle="Solid"

                            BorderWidth="1px" BorderColor="Black" />

                    </asp:GridView>

Summary

After reading this article, you have learned:

  1. How to implement grouping in an ASP.Net GridView

  2. How to calculate subtotal and grand total

  3. How to select a full row in a GridView

  4. Retrieving selected row data

COMMENT USING

Trending up