输入字符串在单元格的网格视图计算中格式不正确



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

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();    
    }
 }
确保对

每个Row CellText属性进行警卫检查。文本可能是whitespace的或empty的。

样品防护检查。

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

必须在此行:

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 的字母),则转换将失败。还要检查区域性,小数分隔符可以是昏迷或点。

最新更新