1

我试图在这个链接中进行计算,但它说输入字符串的格式不正确。我正在计算网格视图中单元格的值。这是我的代码:

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();    
    }
 }
4

2 回答 2

1

确保您Text对每个Row Cell. 文本可能是whitespaceempty

样品保护检查。

   if(string.IsNullOrWhiteSpace(e.Row.Cells[5].Text) || 
       string.IsNullOrWhiteSpace(e.Row.Cells[3].Text))
          return;
于 2014-01-16T07:41:30.673 回答
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);

如果您的单元格包含禁止字符(例如 int32 的字母),则转换将失败。还要检查文化,小数点的分隔符可能是逗号或点。

于 2014-01-16T07:46:30.240 回答