Grouping in ASP.NET Gridview

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

Up Next
    Ebook Download
    View all
    Learn
    View all