Reputation: 89
Here is the screenshot of my webpage having two panels. The first panel fills grid view in the second panel. columns total fee , Registration fee,Installments, pending fee are Template fields. i want to calculate the pending fee = Total Fee - Registration fee - ( sum of installments) in the footer of pending fee column. i also want to display the sum of installments paid in the footer.
Can someone beam me up how to go about it.
This is the output i am getting, and below it is the source code of the page
<asp:TemplateField HeaderText="Total Fee">
<ItemTemplate>
<asp:Label ID="Lbl_TotalFee" runat="server" Text='<%# Eval("total_fee") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Registration Fee">
<ItemTemplate>
<asp:Label ID="LblRegFee" runat="server" Text='<%# Eval("reg_fee") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LblInstallments" runat="server" Text="Total Installments"/>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Installments">
<ItemTemplate>
<asp:Label ID="Lbl_Installment" runat="server"
Text='<%# Eval("installments")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LblTotalIns" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Pending Fee">
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="LblFinalPendingFee" runat="server" ></asp:Label>
</FooterTemplate>
</asp:TemplateField>
This is the code behind....
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
double sum = 0;
if (e.Row.RowType == DataControlRowType.DataRow)
{
//double installments = (e.Data.DataItem as double).installments;
if (DataBinder.Eval(e.Row.DataItem, "installments") != DBNull.Value)
{
sum += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "installments"));
en.Fee_TotalSumOfInstallments = sum.ToString();
}
else
{
en.Fee_TotalSumOfInstallments = "nill";
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label LblTotalIns = (Label)e.Row.FindControl("LblTotalIns");
LblTotalIns.Text = en.Fee_TotalSumOfInstallments.ToString();
Label LblFinalPendingFee = (Label)e.Row.FindControl("LblFinalPendingFee");
double PendFee=(Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "total_fee"))-(Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "reg_fee"))+sum));
en.Fee_TotalPendingFee=PendFee.ToString();
LblFinalPendingFee.Text = en.Fee_TotalPendingFee.ToString();
//string abc = (Convert.ToInt32(e.Row.FindControl("Lbl_TotalFee")) - Convert.ToInt32(e.Row.FindControl("LblRegFee")) - total).ToString();
}
}
The problem is that the pending fee is always calculated 0. where am i wrong here ? pls help!
Thanks.
Upvotes: 0
Views: 199
Reputation: 9489
Declare page level properties:
public double TotalPendingFee { get; set; }
public double TotalSumOfInstallments { get; set; }
Have a footer template similar to this for every footer you need:
<asp:TemplateField HeaderText="PendingFee">
<ItemTemplate>
<asp:Label ID="lblPendingFee" runat="server" Text='<%# Eval("PendingFee")%>' />
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotalPendingFee" runat="server" />
</FooterTemplate>
</asp:TemplateField>
During the row data bound, we can intercept every record and calculate the footer values. and then set it when the footer is bound.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double pendingFee = (e.Data.DataItem as YourType).PendingFee;
// same for installments.
TotalPendingFee += pendingFee; // use your formula
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalPendingFee = (Label)e.Row.FindControl("lblTotalPendingFee ");
lblTotalPendingFee.Text = TotalPendingFee.ToString();
// same for sum of installments
}
}
Also, if PendingFee is not a DB column, then you can create a getter only property on the Dat class (which has TotalFee, RegistrationFee) and evaluate PendingFee.
The Eval("PendingFee") need not change.
the property will:
public class DataClass
{
public double PendingFee
{
get
{
return this.TotalFee - this. RegistrationFee - 0; // your formula.
}
}
}
Upvotes: 1