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
Marimuthu ArigovindasamyPosted Jun 15, 2010, 1:23 AM
Marimuthu ArigovindasamyPosted Jun 7, 2010, 1:38 AM
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();
}
}
CrishPosted Jun 7, 2010, 1:19 AM
write this "Total" field in Footer template.
thanks
Don't forget to Mark Do you like this Answer
that solved your problem!