Display sum of Column total in ASP.Net GridView Footer using RowDataBound event in C# and VB.Net

Last Reply 3 months ago By pandeyism

Posted 3 months ago

I have negative value in column of gridview due to it my column footer sum is not coming correct.

i have used this for footer total 

        Decimal TotalValue = 0;            
        protected void GVallbb_RowDataBound(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                Label lbl = (Label)e.Row.FindControl("itemQty");
                TotalValue += Convert.ToDecimal(lbl.Text);
            else if (e.Row.RowType == DataControlRowType.Footer)

                Label lbl = (Label)e.Row.FindControl("lblTotal");
                lbl.Text = TotalValue.ToString();


total is coming incorrect.

Posted 3 months ago

Hi akhter,

Refer below sample.


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ShowFooter="true"
        <asp:BoundField DataField="ItemName" HeaderText="ItemName" />
        <asp:TemplateField HeaderText="Price">
                <asp:Label Text='<%#Eval("Price") %>' ID="lblPrice" runat="server" />
                <asp:Label Text='<%#Eval("Total") %>' ID="lblTotal" runat="server" />



using System.Data;


Imports System.Data



protected void Page_Load(object sender, EventArgs e)
    if (!this.IsPostBack)
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[] { new DataColumn("ItemName", typeof(string)), new DataColumn("Price", typeof(decimal)) });
        dt.Rows.Add("A", 10);
        dt.Rows.Add("B", -20);
        dt.Rows.Add("C", 5);
        dt.Rows.Add("D", -10);
        GridView1.DataSource = dt;

decimal total = 0;
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    if (e.Row.RowType == DataControlRowType.DataRow)
        Label lblPrice = (e.Row.FindControl("lblPrice") as Label);
        total += Convert.ToDecimal(lblPrice.Text);
    if (e.Row.RowType == DataControlRowType.Footer)
        Label lblTotal = (e.Row.FindControl("lblTotal") as Label);
        lblTotal.Text = "Total : " + total.ToString();


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn() {New DataColumn("ItemName", GetType(String)), New DataColumn("Price", GetType(Decimal))})
        dt.Rows.Add("A", 10)
        dt.Rows.Add("B", -20)
        dt.Rows.Add("C", 5)
        dt.Rows.Add("D", -10)
        GridView1.DataSource = dt
    End If
End Sub

Private total As Decimal = 0

Protected Sub GridView1_OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) Handles GridView1.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        Dim lblPrice As Label = (TryCast(e.Row.FindControl("lblPrice"), Label))
        total += Convert.ToDecimal(lblPrice.Text)
    End If

    If e.Row.RowType = DataControlRowType.Footer Then
        Dim lblTotal As Label = (TryCast(e.Row.FindControl("lblTotal"), Label))
        lblTotal.Text = "Total : " & total.ToString()
    End If
End Sub