我是在代码中执行把数据绑定到控件Gridview上的,我执行查询的时候Gridview里面已经显示了8行数据
然后我执行下面的代码
int columns = Convert.ToInt32(DropDownList1.SelectedValue) - 1;
        //得到当前的正确列数
        GridView1.Columns[columns].Visible = false;
索引超出范围。必须为非负值并小于集合大小。
参数名: index
其实这个时候columns=7;

解决方案 »

  1.   

    Gridview没有Cell这个属性呀
    不然怎么设置
      

  2.   

    columns值超出范围
    <asp:BoundField DataField="BH" HeaderText=""> 
    <ControlStyle CssClass="hidden" /> 
    <FooterStyle CssClass="hidden"/> 
    <HeaderStyle CssClass="hidden"/> 
    <ItemStyle CssClass="hidden"/> 
    </asp:BoundField> 
    .hidden 

    display:none; 

      

  3.   


    设置visibl=false是取页面上是取不到那列的,就是7列啦,用上面的css代码隐藏就可以取到隐藏列
      

  4.   

    我加了
    提示错误
    错误 2 类型“System.Web.UI.WebControls.GridView”不具有名为“ItemStyle”的公共属性。 D:\excel\程序\Default.aspx 80
      

  5.   

    错误 3 验证 (XHTML 1.0 Transitional): 此名称包含大写字符,而名称中不允许使用大写字符。 D:\excel\程序\Default.aspx 79 27 D:\excel\程序\
      

  6.   

    前面也看到你的帖子了,你说GridView1.Columns.Count=0 
    初步判断,你可能DropDownList的选择事件后,设置GridView1的隐藏,但是此时你的GridView1由于回传,还没有来得及绑定,所以你此时的GridView1的数据还是空的。
    建议你在绑定我绑定数据的代码时来隐藏需要的列:
                  OleDbDataAdapter olesda = new OleDbDataAdapter(yuju, oledbcon); 
                DataSet ds = new DataSet(); 
                olesda.Fill(ds, "dat"); 
                GridView1.DataSource = ds.Tables["dat"]; 
                GridView1.DataBind(); 
                int columns = Convert.ToInt32(DropDownList1.SelectedValue) - 1; 
                GridView1.Columns[columns].Visible = false; 
                olesda.Dispose(); 
                ds.Dispose(); 
                oledbcon.Close(); 
      

  7.   

    绑定时在后台的bond事件里写cell的style.add("display","none");
      

  8.   

    应该是这样,在GridView的RowCreated事件中获得列数
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableCellCollection cells1 = e.Row.Cells;
                ViewState["no"] = cells1.Count.ToString();//这就是你要的列数
            }
        }在GridView的PreRender事件中设置
        protected void GridView1_PreRender(object sender, EventArgs e)
        {
            if(GridView1.Rows.Count>0)
                GridView1.Columns[Convert.ToInt16(ViewState["no"])-1].Visible = false; 
        }
      

  9.   

    LZ的那段代码似乎是  天轰穿vs2005视频里的?
      

  10.   

    看看selectValue的值之后你再看看那里错了吧 
      

  11.   

    13楼稍微有误,应该是直接在RowCreated隐藏
        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableCellCollection cells1 = e.Row.Cells;
                int i1 = cells1.Count;
                cells1[i1 - 1].Visible = false;//隐藏最后一列标题
            }        if (e.Row.RowType == DataControlRowType.DataRow)
            {
                TableCellCollection cells2 = e.Row.Cells;
                int i2 = cells2.Count;
                cells2[i2 - 1].Visible = false;//隐藏最后一列数据
            }
    }
      

  12.   

    可是如果在RowCreated隐藏
    我不如不查询这列就好了
    我只是在查询的时候想看到
    然后在要打印的时候把他给隐藏掉
      

  13.   

    事实上不一定在RowCreated事件下隐藏,也可以这样在Button的click下,如
        protected void Button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].Cells[7].Visible = false;
            }
        }
    当然你还应该考虑隐藏标头,其中的7,应该是RowCreated事件下获得!!
      

  14.   

    呵呵!!楼主到底要干什么??
    贴子不是隐藏列吗??
    隐藏表头:
     GridView1.HeaderRow.Cells[7].Visible = false;
      

  15.   

    C#中GridView隐藏列的方法
    http://blog.csdn.net/hustypf/article/details/7797333
      

  16.   

    http://blog.csdn.net/hustypf/article/details/7797333
    C#中GridView隐藏列的方法
      

  17.   

    绑定时直接设置 某列隐藏,会出错
    下面有两种方法,不仅可以隐藏,还仍然可以正常取值
    方法一:
    在RowCreated事件中书写如下代码   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
             } 
            //可以根据需要设置更多的列
         }
         因为在RowCreated事件(隐藏)在绑定时候发生,所以这样就即能将数据绑定到列上,又隐藏了该列.所
    以可以访问到隐藏列的值
    方法二:
    Public   void myTestFunction()
    {
       string conString="....";//省略
         string sqlquery="...";//省略
        SqlConnection con = new SqlConnection(conString);
             SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
             DataSet ds = new DataSet();
             da.Fill(ds);
             ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
             GridView1.DataSouce = ds.Tables[0];
             GridView1.DataBind() ;}
      

  18.   

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.Pager)//如果不是
            {
                //将GRIDVIEW的第一列隐藏
                e.Row.Cells[0].Visible = false;
            }
        }