.aspx页面中
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle Width="80px"></ItemStyle>
<HeaderTemplate>
<asp:Literal id="Literal3" Runat="server" Text="支付金额"></asp:Literal>
</HeaderTemplate>
<ItemTemplate>
<asp:Literal id=Literal4 Runat="server" Text='<%#PayReal((string)DataBinder.Eval(Container,"DataItem.PayWayCash").ToString(),(string)DataBinder.Eval(Container,"DataItem.PayWayCheck").ToString())%>'>
</asp:Literal>
</ItemTemplate>
</asp:TemplateColumn>
.cs页面中
AlreadyPays_ItemDataBound中
double totals=0;
for(int i=0;i<AlreadyPays.Items.Count;i++)
{
Response.Write("<script>window.alert('"+e.Item.Cells[7].Text.ToString()+"');</script>");
totals+=Convert.ToDouble(AlreadyPays.Items[i].Cells[7].Text.ToString());
}
AlreadyPays.Columns[0].FooterText="合计:";
AlreadyPays.Columns[7].FooterText=string.Format("{0:c}",totals.ToString());
可是一运行,总是提示出错
输入字符串的格式不正确。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 
异常详细信息: System.FormatException: 输入字符串的格式不正确。
源错误: 
行 223: {
行 224:行 225:出错
totals+=Convert.ToDouble(AlreadyPays.Items[i].Cells[7].Text.ToString());
弄了好几天了,老出问题

解决方案 »

  1.   

    跟据数学的计算方式,看看,你的值对不对
    比如:AlreadyPays.Columns[7].FooterText
    我看你用了合计,如果有字符串那肯定不能计算,理清思路。
      

  2.   

    string.Format("{0:c}",totals.ToString())
    问题出在这里 格式化代码{0:c}不对
      

  3.   

    string.Format("{0:c}",totals.ToString())
    问题出在这里 格式化代码{0:c}不对
    ——————————————————————
    不对呀,结果还是0
      

  4.   

    我在累加前用Response.Write("<script>window.alert('"+e.Item.Cells[7].Text.ToString()+"');</script>");结果就是0,这是怎么回事呀
      

  5.   

    问题在这里:
    关于ItemDataBound事件,由于这个事件是在每一个Item绑定时触发,也就是在第一行触发该事件时第二行还没有绑定数据,所以出错,e.Item是当前正在绑定的行,所以没问题;解决: 1)不要用ItemDataBound事件了,把你循环的代码跟在AlreadyPays.DataBind();后面就可以了
    2)
    double totals=0;
    AlreadyPays_ItemDataBound(....)
    {
       if(e.Item.ItemIndex>-1)
        {
           totals+=Convert.ToDouble(AlreadyPays.Items[i].Cells[7].Text.Trim().Replace("&nbsp;","0"));   }
       else if(e.Item.ItemType == ListItemType.Footer)
       {
             e.Item.Cells[0].Text ="total";
             e.Item.Cells[7].Text = totals.ToString("#0.00");
        }
    }