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

当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
            { 
                //处理
            }