日期:2014-05-17  浏览次数:20428 次

时能帮我看看这行为嘛报错啊。Gridview的一个问题,谢谢了
  
if (_myDs.Tables[_orderItemsName].Rows.Count == 1)
        {
            gvItems.FooterRow.Cells[2].Text = _myDs.Tables[_orderItemsName].Rows[0]["Price"].ToString();
            Response.Write(gvItems.FooterRow.Cells[2].Text.ToString());
            int temp = Convert.ToInt32(gvItems.FooterRow.Cells[2].Text.ToString());
            Response.Write(temp);
        }
        else
        {
            int temp = Convert.ToInt32(gvItems.FooterRow.Cells[2].Text.ToString());
            gvItems.FooterRow.Cells[2].Text = (temp + Convert.ToInt32((lblClothPrice.Text).ToString())).ToString();
            //gvItems.FooterRow.Cells[2].Text = Convert.ToInt32(gvItems.FooterRow.Cells[2].Text.ToString()) + Convert.ToInt32(lblClothPrice.Text.ToString());
        }

当rowcount是1的时候,if statement里面的运行正常,当rowcount到了2的时候执行else statement 就报错
Exception Details: System.FormatException: Input string was not in a correct format.
Line 122:  int temp = Convert.ToInt32(gvItems.FooterRow.Cells[2].Text.ToString());

gvItems.FooterRow.Cells[2]是一个integer,而且当我运行第一个if的时候gvItems.FooterRow.Cells[2]里面已经赋值了。为嘛到了else 就报错呢,而且有人和我说可能是white space的问题我去看了看html,html里面的第三个cell就是个数字没有任何space

谢谢。谢谢

------解决方案--------------------
int temp=0
if (!string.IsNullOrWhiteSpace(gvItems.FooterRow.Cells[2].Text.ToString()))
{
temp = Convert.ToInt32(gvItems.FooterRow.Cells[2].Text.ToString());
}