Hi,
Use RowDataBound event of the GridView :
decimal TotalSalary = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// If current row is a DataRow, current salary is stored in CurrentSalary and added to TotalSalary
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal CurrentSalary = (decimal)DataBinder.Eval(e.Row.DataItem, "Salary");
TotalSalary += CurrentSalary;
}
// If current row is Footer, its label value is changed with the TotalSalary
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotal =(Label)e.Row.FindControl("lblTotal");
lblTotal.Text ="Total: "+ TotalSalary.ToString();
}
}
<asp:GridView ID="GridView1"
runat="server"
ShowFooter="true"
AutoGenerateColumns="false"
DataSourceID="SqlDataSource1"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField HeaderText="Employee ID" DataField="EmployeeID" />
<asp:BoundField HeaderText="First Name" DataField="FirstName" />
<asp:BoundField HeaderText="Last Name" DataField="LastName" />
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<%# Eval("Salary") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label runat="server" ID="lblTotal" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource
runat="server"
ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:ConString %>"
SelectCommand="Select Top 10 EmployeeID, FirstName, LastName, Salary FROM Employees ORDER BY Salary DESC"></asp:SqlDataSource>
reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator