我需要设置 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/