【问题标题】:DataManipulation in templatefields of a gridview网格视图的模板字段中的数据操作
【发布时间】:2014-05-21 12:40:54
【问题描述】:

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

谁能告诉我如何去做。

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

 <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。我在哪里错了?请帮忙!

谢谢。

【问题讨论】:

    标签: asp.net gridview templatefield


    【解决方案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 不是 DB 列,那么您可以在 Dat 类(具有 TotalFee、RegistrationFee)上创建一个仅 getter 属性并评估 PendingFee。

    Eval("PendingFee") 不需要改变。

    房产将:

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

    【讨论】:

    • HI 谢谢你的支持兄弟,但我对itemtemplate标签中asp标签的文本属性有疑问..即:未决费用列不从数据库中取值,因为没有列对于数据库中的待处理费用,文本属性中的语句 Eval("PendingFee") 是否有效?
    • 谢谢你,我从你的回答中得到了分期付款的总额,但未决费用总是为 0,我已经编辑了这个问题,你能帮忙看看我哪里出错了。 .. :)
    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2016-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多