Aditya
Aditya

Reputation: 85

How can i get sum of particular column in grid view?

I want to show the sum of particular column in grid view and show it on top of that column. Note: Data is coming from database. This is my gridview

 <asp:GridView ID="SalesSummaryGrid" OnPageIndexChanging="SalesSummaryGrid_PageIndexChanging" OnRowDataBound="SalesSummaryGrid_RowDataBound" 
                        PageSize="10" CssClass="mydatagrid" HeaderStyle-CssClass="header"  RowStyle-CssClass="rows"
                      PagerStyle-CssClass="pager" AllowPaging="true" runat="server" AutoGenerateColumns="false">
                       <Columns>
                          <asp:TemplateField HeaderText="Apr">
                             <ItemTemplate>
                                <asp:Label ID="AprMonth" runat="server" Text='<%# Eval("Apr") %>' CssClass="labelstyle"></asp:Label>
                             </ItemTemplate>
                          </asp:TemplateField>
                       <Columns>
            </asp:GridView>

Upvotes: 1

Views: 1220

Answers (1)

Praneet Nadkar
Praneet Nadkar

Reputation: 813

In the RowDataBound for Footer, you can do FindControl on the HeaderRow of the GridView to find Labels in the Header. Then you can set the values of these.

foreach (GridViewRow gvr in GridView4.Rows) {
    if (gvr.RowType == DataControlRowType.DataRow) {
        decimal fee = Convert.ToDecimal(gvr.Cells(5).Text);
        feeFooter += fee;
    }
}
GridView4.Columns(3).HeaderText = feeFooter;

Source : https://forums.asp.net/t/1812651.aspx?GridView+Totals+in+Header

Upvotes: 3

Related Questions