取出来放到datatable里做判断有什么问题么

解决方案 »

  1.   

    数据里有空的情况,下面这句就出现:输入字符串的格式不正确。
      sum1 += Convert.ToDouble(e.Row.Cells[14].Text); 
      

  2.   

    整段代码如下:  protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
          
          if (e.Row.RowIndex >= 0)
          {
           if (e.Row.Cells[14].Text != "")
              {
                 sum1 += Convert.ToDouble(e.Row.Cells[14].Text);
              }         
          // e.Row.Cells[14].Text = e.Row.Cells[14].Text.Length.ToString();
           }
          else if (e.Row.RowType == DataControlRowType.Footer)
          {           e.Row.Cells[14].Text = sum1.ToString();
             
          }
        }
      

  3.   


    //try:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
          
          if (e.Row.RowIndex >= 0) 
          { 
              if (e.Row.Cells[14].Text.Trim() != "")  //另外最好先作个格式校验,万一用户输入其他值,且不是报错?
              { 
                sum1 += Convert.ToDouble(e.Row.Cells[14].Text.Trim()); 
              }        
          } 
          else if (e.Row.RowType == DataControlRowType.Footer) 
          {           e.Row.Cells[14].Text = sum1.ToString(); 
          } 
        }
      

  4.   

    数据库字段,是number类型的,再插入的时候就避免了
      

  5.   

    晕,看来是我没写明白。
    我说的意思是:
       在gridview中,合计一列的数据,如果有一个数据没有填,怎么把他避开,我做的代码为什么会出错?
      

  6.   

    避开是什么意思?
    if(!String.IsNullOrEmpty(e.Row.Cells[14].Text))
       sum1 += Convert.ToDouble(e.Row.Cells[14].Text); 
    ......
      

  7.   

    sum1 += Convert.ToDouble(e.Row.Cells[14].Text); 
    这句根本就有问题,Convert.ToDouble(e.Row.Cells[14].Text)如查e.Row.Cells[14].Text是空那在去转成DOUBLE那是一定要出问题的类型转换错误呀
    要先判断
              if(e.Row.Cells[14].Text==null)
             { sum1 += Convert.ToDouble(0); 
    }
             else
               {sum1 += Convert.ToDouble(e.Row.Cells[14].Text); 
    }
    这样我想是不会出问题的。
    只是没有试过。
      

  8.   

    我终于知道是什么原因了,没有值的地方是DBNull类,请问各位老师,上面的问题的if语句怎么写?
      

  9.   

    你说的问题一般都直接在sql里就处理掉
    在sqlserver使用isnull函数例如select isnull(price,0) as price from orders                             --如果price为null的话,用0替换在oracle下使用Nvl函数
    select  nvl(price,0) form orders  --如果price为null的话,用0替换
      

  10.   

      if (e.Row.Cells[14].Text != DBNull.value ) 
              { 
                  sum1 += Convert.ToDouble(e.Row.Cells[14].Text); 
              }
      

  11.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
          
          if (e.Row.RowIndex >= 0) 
          { 
           double dblvalue = 0;
          if (double.TryParse(e.Row.Cells[14].Text ,out dblvalue)) 
              { 
                sum1 += dblvalue;
               }
    方法2:
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
        { 
          
          if (e.Row.RowIndex >= 0) 
          { 
           double dblvalue = 0;
          if (e.Row.Cells[14].Text != DBNull.Value && !string.IsNullOrEmpty(e.Row.Cells[14].Text))
              { 
                sum1 += ...;
               }