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>
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.