How To Get Row Total And Column Total With My Working Filter

RAVI 1,056 Reputation points
2022-11-01T07:46:13.043+00:00

Hello
This Is My aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
   
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head runat="server">  
<title>DEMO PAGE</title>  
   
<script type="text/javascript" src="jquery-3.2.1.min.js"></script>   
<script type="text/javascript" src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>  
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css" />  
   
   
<script>  
         $(document).ready(function () {  
             $('#<%=gvDemo.ClientID%>').DataTable({  
                 initComplete: function () {  
                     this.api().columns("0").every(function () {  
                         var column = this;  
                         var select = $('<select style="width:130px;"><option value=""></option></select>')  
                            .appendTo( $("#Span1").empty() )  
                               
                             .on('change', function () {  
                                 var val = $.fn.dataTable.util.escapeRegex(  
                                     $(this).val()  
                                 );  
  
                                 column  
                                     .search(val ? '^' + val + '$' : '', true, false)  
                                     .draw();  
                             });  
  
                         column.data().unique().sort().each(function (d, j) {  
                             select.append('<option value="' + d + '">' + d + '</option>')  
                         });  
                     });  
                       
                       
                     this.api().columns("1").every(function () {  
                         var column = this;  
                         var select = $('<select style="width:130px;"><option value=""></option></select>')  
                            .appendTo( $("#Span2").empty() )  
                               
                             .on('change', function () {  
                                 var val = $.fn.dataTable.util.escapeRegex(  
                                     $(this).val()  
                                 );  
  
                                 column  
                                     .search(val ? '^' + val + '$' : '', true, false)  
                                     .draw();  
                             });  
  
                         column.data().unique().sort().each(function (d, j) {  
                             select.append('<option value="' + d + '">' + d + '</option>')  
                         });  
                     });  
                       
                          
                       
                     //   end  
                 }  
             });  
         });  
        </script>  
   
</head>  
<body>  
<form id="form1" runat="server">  
<div>  
          
<TABLE>  
  <TR>  
   <TD style="WIDTH: 100px; height: 31px;">  
      <span style="color: darkcyan; font-family: Calibri"><strong>ITEM</strong></span></TD>  
  <TD style="WIDTH: 100px; height: 31px;">  
      <span style="color: darkcyan; font-family: Calibri"><strong>DESCRIPTION</strong></span></TD>   
  </TR>  
  <TR>  
   <TD onChange="calc()"; style="WIDTH: 100px;font-size:12px; font-weight:bold;font-family:Calibri;color:Black;"> <span id="Span1"></span></TD>  
  <TD onChange="calc()"; style="WIDTH: 100px;font-size:12px; font-weight:bold;font-family:Calibri;color:Black;"> <span id="Span2"></span></TD>  
      
  </TR> </TABLE>   
    
<asp:GridView ID="gvDemo" runat="server" AllowSorting="true" AutoGenerateColumns="false"  OnPreRender="gvDemo_PreRender" OnRowCreated="gvDemo_RowCreated" Font-Names="Calibri" Font-Size="11pt" OnSorting="gvDemo_Sorting">  
    <Columns>  
      <asp:BoundField DataField="Item1" HeaderText="Item" />  
     <asp:BoundField DataField="Description" HeaderText="Description" />  
      <asp:BoundField DataField="Qty" HeaderText="QTY" />    
        <asp:BoundField DataField="Qty" HeaderText="VALUE" />        
    </Columns>  
</asp:GridView>  
    
    </div>  
    </form>  
</body>  
</html>  

my code file

using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Data.SqlClient;  
using System.Collections.Specialized;  
using System.Text;  
using System.Drawing;  
using System.IO;  
using System.Net;  
using System.Net.Mail;  
using System.Net.Configuration;  
  
public partial class _Default : System.Web.UI.Page  
{  
  
  
      
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!Page.IsPostBack)  
        {  
            DataTable dt = new DataTable();  
            dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item1", typeof(string)),      
                       
                    new DataColumn("Description",typeof(string)),  
             new DataColumn("Qty", typeof(int)),    
             new DataColumn("Value", typeof(int))});  
            dt.Rows.Add("A", "M1", "2", "1");  
            dt.Rows.Add("A", "M2", "4", "2");  
            dt.Rows.Add("A", "M3", "6", "4");  
  
            dt.Rows.Add("E", "N4", "3", "4");  
            dt.Rows.Add("E", "N4", "6", "10");  
            dt.Rows.Add("F", "P1", "2", "20");  
            dt.Rows.Add("F", "K1", "3", "10");  
            dt.Rows.Add("F", "Z1", "5", "20");   
            gvDemo.DataSource = dt;  
            gvDemo.DataBind();     
              
        }  
    }  
    protected void btnPopulate_Click(object sender, EventArgs e)  
    {  
  
    }  
    protected void gvDemo_PreRender(object sender, EventArgs e)  
    {  
         
    }  
    protected void gvDemo_RowCreated(object sender, GridViewRowEventArgs e)  
    {  
          
    }  
}  

