Add multiple Footer in GridView from code behind in ASP.Net

Last Reply 5 months ago By dharmendr

Posted 5 months ago

How to add multiple footer from code behind

                total2 = Convert.ToDecimal(Session["total"].ToString()) + Convert.ToDecimal(total.Text.ToString());
                dt.Rows.Add(product.SelectedItem.ToString(), qty.Text.ToString(), price.Text.ToString(), total.Text.ToString());
                qty.Text = "0";
                price.Text = "0";
                total.Text = "0";
                cgst.Text = "0";
                sgst.Text = "0";
                product.SelectedIndex = product.Items.IndexOf(product.Items.FindByText("select"));
                Session["dtInSession"] = dt;
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.FooterRow.Cells[2].Text = "Grand Total(Rs.)";                
                GridView1.FooterRow.Cells[3].Text = total2.ToString();                
                Session["total"] = total2.ToString();
            } 
        }
        private void gridVIEWData()
        {
            dt.Columns.AddRange(new DataColumn[4] {
                new DataColumn("Product Name"),
                new DataColumn("Quantity"),
                new DataColumn("Price(Rs.)"),                
                new DataColumn("Total Price(Rs.)")
              });
            Session["dtInSession"] = dt;
        }
Posted 5 months ago
Hi @JanviDarji,
Please try the following

How to add or remove multiple Css Class names from CssClass property from code behind in ASP.Net

It might help you.

Cheers Andrea.

Posted 5 months ago

Hi JanviDarji,

Please explain in details about multiple footer.

Share your Html.


Posted 5 months ago Modified on 5 months ago

Hi JanviDarji,

Check this example. Now please take its reference and correct your code.

HTML

<asp:GridView ID="GridView1" ShowFooter="true" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Product Name" />
        <asp:BoundField DataField="Quantity" HeaderText="Quantity" />
        <asp:BoundField DataField="Price" HeaderText="Price(Rs.)" />
        <asp:BoundField DataField="Total" HeaderText="Total Price(Rs.)" />
    </Columns>
</asp:GridView>

Namespaces

C#

using System.Data;

VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        GetData();
        BindgridView();
    }
}

private void BindgridView()
{
    DataTable dt = Session["dtInSession"] as DataTable;
    GridView1.DataSource = dt;
    GridView1.DataBind();
    decimal total = dt.AsEnumerable().Sum(row => Convert.ToDecimal(row["Total"]));
    GridView1.FooterRow.Cells[2].Text = "Grand Total(Rs.)";
    GridView1.FooterRow.Cells[3].Text = total.ToString("N2");
}

private void GetData()
{
    DataTable dt = new DataTable();
    dt.Columns.AddRange(new DataColumn[4] {
                        new DataColumn("Name"),
                        new DataColumn("Quantity"),
                        new DataColumn("Price"),         
                        new DataColumn("Total") });
    dt.Rows.Add("Product 1", 5, 150, 750);
    dt.Rows.Add("Product 2", 8, 100, 800);
    dt.Rows.Add("Product 3", 12, 120, 1440);
    dt.Rows.Add("Product 4", 15, 80, 1200);
    Session["dtInSession"] = dt;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        GetData()
        BindgridView()
    End If
End Sub

Private Sub BindgridView()
    Dim dt As DataTable = TryCast(Session("dtInSession"), DataTable)
    GridView1.DataSource = dt
    GridView1.DataBind()
    Dim total As Decimal = dt.AsEnumerable().Sum(Function(row) Convert.ToDecimal(row("Total")))
    GridView1.FooterRow.Cells(2).Text = "Grand Total(Rs.)"
    GridView1.FooterRow.Cells(3).Text = total.ToString("N2")
End Sub

Private Sub GetData()
    Dim dt As DataTable = New DataTable()
    dt.Columns.AddRange(New DataColumn(3) {
                        New DataColumn("Name"),
                        New DataColumn("Quantity"),
                        New DataColumn("Price"),
                        New DataColumn("Total")})
    dt.Rows.Add("Product 1", 5, 150, 750)
    dt.Rows.Add("Product 2", 8, 100, 800)
    dt.Rows.Add("Product 3", 12, 120, 1440)
    dt.Rows.Add("Product 4", 15, 80, 1200)
    Session("dtInSession") = dt
End Sub

Screenshot