Gridview Column Total

RAVI 896 Reputation points
2024-03-26T08:00:23.19+00:00

Hello

This Is My Aspx Page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %> 
<html>
<head runat="server">
    <title>Untitled Page</title>
        
<style type="text/css">
    table {
    border-collapse: collapse;
}
    
    table, th, td, tr {
    border: 1px solid black;
}
</style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <br />
        <asp:GridView ID="GV1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
            CellSpacing="3" Font-Bold="True" Font-Names="Calibri" Font-Size="11pt"  
            Width="400px">
            <Columns>
            
            
                <asp:TemplateField>
                    <HeaderTemplate>
                        SI NO</HeaderTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblSRNO" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                
                
                
                <asp:TemplateField HeaderText="FIELD 2">
    <FooterTemplate>
        <asp:Label ID="FA" runat="server" Font-Size="16px" ForeColor="red"></asp:Label> 
    </FooterTemplate>
    <ItemTemplate>
         <asp:Label ID="A" runat="server"  ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[Field2]").ToString()) ? "" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[Field2]").ToString())))%>' >
 </asp:Label>
   
    </ItemTemplate>
    <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
    <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
    <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
     <ControlStyle Width="100px" />
</asp:TemplateField>
                
                                                    
                                    
<asp:TemplateField HeaderText="FIELD 3">
    <FooterTemplate>
        <asp:Label ID="FA1" runat="server" Font-Size="16px" ForeColor="red"></asp:Label> 
    </FooterTemplate>
    <ItemTemplate>
         <asp:Label ID="A1" runat="server"  ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[Field3]").ToString()) ? "" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[Field3]").ToString())))%>' >
 </asp:Label>
   
    </ItemTemplate>
    <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
    <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
    <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
     <ControlStyle Width="100px" />
</asp:TemplateField>
                                    
<asp:TemplateField HeaderText="FIELD 4">
    <FooterTemplate>
        <asp:Label ID="FA2" runat="server" Font-Size="16px" ForeColor="red"></asp:Label> 
        
    </FooterTemplate>
    <ItemTemplate>
         <asp:Label ID="A2" runat="server"  ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[Field4]").ToString()) ? "" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[Field4]").ToString())))%>' >
 </asp:Label>
  
    </ItemTemplate>
    <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
    <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
    <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
     <ControlStyle Width="100px" />
</asp:TemplateField>
          
                
                
                
                
                
                
                 
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

This Is My Code Behind

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 Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //

        DataTable hdt = new DataTable();
        SqlConnection hcon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString);
        hcon1.Open();
        SqlCommand hcmd1 = new SqlCommand("select * from Table1 ", hcon1);
        SqlDataAdapter hada1 = new SqlDataAdapter(hcmd1);
        hada1.Fill(hdt);
        GV1.DataSource = hdt;
        GV1.DataBind();
        hcon1.Close();
    }
}

This Is Sql Code

 
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Table1](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Field1] [varchar](150) NULL,
	[Field2] [float] NULL,
	[Field3] [float] NULL,
	[Field4] [float] NULL,
 CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Table1] ON
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (1, N'B1/A1', 1000, 50000, 40000)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (2, N'B2/23-24', 45800, 100, 400)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (3, N'B2/23-24', 200100, 5000, 8000)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (4, N'B3/B1', 100, 300, 600)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (24, N'B3/B1', 300, 20, 40)
INSERT [dbo].[Table1] ([ID], [Field1], [Field2], [Field3], [Field4]) VALUES (25, N'B4/A2', 400, 100, 200)
SET IDENTITY_INSERT [dbo].[Table1] OFF

This is the output

11111

I want Field2, field3, field4 column total in footer

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

Accepted answer
  1. Lan Huang-MSFT 25,386 Reputation points Microsoft Vendor
    2024-03-26T10:02:18.2+00:00

    Hi @RAVI,

    You don't need to add FooterTemplate in every TemplateField, just set ShowFooter="true" in GridView.

    The specific code is as follows:

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable hdt = new DataTable();
        SqlConnection hcon1 = new SqlConnection(ConfigurationManager.ConnectionStrings["CHEMIMSConnectionString"].ConnectionString);
        hcon1.Open();
        SqlCommand hcmd1 = new SqlCommand("select * from Table1 ", hcon1);
        SqlDataAdapter hada1 = new SqlDataAdapter(hcmd1);
        hada1.Fill(hdt);
        GV1.DataSource = hdt;
        GV1.DataBind();
        double total = 0; ;
       GV1. FooterRow.Cells[0].Text = "Total";
       GV1.FooterRow.Cells[0].Font.Bold = true;
        GV1.FooterRow.Cells[0].HorizontalAlign = HorizontalAlign.Left;
        for (int k = 2; k < hdt.Columns.Count; k++)
        {
            total= hdt.AsEnumerable().Sum(x => x.Field<Double>(k));
            
            GV1.FooterRow.Cells[k-1].Text = total.ToString();
          
          
        }
        hcon1.Close();
    }
    
    <html>
    <head runat="server">
        <title>Untitled Page</title>
        <style type="text/css">
            table {
                border-collapse: collapse;
            }
            table, th, td, tr {
                border: 1px solid black;
            }
        </style>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <br />
                <asp:GridView ID="GV1" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                    CellSpacing="3" Font-Bold="True" Font-Names="Calibri" Font-Size="11pt"
                    Width="400px" ShowFooter="true">
                    <Columns>
                        <asp:TemplateField>
                            <HeaderTemplate>
                                SI NO
                            </HeaderTemplate>
                            <ItemTemplate>
                                <asp:Label ID="lblSRNO" runat="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FIELD 2">                 
                            <ItemTemplate>
                                <asp:Label ID="A" runat="server" ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[Field2]").ToString()) ? "" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[Field2]").ToString())))%>'>
                                </asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
                            <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
                            <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
                            <ControlStyle Width="100px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FIELD 3">            
                            <ItemTemplate>
                                <asp:Label ID="A1" runat="server" ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[Field3]").ToString()) ? "" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[Field3]").ToString())))%>'>
                                </asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
                            <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
                            <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
                            <ControlStyle Width="100px" />
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="FIELD 4">                     
                            <ItemTemplate>
                                <asp:Label ID="A2" runat="server" ForeColor="Black" Font-Size="20px" Text='<%# String.IsNullOrEmpty(Eval("[Field4]").ToString()) ? "" : (String.Format(System.Globalization.CultureInfo.CreateSpecificCulture("hi-IN"), "{0:#,#}" , decimal.Parse(Eval("[Field4]").ToString())))%>'>
                                </asp:Label>
                            </ItemTemplate>
                            <FooterStyle Font-Size="26px" ForeColor="Teal" HorizontalAlign="Right" />
                            <HeaderStyle ForeColor="Black" BackColor="#FCF3CF" Font-Size="20px" />
                            <ItemStyle ForeColor="Teal" HorizontalAlign="Right" />
                            <ControlStyle Width="100px" />
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>
    

    User's image

    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.


1 additional answer

Sort by: Most helpful
  1. RAVI 896 Reputation points
    2024-03-27T05:57:29.78+00:00

    Thank you solved i used this for null

    Convert.ToDecimal(string.IsNullOrEmpty(textboxText) ? "0" : textboxText);
    
    0 comments No comments