This article will show you how you Display Grand Total In
Gridview Footer On RowDataBound in Asp.Net Using C#.Net. In this we
will display to bind value total at run time in asp.net using c#.net.
Some of my previous articles are as follows: Merge
Footer or Apply ColumnSpan to Footer of GridView In Asp.Net Using C#.Net, Display
Selected Row Value Of GridView In JavaScript Alert Message Asp.Net Using C#.Net, Detect
Checked CheckBox Indise GridView on Button Click In Asp.Net Using C#.Net, Page
Count Display in GridView Footer In Asp.Net Using C#.Net, GridView
Bind Using DataTable With Paging In Asp.Net Using C#.Net, Single
RadioButton Selection in GridView In Asp.Net Using C#.Net, Bind
and Validate GridView TextBox Value by jQuery In Asp.Net Using C#, Bind
and Validate GridView TextBox Value by RequiredFieldValidator In Asp.Net Using
C#, Populate
Data in GridView on DropdownList Selected Role in Asp.net Using C#.net, Search
and Display Data In GridView From Database Table In Asp.Net Using C#.Net, Read
XML File Data Using Linq Query and Add in DataTable to Bind GridView in Asp.net
Using C#.Net, Bind
& Search XML File Data and Display in GridView in Asp.net Using C#.Net,
Search
GridView Record on Button Click By Using C#.Net in Asp.Net.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Display
Grand Total In Gridview Footer On RowDataBound In Asp.Net Using
C#.Net
</title>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" cellpadding="4" cellspacing="4">
<tr>
<td align="left">
<asp:GridView ID="GridView1" runat="server" Width="100%"
AutoGenerateColumns="False"
ShowFooter="True" onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Name
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblName"
runat="server"
Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
<FooterStyle Font-Bold="True"
/>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Address
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNamAddress"
runat="server"
Text='<%# Eval("Address") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
Type
</HeaderTemplate>
<FooterTemplate>
<b> Total :</b>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblUserType"
runat="server"
Text='<%# Eval("UserType") %>'></asp:Label>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right"
/>
</asp:TemplateField>
<asp:TemplateField HeaderText="Earning">
<FooterTemplate>
<asp:Label ID="lblGrandtotal"
runat="server"
Text='' Font-Bold="true"></asp:Label>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lbltotal"
runat="server"
Text='<%# Eval("Earning") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
|
In above code I have use itemtemplate to bind the gridview
fields and used label control to display the value using Eval. In HeaderTemplate
I have added label control . I will detect this control and assign
Now check the footer of the gridview in this I have added a label control in this I
will display the total.
Now check the code to bind and display no of pages.
using System;
using System.Data.SqlClient;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;
using System.Collections;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
DataTable
objdt = new DataTable();
protected
void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
GetDistinctValue();
}
}
private
void GetDistinctValue()
{
SqlConnection
con = new SqlConnection(System.Configuration.
ConfigurationManager.ConnectionStrings["con"].ToString());
try
{
string
query = "select * from UserDetail;";
SqlDataAdapter
da = new SqlDataAdapter(query,
con);
con.Open();
da.Fill(objdt);
con.Close();
if
(objdt.Rows.Count > 0)
{
GridView1.DataSource =
objdt;
GridView1.DataBind();
}
}
catch
{
con.Close();
}
}
protected
void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if
(e.Row.RowType == DataControlRowType.Footer)
{
var
total = objdt.AsEnumerable().Select(c => new
{ earning = c.Field<int>("Earning") }).Sum(m => m.earning);
Label
lblTotal = (Label)e.Row.FindControl("lblGrandtotal");
lblTotal.Text =
total.ToString();
}
}
}
}
|
On above code I have used a function to bind the data. Now I
have I have added the control in footer to display the total. Now check the
below code to display total value at run time.
protected
void GridView1_RowDataBound(object sender, GridViewRowEventArgs
e)
{
if
(e.Row.RowType == DataControlRowType.Footer)
{
var
total = objdt.AsEnumerable().Select(c => new
{ earning = c.Field<int>("Earning") }).Sum(m => m.earning);
Label
lblTotal = (Label)e.Row.FindControl("lblGrandtotal");
lblTotal.Text =
total.ToString();
}
}
|
In above code I have validated when the footer row is
appearing. After that I have get sum all
values by using linq query, and then detected the label control to display the
total.
Now we have done run the application to check the output.
DOWNLOAD
not working
ReplyDeleteHi The code is 100% working. but still please let me know what error you are getting.
DeletePlease download the sample and try the why code is written.
Deletenot working,its is not showing data in foottemplate
ReplyDelete