c# - 如何在 C# 中的 gridview 中设置页脚中特定列的总计?

标签 c# asp.net

我需要设置 GridView 中特定列的总计。

我的代码是:

<asp:TemplateField>
     <HeaderTemplate>
         Amount
     </HeaderTemplate>
     <ItemTemplate>
         <asp:Label ID="lblAmt" HeaderText="Amount" runat="server" 
              Text='<%# Eval("Amount")%>' Visible="true">
         </asp:Label>
     </ItemTemplate>
     <FooterTemplate>
         <asp:Label ID="lblTotalAmt" runat="server" />
     </FooterTemplate>
</asp:TemplateField>    

然后:

decimal totProfit = 0M;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
          Label lblAmt = (Label)e.Row.FindControl("lblAmt");
          decimal Profitprice = Decimal.Parse(lblAmt.Text);
          totProfit += Profitprice;
     }
     if (e.Row.RowType == DataControlRowType.Footer)
     {
          Label lblTotalAmt = (Label)e.Row.FindControl("lblTotalAmt");
          lblTotalAmt.Text = totProfit.ToString();
     }
 }     

但是错误如下:

Input string was not in a correct format.

最佳答案

整数转换期间 MS 识别出一个错误,该错误可能会在此处发挥作用 ( http://support.microsoft.com/kb/942460 )

另一个选项是确保它是“金额”字段中的数字。

decimal totProfit = 0M;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lblAmt = (Label)e.Row.FindControl("lblAmt");
        decimal Profitprice; 
        if (Decimal.TryParse(lblAmt.Text, Profitprice) ) {
             totProfit += Profitprice;
        }
    }
    if (e.Row.RowType == DataControlRowType.Footer)
    {
        Label lblTotalAmt = (Label)e.Row.FindControl("lblTotalAmt");
        lblTotalAmt.Text = totProfit.ToString();
    }
}     

关于c# - 如何在 C# 中的 gridview 中设置页脚中特定列的总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16565893/

相关文章:

c# - 是否有任何一个网站包含许多优秀的 C# 截屏视频?

c# - 在 .NET 中编写 T-SQL 功能

c# - 如何从整数生成 MD5 哈希(32/64 个字符)

c# - 二维离散傅里叶变换的复杂性

c# - 如何以编程方式为 IIS7 创建子域?

c# - Android 到 Web 服务。未传递的值

c# - 使用 C# 在 HTML 中查找特定类并获取其值

c# - SEO:自定义站点地图提供程序与静态 web.sitemap 文件

asp.net - 避免 ASP.NET 中的 session 劫持

c# - 从其他 C# 服务器端 Controller 调用 ASP.NET post 路由