数据保存在DataTable里,然后DataGrid.datasource=DT;DataGrid.Databind();
但DT中的个别列用户不需要(但作为连接条件,必须存在)。
用DataGrid1.Columns[1].Visible=false;报错,原来动态生成的列不能隐藏。
那如何将动态生成的列隐藏呢?还望高手指点!

解决方案 »

  1.   

    那你就到数据源里删除掉不需要的列,也可以用 DataView 的 RowFilter方法把不需要的列过滤掉
      

  2.   

    DataGrid的列不固定,无法使用模板列、
      

  3.   


    <Columns>
    <asp:TemplateField HeaderText="材料名称">
    <ItemTemplate>
    <asp:Label ID="lab_clmc" runat="server" Text='<%#Eval("clmc") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="材料单位">
    <ItemTemplate>
    <asp:Label ID="lab_clmc" runat="server" Text='<%#Eval("cldw") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="标识" Visible="false">
    <ItemTemplate>
    <asp:Label ID="lab_id" runat="server" Text='<%#Eval("keyid") %>'></asp:Label>
    </ItemTemplate>
    <ItemStyle HorizontalAlign="Right" Width="50px" />
    </asp:TemplateField>
    </Columns>
      

  4.   

    那就用CommandName和CommandArgument传参数好了
      

  5.   


    RowDataBound 事件中
    e.Row.Cells[i].Visible = false;
      

  6.   

    DataGrid 的话 是 ItemDataBound 事件中
      

  7.   


    e.Item.Cells[i].Visible = false;
      

  8.   

    http://blog.csdn.net/liyin_liu/archive/2008/05/08/2415145.aspx
    GridView显示隐藏某一列
      

  9.   

    你可以在GridViewde的RowDataBound事件里这么写
    protected void mGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      e.Row.Cells[1].Visible = false;
    }
      

  10.   

    如果说要作为连接条件的话,你根本没有必要把这一列放上去,你可以在写 sql 查询的时候查出这个条件,这样效率还比你绑定到DataGrid 中要高。
      

  11.   

    用样式 暨可隐藏又可取值
    .hidden{display:none;}
    然后样式csslass=hidden
      

  12.   

    如果是自动生成列的话,可以重新绑定下数据源,就是把select语句换下,数据源里列不一样了,grid里当然也呈现不一样的结果了!