Friday, June 27, 2008

using gridview calculate total in footer template

[sourcecode langauge="html"]

<asp:GridView ID="gv" Runat="Server"
AllowPaging="false"
AllowSorting="false"
AutoGenerateColumns="false"
BorderColor="#000080"
BorderStyle="Solid"
BorderWidth="2px"
Caption=""
HorizontalAlign="Center"
ShowFooter="true"
Width="90%"
OnRowDataBound="gv_RowDataBound" >
<HeaderStyle HorizontalAlign="Center" />
<RowStyle  />
<AlternatingRowStyle  />
<FooterStyle  HorizontalAlign="Right"/>
<Columns>
<asp:TemplateField HeaderText="Name" FooterText="Total:">
<ItemTemplate>
<%# Eval("name") %>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Salary"
ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Literal id="lblsalary" runat="server"
text='<%# Eval("Salry") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Literal id="lbltotSalary" runat="server" />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Tax"
ItemStyle-HorizontalAlign="Right">
<ItemTemplate>
<asp:Literal id="lblTax" runat="server"
text='<%# Eval("Tax") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:Literal id="lblTotTax"
runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

[/sourcecode]

codebehind pages

[sourcecode langauge="csharp"]

private Decimal msalaryTotal;
private Decimal mTaxTotal;
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource dSource = null;

if (!Page.IsPostBack)
{
dSource = new SqlDataSource();
dSource.ConnectionString = ConfigurationManager.
ConnectionStrings["dbConnectionString"].ConnectionString;
dSource.DataSourceMode = SqlDataSourceMode.DataReader;
dSource.ProviderName = "System.Data.SqlDb";
dSource.SelectCommand = "SELECT Name,Salary,Tax" +
"FROM emp " +"ORDER BY name";
msalaryTotal = 0;
mTaxTotal = 0;
gv.DataSource=dSource;
gv.DataBind();
}
}
protected void gv_RowDataBound(Object sender,GridViewRowEventArgs e)
{
DbDataRecord rowData;
Decimal amt;
Literal salaryLabel;
Literal TaxLabel;
Literal totalLabel;
switch (e.Row.RowType)
{
case DataControlRowType.DataRow:
rowData = (DbDataRecord)(e.Row.DataItem);
amt = (Decimal)(rowData["Salry"]);
msalaryTotal += amt;
salaryLabel = (Literal)(e.Row.FindControl("lblSalary"));
salaryLabel.Text = amt.ToString("C2");
amt = (Decimal)(rowData["Tax"]);
mTaxTotal += amt;
TaxLabel = (Literal)(e.Row.FindControl("lblTax"));
TaxLabel.Text = amt.ToString("C2");
break;
case DataControlRowType.Footer:
totalLabel = (Literal)(e.Row.FindControl("lbltotsalary"));
totalLabel.Text =msalaryTotal.ToString("C2");
totalLabel = (Literal)(e.Row.FindControl("lblTotTax"));
totalLabel.Text = mTaxTotal.ToString("C2");
break;
default:
break;
}
}

[/sourcecode]

1 comment :

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath