locked
How to create a Totals row without using the Footer row in GridView? RRS feed

  • Question

  • User476110528 posted

    I need to sum multiple columns, but I can't use the footer row like I did on a previous page. The footer row is already being used for Add New row data functionality. I would like to create a total row as the last row that contains the totals of all the columns with the footer row just below it. Can anyone help? Thanks!

    Thursday, October 10, 2019 3:44 PM

All replies

  • User283571144 posted

    Hi Jdavila,

    I need to sum multiple columns, but I can't use the footer row like I did on a previous page. The footer row is already being used for Add New row data functionality. I would like to create a total row as the last row that contains the totals of all the columns with the footer row just below it. Can anyone help? Thanks!

    According to your description, I suggest you could try to use databound to calculcate all the column and add it to the last row by using table.addat function.

    More details, you could refer to below codes:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm14.aspx.cs" Inherits="TestApplicationWithDatabase.WebForm14" %>
    
     
    
    <!DOCTYPE html>
    
     
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" DataKeyNames="First" ShowFooter="True" >
                    <Columns>
                        <asp:TemplateField HeaderText="First" SortExpression="First">
                            <EditItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("First") %>'></asp:Label>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("First") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Second" SortExpression="Second">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Second") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label2" runat="server" Text='<%# Bind("Second") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Third" SortExpression="Third">
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Third") %>'></asp:TextBox>
                            </EditItemTemplate>
                            <ItemTemplate>
                                <asp:Label ID="Label3" runat="server" Text='<%# Bind("Third") %>'></asp:Label>
                            </ItemTemplate>
                            <FooterTemplate>
                             <asp:Label ID="Label4" runat="server" Text='aa'></asp:Label>
                            </FooterTemplate>
                        </asp:TemplateField>
    
     
    
                    </Columns>
                    
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    Code-behind:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    using System.Data;
    
     
    
    namespace TestApplicationWithDatabase
    {
        public partial class WebForm14 : System.Web.UI.Page
        {
            protected void bind()
            {
                string str, strSql;
                str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542ConnectionString"].ConnectionString;
                SqlConnection conn = new SqlConnection(str);
                strSql = "select * from Sum";
                SqlDataAdapter da = new SqlDataAdapter(strSql, str);
                DataSet ds = new DataSet();
                da.Fill(ds, "Sum");
                //DataTable dt = new DataTable();
                //dt.Columns.AddRange(new DataColumn[3] { new DataColumn("First"),new DataColumn("Second"),new DataColumn("Third")});
                //dt.Rows.Add(1,2,3);
                //dt.Rows.Add(4,5,6);
                //decimal sum = 0;
                //for (int i = 0; i < dt.Rows.Count; i++)
                //{
                //    sum += Convert.ToDecimal(dt.Rows[i]["First"])+ Convert.ToDecimal(dt.Rows[i]["Second"])+ Convert.ToDecimal(dt.Rows[i]["Third"]); ;
                //}
                //DataRow dr = dt.NewRow();
                //dr["Third"] = sum; 
                //dt.Rows.Add(dr);
                this.GridView1.DataSource = ds.Tables[0].DefaultView;
                this.GridView1.DataBind();
                conn.Close();
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }
    
     
    
            int sfirst = 0;
    
     
    
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                // int sum = 0;
                //// int s = Convert.ToInt32(e.Row.Cells[0].Text);
                // if (e.Row.RowType == DataControlRowType.DataRow)
                // {
                //     for (int i = 0; i < GridView1.Rows.Count; i++)
                //     {
                //         for (int j = 0; j < e.Row.Cells.Count; j++)
                //         {
                //             s = Convert.ToInt32(GridView1.Rows[i].Cells[j].Text);
                //         }
                //     }
                //     sum += s;
                // }
                // GridView grid = (GridView)sender;
                // if (e.Row.RowIndex == (grid.Rows.Count - 1))
                // {
                //     Label lbsum = (Label)e.Row.FindControl("lbsum");
                //     sum = Int32.Parse(lbsum.Text);
                // }
    
     
    
    
               
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    //total += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "Label1"));
                    Label first = (Label)e.Row.FindControl("Label1");
                    Label second = (Label)e.Row.FindControl("Label2");
                    Label third = (Label)e.Row.FindControl("Label3");
                    sfirst = sfirst + int.Parse(first.Text)+int.Parse(second.Text)+int.Parse(third.Text);
                }
                
                if (e.Row.RowType == DataControlRowType.Footer)
                {
                    GridViewRow gvrow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
                    TableCell tCell = new TableCell();
                    tCell.Text = "Total: " + sfirst.ToString();
                    gvrow.Cells.Add(tCell);
                    Table tbl = e.Row.Parent as Table;
                    tbl.Rows.AddAt(tbl.Rows.Count - 1, gvrow);
                
                }
            }
        }
    }

    Result:

    Best Regards,

    Brando

    Friday, October 11, 2019 9:59 AM