Dear all;
I have a gridview which has two columns. The first column is called the date and the second column is called the amount. I would like a situation where I would be able to add an extra row at the end of the gridview and use it to determine the total amount. see example below.
Month amount
2001-01 20
2001-02 30
2001-03 40
Total 90 (this is the extra row I am talking about). It should be added and include the summation of the amount)
So how do you add this extra row and perform the summation in gridview. Thank you.
Loading

CrishRank #1,487 · 1.1K reputationPosted Jun 6, 2010, 9:19 PM EDT
write this "Total" field in Footer template.
thanks
Don't forget to Mark Do you like this Answer
that solved your problem!
Marimuthu ArigovindasamyRank #5,893 · 283 reputationPosted Jun 6, 2010, 9:38 PM EDT
ASPX page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Month">
<ItemTemplate>
<asp:Label ID="lblMonth" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Month")%>'>asp:Label>
ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalDesc" runat="server" Text="Total">asp:Label>
FooterTemplate>
asp:TemplateField>
<asp:TemplateField HeaderText="Amount">
<ItemTemplate>
<asp:Label ID="lblAmount" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Amount")%>'>asp:Label>
ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" Text="" runat="server">asp:Label>
FooterTemplate>
asp:TemplateField>
Columns>
asp:GridView>
ASPX.CS code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "Amount"));
totalAmount = totalAmount + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = totalAmount.ToString();
}
}
Marimuthu ArigovindasamyRank #5,893 · 283 reputationPosted Jun 14, 2010, 9:23 PM EDT