GridView设置了AllowPaging为False的话,OnRowDataBound中可以用
e.Rows.Cells[2].Text
但如果设置为True后,OnRowDataBound在运行的时候:
异常详细信息: System.ArgumentOutOfRangeException: 指定的参数已超出有效值的范围。
参数名: index

解决方案 »

  1.   

    GridView.rows(index)
    是以当前一页为单位的,
    index是总共的数据行数的话,那当然超出了
    比如80行 一页10号
    index到10的时候就异常了
      

  2.   

    确实是这样,但你的目的是什么啊?不行可以遍历一下
            foreach (GridViewRow thisItem in GridView1.Rows)
            {
                
            }
      

  3.   

    protected void GridView_OnRowDataBound(...)
    {
      if(e.Row.RowIndex>-1)//需要加上个判断条件
      {
         string tmp = e.Row.Cells[2].Text;
      }
    }
      

  4.   

    原因是启动了AllowPaging后,GridView的翻页这一行业算一行,也会触发OnRowDataBound事件,然而这一行跟其他数据行不同,它只有一格单元格,所以这是候调用Cells[2]就报超出索引的错误
      

  5.   

    谢谢楼上,问题的确如此再问个问题:
    两个GridView,导出为Excel表,其一导出结果正常,而另一个得到的Excel表中汉字显示全是乱码?
      

  6.   

    再问个问题:
    两个GridView,导出为Excel表,其一导出结果正常,而另一个得到的Excel表中汉字显示全是乱码?====贴出你的导出代码:
      

  7.   

    public static void GridViewToExcel(System.Web.UI.Control ctl, string FileName)//将GridView中的内容导出到Excel表中
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Charset = "UTF-8";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
            HttpContext.Current.Response.ContentType = "application/ms-excel";
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter wt = new System.IO.StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(wt);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(wt.ToString());
            HttpContext.Current.Response.End();
        }