在单元格的gridview计算中,输入字符串的格式不正确

时间:2014-01-16 07:37:24

标签: c# asp.net gridview row computation

我尝试在此link中进行计算,但它表示输入字符串的格式不正确。我正在计算gridview中单元格的值。 这是我的代码:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    int tot = 0;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
          decimal medjtot = (Convert.ToInt32(e.Row.Cells[3].Text) * (Convert.ToDecimal(e.Row.Cells[4].Text) / 12)) * Convert.ToDecimal(e.Row.Cells[5].Text);
          Label RowTotal = (Label)e.Row.FindControl("Label1");
          RowTotal.Text = medjtot.ToString();    
    }
 }

2 个答案:

答案 0 :(得分:1)

请确保对TextRow Cell属性进行警卫检查。文字可能是whitespaceempty

样品保护检查。

   if(string.IsNullOrWhiteSpace(e.Row.Cells[5].Text) || 
       string.IsNullOrWhiteSpace(e.Row.Cells[3].Text))
          return;

答案 1 :(得分:0)

必须在这一行:

decimal medjtot = (Convert.ToInt32(e.Row.Cells[3].Text) * (Convert.ToDecimal(e.Row.Cells[4].Text) / 12)) * Convert.ToDecimal(e.Row.Cells[5].Text);

如果您的单元格包含forbiden字符(例如int32的字母),则转换将失败。检查文化,十进制的分隔符可能是昏迷或点。