all filter working fine
I need total and grandtotal as below
255973-mmmm.jpg

thanking you

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
{count} votes

Accepted answer
  1. Lan Huang-MSFT 28,841 Reputation points Microsoft Vendor
    2022-11-04T08:05:10.78+00:00

    Hi @RAVI ,
    I tried what you said. The reason it doesn't work is because the header column count doesn't match, the header column count and row column count should be equal.
    You just need to add a column to GrandTotal.

      cell = new TableCell();  
                    cell.Text = "---";  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
    

    257181-8.gif

     protected void Page_Load(object sender, EventArgs e)  
            {  
                if (!Page.IsPostBack)  
                {  
                    DataTable dt = new DataTable();  
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item1", typeof(string)),  
      
                         new DataColumn("Description",typeof(string)),  
                  new DataColumn("Qty", typeof(int)),  
                  new DataColumn("Value", typeof(int))});  
                    dt.Rows.Add("A", "M1", "2", "1");  
                    dt.Rows.Add("A", "M2", "4", "2");  
                    dt.Rows.Add("A", "M3", "6", "4");  
      
                    dt.Rows.Add("E", "N4", "3", "4");  
                    dt.Rows.Add("E", "N4", "6", "10");  
                    dt.Rows.Add("F", "P1", "2", "20");  
                    dt.Rows.Add("F", "K1", "3", "10");  
                    dt.Rows.Add("F", "Z1", "5", "20");  
                    gvDemo.DataSource = dt;  
                    gvDemo.DataBind();              
                    gvDemo.UseAccessibleHeader = true;  
                    gvDemo.HeaderRow.TableSection = TableRowSection.TableHeader;  
      
                }  
            }  
      
            // To keep track of the previous row Group Identifier      
            string strPreviousRowName = string.Empty;  
            // To keep track the Index of Group Total      
            int intSubTotalIndex = 1;  
            // To temporarily store Sub Total      
            string dblSubTotalDescription = "";  
            double dblSubTotalValue = 0;  
            double dblSubTotalQuantity = 0;  
            // To temporarily store Grand Total      
            double dblGrandTotalValue = 0;  
            double dblGrandTotalQuantity = 0;  
            protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)  
            {  
                bool IsSubTotalRowNeedToAdd = false;  
                bool IsGrandTotalRowNeedtoAdd = false;  
                if ((strPreviousRowName != string.Empty) && (DataBinder.Ev al(e.Row.DataItem, "Item1") != null))  
                    if (strPreviousRowName != DataBinder.Ev al(e.Row.DataItem, "Item1").ToString())  
                        IsSubTotalRowNeedToAdd = true;  
                if ((strPreviousRowName != string.Empty) && (DataBinder.Ev al(e.Row.DataItem, "Item1") == null))  
                {  
                    IsSubTotalRowNeedToAdd = true;  
                    IsGrandTotalRowNeedtoAdd = true;  
                    intSubTotalIndex = 0;  
                }  
      
                if (IsSubTotalRowNeedToAdd)  
                {  
      
                    GridView grdViewOrders = (GridView)sender;  
                    // Creating a Row            
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                    //Adding Total Cell            
                    TableCell cell = new TableCell();  
                    cell.Text = strPreviousRowName.ToString();  
                    cell.HorizontalAlign = HorizontalAlign.Left;  
                    //cell.ColumnSpan = 2;  
                    cell.CssClass = "SubTotalRowStyle";  
                    row.Cells.Add(cell);  
      
                    //Create Unit Column  
                    cell = new TableCell();  
                    cell.Text = "Sub Total";  
                    cell.HorizontalAlign = HorizontalAlign.Left;  
                    cell.CssClass = "SubTotalRowStyle";  
                    row.Cells.Add(cell);  
      
                    //Adding Quantity Column              
                    cell = new TableCell();  
                    cell.Text = dblSubTotalQuantity.ToString();  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "SubTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding Unit Price Column            
                    cell = new TableCell();  
                    cell.Text = dblSubTotalValue.ToString();  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "SubTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding the Row at the RowIndex position in the Grid        
                    grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex + intSubTotalIndex, row);  
                    intSubTotalIndex++;  
                    dblSubTotalDescription = "";  
                    dblSubTotalValue = 0;  
                    dblSubTotalQuantity = 0;  
      
                }  
                if (IsGrandTotalRowNeedtoAdd)  
                {  
      
                    GridView grdViewOrders = (GridView)sender;  
                    // Creating a Row        
                    GridViewRow row = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);  
                    //Adding Total Cell             
                    TableCell cell = new TableCell();  
                    cell.Text = "Grand Total";  
                    cell.HorizontalAlign = HorizontalAlign.Left;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
      
                    cell = new TableCell();  
                    cell.Text = "---";  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding Quantity Column             
                    cell = new TableCell();  
                    cell.Text = dblGrandTotalQuantity.ToString();  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding Unit Price Column            
                    cell = new TableCell();  
                    cell.Text = dblGrandTotalValue.ToString();  
                    cell.HorizontalAlign = HorizontalAlign.Right;  
                    cell.CssClass = "GrandTotalRowStyle";  
                    row.Cells.Add(cell);  
                    //Adding the Row at the RowIndex position in the Grid       
                    grdViewOrders.Controls[0].Controls.AddAt(e.Row.RowIndex, row);  
      
                }  
            }  
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)  
            {  
                // This is for cumulating the values         
                if (e.Row.RowType == DataControlRowType.DataRow)  
                {  
                    strPreviousRowName = DataBinder.Ev al(e.Row.DataItem, "Item1").ToString();  
                    string dblDescription = Convert.ToString(DataBinder.Ev al(e.Row.DataItem, "Description").ToString());  
                    double dblValue = Convert.ToDouble(DataBinder.Ev al(e.Row.DataItem, "Value").ToString());  
                    double dblQuantity = Convert.ToDouble(DataBinder.Ev al(e.Row.DataItem, "Qty").ToString());  
                    // Cumulating Sub Total  
                    dblSubTotalDescription = dblDescription;  
                    dblSubTotalValue += dblValue;  
                    dblSubTotalQuantity += dblQuantity;  
                    // Cumulating Grand Total             
                    dblGrandTotalValue += dblValue;  
                    dblGrandTotalQuantity += dblQuantity;  
                }  
            }  
    

    Only part of the code has been modified in the front end:

     <style>  
             .SubTotalRowStyle {  
                 border: solid 1px Black;  
                 background-color: #D8D8D8;  
                 font-weight: bold;  
             }  
             .GrandTotalRowStyle {  
                 border: solid 1px Gray;  
                 background-color: #000000;  
                 color: #ffffff;  
                 font-weight: bold;  
             }  
             .GroupHeaderStyle {  
                 border: solid 1px Black;  
                 background-color: #4682B4;  
                 color: #ffffff;  
                 font-weight: bold;  
             }  
             .serh-grid {  
                 width: 85%;  
                 border: 1px solid #6AB5FF;  
                 background: #fff;  
                 line-height: 14px;  
                 font-size: 11px;  
                 font-family: Verdana;  
             }  
    </style>  
    

      <asp:GridView ID="gvDemo" runat="server" AllowSorting="true"  AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound"  
                    OnRowCreated="GridView1_RowCreated" Font-Names="Calibri" Font-Size="11pt">    
    

       <asp:BoundField DataField="VALUE" HeaderText="VALUE" />     
    

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. RAVI 1,056 Reputation points
    2022-11-09T08:09:01.127+00:00

    Hello @Lan Huang-MSFT

    Thanks for your code

    if i have my datatable data as below

     DataTable dt = new DataTable();  
                    dt.Columns.AddRange(new DataColumn[4] { new DataColumn("Item1", typeof(string)),      
    
                    new DataColumn("Description",typeof(string)),  
                    new DataColumn("Qty", typeof(int)),    
                    new DataColumn("Value", typeof(int))});  
                    dt.Rows.Add("A", "M1", "2", "1");  
                    dt.Rows.Add("A", "K1", "4", "2");  
                    dt.Rows.Add("A", "M3", "6", "4");  
    
                    dt.Rows.Add("E", "N4", "3", "4");  
                    dt.Rows.Add("E", "N4", "6", "10");  
                    dt.Rows.Add("F", "P1", "2", "20");  
                    dt.Rows.Add("F", "N4", "3", "10");  
                    dt.Rows.Add("F", "K1", "5", "20");  
                    gvDemo.DataSource = dt;  
                    gvDemo.DataBind();  
                    gvDemo.UseAccessibleHeader = true;  
                    gvDemo.HeaderRow.TableSection = TableRowSection.TableHeader;  
    

    then after

    **1) if i do filter K1 it works fine in Description column i want total for k1 as Qty column total = 9 and Value Column total = 22 without postback

    2) if i do filter N4 it works fine in Description column i want total for N4 as Qty column total = 12 and Value Column total = 24 without postback**

    Thanking You

    0 comments No comments