This will help to do how we can calculate the Total values on a Grid View control. We can add the sum of both row wise and column wise.If any one is stuck in coding can refer this code.. This is just a sample
The Grid View will look like this
a b Total ============ 10 1 11 1 1 2 ============ 11 2 13
For this Put labels and bind the value using Eval(),and allow Footer equals true.then using methods which is written in code behind we can calculate the column total and grand totals.
HTML Code
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false" ShowFooter="true" > <RowStyle BackColor="#EFF3FB" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField HeaderText="a"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# CalculateATotal(DataBinder.Eval(Container.DataItem,"a").ToString()) %>'></asp:Label> </ItemTemplate> <FooterTemplate > <asp:Label ID="Label12" runat="server" Text='<%# aTotal%>'></asp:Label> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="b"> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# CalculateBTotal(DataBinder.Eval(Container.DataItem,"b").ToString()) %>'></asp:Label> </ItemTemplate> <FooterTemplate > <asp:Label ID="Label13" runat="server" Text='<%# bTotal%>'></asp:Label> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="total"> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# CalculateTotal(DataBinder.Eval(Container.DataItem,"a").ToString(), DataBinder.Eval(Container.DataItem,"b").ToString())%>'></asp:Label> </ItemTemplate> <FooterTemplate > <asp:Label ID="Label11" runat="server" Text='<%# grandTotal%>'></asp:Label> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> Grand Total :<asp:Label ID="Label4" runat="server"></asp:Label> </div> </form> </body> </html>
Code Behind
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page { public int grandTotal; public int aTotal; public int bTotal; protected void Page_Load(object sender, EventArgs e) { //SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ToString());
SqlConnection con = new SqlConnection(ConnProviderString()); con.Open(); DataSet ds = new DataSet(); SqlDataAdapter adpt = new SqlDataAdapter("select a,b from val",con ); adpt.Fill(ds); GridView1.DataSource = ds; GridView1.DataBind(); } public string ConnProviderString() { string connectionString = "server=bharathi-166;integrated security=true;database=Master;uid=sa;pwd=;"; return connectionString; } protected string CalculateTotal(string a, string b) { int total = Convert.ToInt16(a) + Convert.ToInt16(b); grandTotal = grandTotal + total; return total.ToString(); } protected string CalculateATotal(string a) { aTotal = aTotal + Convert.ToInt16(a); return a; } protected string CalculateBTotal(string b) { bTotal = bTotal + Convert.ToInt16(b); return b; } }
AttachmentsGrid View and Footer (29650-26429-Code in aspx page.txt)
|
No responses found. Be the first to respond and make money from revenue sharing program.
|