gridview contol for footer template

Posted by reddysankark-13471 under ASP.NET on 2/1/2012 | Points: 10 | Views : 2010 | Status : [Member] | Replies : 2
how add footer in grid view and how to calculate total qty in footer by using label control

sankarreddy


Responses

Posted by: Pradeepkumar417 on: 2/1/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

You can add footer to gridview control by using footer template like below.

<Columns>
<asp:TemplateField HeaderText="Header1">
<ItemTemplate>

</ItemTemplate>
<EditItemTemplate>

</EditItemTemplate>
<FooterTemplate>

</FooterTemplate>
</asp:TemplateField>
</Columns>

Thanks & Regards,
Software Engineer,
Pradeep Kumar

Thanks & Regards,
Software Engineer,
Pradeep Kumar

reddysankark-13471, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Deepak_shr on: 2/1/2012 [Member] Starter | Points: 25

Up
0
Down
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

Login to post response