在ROWDATABOUND事件里统计编写,看看MSDN关于此事件的介绍。

解决方案 »

  1.   

    int x = 0;
        int y = 0;
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.Cells[4].Text)
            {
                case "按时":
                    x += 1;
                    break;
                case "延期":
                    y += 1;
                    break;
                default:
                    break;
            }
        }
      

  2.   

    谢谢shoushii!!但我运行后出现如下错误:“指定的参数已超出有效值的范围。参数名: index”这是为什么呢??
      

  3.   

    应该写在ROWDATABOUND事件里,因为ROWCREATED事件,此时数据还没加载
    强烈建议你看看MSDN  ROWDATABOUND
      

  4.   

    谢谢only-endure!!但我是写在RowDataBound里的呀。把我的代码粘贴如下:
    int x = 0;
    int y = 0;       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {               
           
            switch (e.Row.Cells[4].Text)
           {
              case "按时完工":
                   x += 1;
                  break;
              case "延期完工":
                   y += 1;
                  break;
              default:
                   break;
          }
         
            //  自动统计
            if (e.Row.RowType == DataControlRowType.Footer)        {                     float f = ((float)(x + y) / GridView1.Rows.Count) * 100;            String str = String.Format("{0:f1}", f);            e.Row.Cells[3].Text = str + "%";
               
                e.Row.Cells[5].Text = x.ToString();            e.Row.Cells[7].Text = y.ToString();
                
            }    }
      

  5.   

    现在是这样的情况:如果照shoushii的方法,代码如下: int x = 0;
        int y = 0;
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            switch (e.Row.Cells[4].Text)
            {
                case "按时完工":
                    x += 1;
                    break;
                case "延期完工":
                    y += 1;
                    break;
                default:
                    break;
            }
        }
     protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
     //  自动统计
            if (e.Row.RowType == DataControlRowType.Footer)        {                     float f = ((float)(x + y) / GridView1.Rows.Count) * 100;            String str = String.Format("{0:f1}", f);            e.Row.Cells[3].Text = str + "%";
               
                e.Row.Cells[5].Text = x.ToString();            e.Row.Cells[7].Text = y.ToString();
                
            }出现的结果是x,y没有自增,在最后的统计结果中仍然是0.若照only-endure的方法,代码已在上面,出现的结果是“指定的参数已超出有效值的范围。参数名: index”这是为什么呢??
      

  6.   

    还要判断行的类型。看看MSDN
    if(e.Row.RowType == DataControlRowType.DataRow)
    {   switch (e.Row.Cells[4].Text)
            {
                case "按时完工":
                    x += 1;
                    break;
                case "延期完工":
                    y += 1;
                    break;
                default:
                    break;
            }
    晕,我不是说过了么,这些数据的东西都放在ROWDATABOUND事件中。
      

  7.   

    请原来我的无知吧!5555,现在时间紧,看MSDN有点没头绪的感觉。请“提刑官”再帮帮忙吧。我把代码修改如下,但统计结果还是为0,没有变化啊。是不是那个 switch (e.Row.Cells[4].Text)语句有问题?我的那列用的是Label控件,SQL数据库中的格式为nvarchar,这有关系吗??盼复,谢谢!!
     int x = 0;
        int y = 0;
       
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {         
                       
                if (e.Row.RowType == DataControlRowType.DataRow)
                {              //  自动统计
                    switch (e.Row.Cells[4].Text)
                    {
                        case "按时完工":
                            x += 1;
                            break;
                        case "延期完工":
                            y += 1;
                            break;
                        default:
                            break;
                    }
                }
               else if (e.Row.RowType == DataControlRowType.Footer)
                {                float f = ((float)(x + y) / GridView1.Rows.Count) * 100;                String str = String.Format("{0:f1}", f);                e.Row.Cells[3].Text = str + "%";                e.Row.Cells[5].Text = x.ToString();                e.Row.Cells[7].Text = y.ToString();            }             
             
        }
      

  8.   

    加个trim试下int x = 0;
        int y = 0;
       
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {         
                       
                if (e.Row.RowType == DataControlRowType.DataRow)
                {              //  自动统计
                         //ken update
                    switch (e.Row.Cells[4].Text.Trim())
                    {
                        case "按时完工":
                            x += 1;
                            break;
                        case "延期完工":
                            y += 1;
                            break;
                        default:
                            break;
                    }
                }
              if (e.Row.RowType == DataControlRowType.Footer)
                {                float f = ((float)(x + y) / GridView1.Rows.Count) * 100;                String str = String.Format("{0:f1}", f);                e.Row.Cells[3].Text = str + "%";                e.Row.Cells[5].Text = x.ToString();                e.Row.Cells[7].Text = y.ToString();            }             
             
        }