首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

当GridViewz中的Cells[4]。Text为空时,出错呀 如何解决

2012-08-11 
当GridViewz中的Cells[4]。Text为空时,出错呀 怎么解决protected void GridView1_RowDataBound(object send

当GridViewz中的Cells[4]。Text为空时,出错呀 怎么解决
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  {
  string sum = "";

  if (e.Row.RowType == DataControlRowType.Header)
  {

  }

  if (e.Row.RowType == DataControlRowType.DataRow)
  {
  sum += Convert.ToDouble(e.Row.Cells[4].Text)*0.7 + Convert.ToDouble(e.Row.Cells[5].Text)*0.2 + Convert.ToDouble(e.Row.Cells[6].Text)*0.1;
  e.Row.Cells[7].Text = sum;
  }
}

[解决办法]
e.Row.Cells[4].Text先判断下吧

或者三目运算符解决
[解决办法]

C# code
sum += Convert.ToDouble(e.Row.Cells[4].Text)*0.7 + Convert.ToDouble(e.Row.Cells[5].Text)*0.2 + Convert.ToDouble(e.Row.Cells[6].Text)*0.1;把这个分开,一个一个写!double a = Convert.ToDouble(e.Row.Cells[4].Text  == "" ? 0 : e.Row.Cells[4].Text);double b = Convert.ToDouble(e.Row.Cells[5].Text  == "" ? 0 : e.Row.Cells[5].Text);double c = Convert.ToDouble(e.Row.Cells[6].Text  == "" ? 0 : e.Row.Cells[6].Text);sum += a+b+c;
[解决办法]
C# code
  //1.          if (e.Row.Cells[4].Text.ToString() != "")            {                 //处理            }   //2.//模板列的话这么处理            TextBox txtPrice = e.Row.FindControl("tbPrice") as TextBox;            if (txtPrice.Text.ToString() == "")            {                //处理            }            else            {                 //处理            } 

热点排行
Bad Request.