Karthik K

Karthik K

  • 1k
  • 738
  • 192k

Bring TOTAL In footer in Gridview ?

Nov 8 2017 12:05 AM
I have loaded the values in gridview. In the other gridivew I have loaded the selection values in the temporary table and I made remove when clicking Particular button.
 
My Requirement is, I have calculated the total amount in Tempory Table Loaded Gridview.Here I Show my code. I want to achieve the Total Amount in footer Template.
 
Any of you guide me. To achieve this task .
 
Design Page :
<form id="form1" runat="server">
<table>
<tr>
<td >
<div>
<asp:Label Width="400px" ID="lblName" runat="server" Text="ASUS" Font-Size="Medium" ForeColor="Black" BackColor="Orange"></asp:Label>
<asp:GridView ID="grdShop" Width="400px" EmptyDataText="No Such Data Found" runat="server" AutoGenerateColumns="false" DataKeyNames="ProudtCode">
<Columns>
<asp:TemplateField HeaderText="ProudtCode">
<ItemTemplate>
<asp:Label ID="lblPCode" runat="server" Text='<%#Eval("ProudtCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pic">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="62px"
ImageUrl='<%#Eval("ImagePath")%>' Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%#Eval("BrandName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="ProudtCode" DataNavigateUrlFormatString="?PROUDCTCODE={0}" HeaderText="Buy" Text="Buy"/>
</Columns>
</asp:GridView>
</div>
<div>
<asp:Label Width="400px" ID="Label1" runat="server" Font-Size="Medium" Text="APPLE" ForeColor="Black" BackColor="Orange"></asp:Label>
<asp:GridView ID="GridView2" Width="400px" EmptyDataText="No Such Data Found" runat="server" AutoGenerateColumns="false" DataKeyNames="ProudtCode">
<Columns>
<asp:TemplateField HeaderText="ProudtCode">
<ItemTemplate>
<asp:Label ID="lblPCode" runat="server" Text='<%#Eval("ProudtCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pic">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="62px"
ImageUrl='<%#Eval("ImagePath")%>' Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%#Eval("BrandName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="ProudtCode" DataNavigateUrlFormatString="?PROUDCTCODE={0}" HeaderText="Buy" Text="Buy"/>
</Columns>
</asp:GridView>
</div>
</td>
<td align="right">
<div>
<asp:Label Width="500px" ID="Label2" runat="server" Font-Size="Medium" Text="CART LIST" ForeColor="Black" BackColor="Orange"></asp:Label>
<asp:GridView ID="GridView1" Width="500px" EmptyDataText="No Such Data Found" ShowFooter="true"
runat="server" AutoGenerateColumns="false" DataKeyNames="ProudtCode" AllowPaging="true"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="ProudtCode">
<ItemTemplate>
<asp:Label ID="lblPCode" runat="server" Text='<%#Eval("ProudtCode")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pic">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="62px"
ImageUrl='<%#Eval("ImagePath")%>' Width="80px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<asp:Label ID="lblBrand" runat="server" Text='<%#Eval("BrandName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblPrice" runat="server" Text='<%#Eval("Price")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Count">
<EditItemTemplate>
<asp:TextBox ID="txtCount1" runat="server"></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="txtEditCount" runat="server" Width="35px" Text='<%#Eval("Count")%>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total Price">
<ItemTemplate>
<asp:Label ID="lblPrice1" runat="server" Text='<%#Eval("Total")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
<asp:HyperLinkField DataNavigateUrlFields="ProudtCode" DataNavigateUrlFormatString="?DELID={0}" HeaderText="Delete" Text="Delete"/>
</Columns>
</asp:GridView>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
 
Back end codeing :
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString);
DataTable Dt = new DataTable();
DataTable Dt2 = new DataTable();
DataTable Basket_DataTable = null;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["basket"] !=null)
{
Basket_DataTable = (DataTable)Session["basket"];
}
else
{
Basket_DataTable = new DataTable();
Basket_DataTable.Columns.Add("ProudtCode");
Basket_DataTable.Columns.Add("ImagePath");
Basket_DataTable.Columns.Add("BrandName");
Basket_DataTable.Columns.Add("Price");
Basket_DataTable.Columns.Add("Count");
Basket_DataTable.Columns.Add("Total");
}
if(Request["DELID"]!= null)
{
for (int i = 0; i < Basket_DataTable.Rows.Count; i++)
if (Basket_DataTable.Rows[i][0].ToString() == Request["DELID"].ToString())
Basket_DataTable.Rows.Remove(Basket_DataTable.Rows[i]);
}
if (Request["PROUDCTCODE"]!= null)
{
bool Found = false;
for(int i=0; i< Basket_DataTable.Rows.Count; i++)
if(Basket_DataTable.Rows[i][0].ToString()==Request["PROUDCTCODE"].ToString())
Found =true;
if(Found ==false)
{
string query1 = "select * from ProductMaster where ProudtCode='" + Request["PROUDCTCODE"] + "'";
SqlDataAdapter Dtt = new SqlDataAdapter(query1, con);
DataTable Dresult=new DataTable();
Dtt.Fill(Dresult);
if (Dresult != null && Dresult.Rows.Count == 1)
{
Basket_DataTable.Rows.Add(new object[] { Request["PROUDCTCODE"], Dresult.Rows[0]["ImagePath"].ToString(), Dresult.Rows[0]["BrandName"].ToString(), Dresult.Rows[0]["Price"].ToString(), "1", Dresult.Rows[0]["Price"].ToString() });
}
}
}
if (!IsPostBack)
{
LoadGrid();
LoadGrid1();
}
if (IsPostBack == false)
{
GridView1.DataSource = Basket_DataTable;
GridView1.DataBind();
}
Session["basket"]=Basket_DataTable;
}
public void LoadGrid()
{
con.Open();
string query = "Select * from ProductMaster where BrandName='ASUS'";
SqlDataAdapter Ada = new SqlDataAdapter(query,con);
Ada.Fill(Dt);
grdShop.DataSource = Dt;
grdShop.DataBind();
con.Close();
}
public void LoadGrid1()
{
con.Open();
string query5 = "Select * from ProductMaster where BrandName='APPLE'";
SqlDataAdapter Ada2 = new SqlDataAdapter(query5, con);
Ada2.Fill(Dt2);
GridView2.DataSource = Dt2;
GridView2.DataBind();
con.Close();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//decimal GrandTot = 0;
//if (e.Row.RowType == DataControlRowType.DataRow)
//{
// Label Tot = (Label)e.Row.FindControl("lblPrice");
// GrandTot =GrandTot + Convert.ToDecimal(Tot.Text);
// }
//if (e.Row.RowType == DataControlRowType.Footer)
//{
// Label lbl = (Label)e.Row.FindControl("lblTotal");
// lbl.Text = GrandTot.ToString("c");
//}
}
}
 
Thanx in advance
Karthik k

Answers (1)