gridview的模板字段中的DataManipulation

时间:2014-04-08 16:53:06

标签: asp.net gridview templatefield

这是我的网页截图,有两个面板。第一个面板填充第二个面板中的网格视图。列总费,注册费,分期付款,待定费用是模板字段。 我想在待定费用栏的页脚中计算待定费用=总费用 - 注册费 - (分期付款总额)。我还想在页脚中显示支付的分期付款总额enter image description here

有人可以告诉我如何解决这个问题。

这是我得到的输出,下面是页面的源代码

enter image description here

 <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>

这是背后的代码......

 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();
    }  
}

问题是待处理的费用总是计算在0.我在哪里错了?请帮助!

感谢。

1 个答案:

答案 0 :(得分:1)

声明页面级属性:

public double TotalPendingFee { get; set; }
public double TotalSumOfInstallments { get; set; }

为您需要的每个页脚添加与此类似的页脚模板:

<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>

在行数据绑定期间,我们可以拦截每个记录并计算页脚值。然后在绑定页脚时设置它。

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 
   } 
}

此外,如果PendingFee不是数据库列,那么您可以在Dat类(具有TotalFee,RegistrationFee)上创建仅限getter属性并评估PendingFee。

Eval(&#34; PendingFee&#34;)无需更改。

该物业将:

public class DataClass
{
 public double PendingFee
 {
  get
  {
   return this.TotalFee - this. RegistrationFee - 0; // your formula.
  }
 }
}