问题是这样的,我在后台动态绑定了GridView的数据源,并且生成了。前台代码只有这么多
     <asp:GridView ID="gvPersons" runat="server" AllowPaging="True" AllowSorting="True"  OnPageIndexChanging="gvPersons_PageIndexChanging" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
         <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
         <RowStyle BackColor="White" ForeColor="#330099" />
         <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
         <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
         <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
         
     </asp:GridView>
其中一部分是样式的。
但是因为生成的Gridview的列太多,造成页面加宽了很多 ,我想将一部分不需要的列隐藏掉,所以我用gvPersons.Columns[0].Visible = false;.
然后问题就来了,运行时它报错了,它说:
用户代码未处理 System.ArgumentOutOfRangeException
  Message="索引超出范围。必须为非负值并小于集合大小。\r\n参数名: index"
  Source="mscorlib"
  ParamName="index"
  StackTrace:
       在 System.Collections.ArrayList.get_Item(Int32 index)
       在 System.Web.UI.StateManagedCollection.System.Collections.IList.get_Item(Int32 index)
       在 System.Web.UI.WebControls.DataControlFieldCollection.get_Item(Int32 index)
       在 Search.setGridView() 位置 f:\MOPSDesk1112\Web\Search.aspx.cs:行号 193
       在 Search.Page_Load(Object sender, EventArgs e) 位置 f:\MOPSDesk1112\Web\Search.aspx.cs:行号 26
       在 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       在 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       在 System.Web.UI.Control.OnLoad(EventArgs e)
       在 System.Web.UI.Control.LoadRecursive()
       在 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)不知道应该怎么弄好,请各位高手们帮帮忙,大家讨论讨论。

解决方案 »

  1.   

    gvPersons.Columns[0].Visible = false;//这句在哪里写的?
      

  2.   

       void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
         {
             if (e.Row.RowType == DataControlRowType.DataRow || 
                 e.Row.RowType == DataControlRowType.Header)
             {
                 e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
             } 
            //可以根据需要设置更多的列
         }
      

  3.   

    RowCreated 你这个是创建事件.这一行都没有创建出来,你又怎么去调用 e.Row.Cells[0] 这一行的第一个格子呢?? 
      

  4.   

    http://hi.baidu.com/zhaozhijin/blog/item/f7507c33ec4d36fa1a4cff59.html 可以参考这个