简述:用GridView显示数据,并且要可编辑和删除,在页脚插入求和值。写了求和代码之后,点编辑的时候会出错,删除和插入都正常。
列求和代码如下:
    private double sum = 0;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
       
          if (e.Row.RowIndex >= 0)
        {
           sum += Convert.ToDouble(e.Row.Cells[4].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[4].Text = "总人数:" + sum.ToString();
        }出错信息是:输入字符串的格式不正确。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.FormatException: 输入字符串的格式不正确。源错误: 
行 27:         if (e.Row.RowIndex >= 0)
行 28:         {
行 29:            sum += Convert.ToDouble(e.Row.Cells[4].Text);
行 30:         }
行 31:         else if (e.Row.RowType == DataControlRowType.Footer)
 
帮忙看看是哪里的问题

解决方案 »

  1.   

    decimal sum=0;
                foreach (GridViewRow row in GridView1.Rows)
                {
                    sum += Convert.ToDecimal(row.Cells[求和那一列的索引].Text);
                }
                Label1.Text = sum.ToString(); 
    这段不该放在GridView1_RowDataBound事件里
    放在一个自定义的绑定方法里就行了
      

  2.   

    sum += Convert.ToInt32(e.Row.Cells[3].Text);
    改成
    if(e.Row.Cells[3].Text!="")
    {
        sum += Convert.ToInt32 (e.Row.Cells[3].Text);
    }这样可以了  但是列里数值是百分数的话 就统计不了 出错了 谁知道百分数怎么统计啊
      

  3.   

    if(e.Row.Cells[3].Text!="")
    {
      sum += Convert.ToInt32 (e.Row.Cells[3].Text.Replace("%",""));
    }把百分号%替换掉。
      

  4.   


    原始数据就是百分数 能转换成Convert.ToDouble 么。。
    private int sum = 0;
    sum += Convert.ToInt32 (e.Row.Cells[3].Text.Replace("%",""));
      

  5.   

    编译错误 
    说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误消息: CS0117: “System.Convert”并不包含“Int32”的定义源错误: 行 111:            if (e.Row.Cells[10].Text != "")
    行 112:            {
    行 113:                sum += Convert.Int32(e.Row.Cells[10].Text.Replace("%","")); 
      

  6.   


    Convert.Convert.ToInt32(e.Row.Cells[10].Text.Replace("%","")); 
      

  7.   

    Convert.ToInt32(e.Row.Cells[10].Text.Replace("%",""));