have a problem gridview total

Posted by Nuwan under ASP.NET on 12/21/2011 | Points: 10 | Views : 4024 | Status : [Member] | Replies : 5
Hi Friend,
I want to get a selected grid view column total.
Please give me a code sample.

Thank You.

Rathnayake


Responses

Posted by: Sksamantaray on: 12/21/2011 [Member] Silver | Points: 25

Up
0
Down

1 ss 12121
2 gg 23234
3 dd 34342





<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID" DataSourceID="SqlDataSource1"
OnRowDataBound="GridView1_RowDataBound"
ShowFooter="True" AllowPaging="True" PageSize="5" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="451px"
>
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<Columns>
<asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField FooterText="Total" HeaderText="Name" InsertVisible="False"
SortExpression="Name">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server"
Text='<%# "$"+Eval("Salary").ToString()%>'>
</asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>
</Columns>

<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />

</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dbRecruitmentConnectionString %>"
SelectCommand="SELECT * FROM [EmpDetails]">
</asp:SqlDataSource>
</div>
</form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class GrandTotalGrid : System.Web.UI.Page
{
decimal GrandTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void GridView1_RowDataBound
(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "salary"));
GrandTotal = GrandTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = GrandTotal.ToString("c");
}
}
}



Thanks,
Sanjay

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anweshabhowmick on: 12/22/2011 [Member] Starter | Points: 25

Up
0
Down
Mr Sksamantaray is right ...Take his answer

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nuwan on: 12/22/2011 [Member] Starter | Points: 25

Up
0
Down
thanks for reply.

Rathnayake

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 12/22/2011 [Member] Silver | Points: 25

Up
0
Down
if it worked for you then click on " Login to Mark As Answer Section"
Thanks

Thanks,
Sanjay

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Self-innovator on: 12/23/2011 [Member] Bronze | Points: 25

Up
0
Down
try this
create function
public string gettotal()
{
SqlConnection cnn = new SqlConnection(Conn);
cnn.Open();
SqlCommand cmd = new SqlCommand("SELECT SUM(EmpSalary) FROM empp", cnn);
int totSal = Convert.ToInt32(cmd.ExecuteScalar());
string Sal = totSal.ToString();
return Sal;
}

call in footer tmplate

<%# gettotal() %>

Join Hands Change lives
Thanks & Regards
Straight Edge Society

Nuwan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response