how to hide auto generate columns in datagrid?

Discussion in 'ASP .Net' started by Hanson, Jun 8, 2004.

  1. Hanson

    Hanson Guest

    I have a datagrid control in an aspx page. The datagrid columns will be
    dynamically generated according to the pre-page's search condition, I want
    to hide some columns in the datagrid. is that anyway to do that?
     
    Hanson, Jun 8, 2004
    #1
    1. Advertising

  2. Hanson

    Cosmin Marin Guest

    Hi,

    You could use DataGridColumn's Visible property.

    Cosmin

    "Hanson" <> wrote in message
    news:...
    > I have a datagrid control in an aspx page. The datagrid columns will be
    > dynamically generated according to the pre-page's search condition, I want
    > to hide some columns in the datagrid. is that anyway to do that?
    >
    >
     
    Cosmin Marin, Jun 8, 2004
    #2
    1. Advertising

  3. Hanson

    Richard Guest

    Set in the datagrid,

    AutoGenerateColumns="false"

    <asp:datagrid runat="server" autogeneratecolumns="false">
    .....boundcolumns stuff....
    </asp:datagrid>

    Autogeneratecolumns, Indicates whether the DataGrid should automatically
    generate BoundColumn columns using the data supplied in a data source (the
    DataGrid will use all fields in the data source to do so).
     
    Richard, Jun 8, 2004
    #3
  4. Hanson

    Richard Guest

    Sorry Hanson, i misunderstood the question a bit,
    anyway,

    use a Visible = False

    to make it invisible and not be showed then

    "Hanson" <> schreef in bericht
    news:...
    > I have a datagrid control in an aspx page. The datagrid columns will be
    > dynamically generated according to the pre-page's search condition, I want
    > to hide some columns in the datagrid. is that anyway to do that?
    >
    >
     
    Richard, Jun 8, 2004
    #4
  5. Hanson

    Hanson Guest

    Thanks first for your response.

    But you might misunderstand my question:

    1), The datagrid's column is generated in runtime not in design time, because the columns is depone the pre-page's search condition. only in runtime, when I construct the dataTable, I can know how many columns in the datagrid.
    2) I can not access the auto-generated columns (when I debug, it should the datagrid.columns.count=0 ), then I can not set the column's visible value;


    Any other help is very appreciated!


    "Cosmin Marin" <> wrote in message news:#...
    Hi,

    You could use DataGridColumn's Visible property.

    Cosmin

    "Hanson" <> wrote in message
    news:...
    > I have a datagrid control in an aspx page. The datagrid columns will be
    > dynamically generated according to the pre-page's search condition, I want
    > to hide some columns in the datagrid. is that anyway to do that?
    >
    >
     
    Hanson, Jun 8, 2004
    #5
  6. Hanson

    Cosmin Marin Guest

    Hi,

    As Richard also suggested (and if my understanding is correct this time): set AutoGenerateColumns to false and dynamically add your columns based on what you have in the datatable.

    Cosmin
    "Hanson" <> wrote in message news:...
    Thanks first for your response.

    But you might misunderstand my question:

    1), The datagrid's column is generated in runtime not in design time, because the columns is depone the pre-page's search condition. only in runtime, when I construct the dataTable, I can know how many columns in the datagrid.
    2) I can not access the auto-generated columns (when I debug, it should the datagrid.columns.count=0 ), then I can not set the column's visible value;


    Any other help is very appreciated!


    "Cosmin Marin" <> wrote in message news:#...
    Hi,

    You could use DataGridColumn's Visible property.

    Cosmin

    "Hanson" <> wrote in message
    news:...
    > I have a datagrid control in an aspx page. The datagrid columns will be
    > dynamically generated according to the pre-page's search condition, I want
    > to hide some columns in the datagrid. is that anyway to do that?
    >
    >
     
    Cosmin Marin, Jun 8, 2004
    #6
  7. Hanson

    Hanson Guest

    Hi Richard,

    Thanks for your reply first. However the problem is I can not access the autogenerated columns, and then I can not set the column's visible property.


    "Richard" <> wrote in message news:40c57f8e$0$13471$...
    Sorry Hanson, i misunderstood the question a bit,
    anyway,

    use a Visible = False

    to make it invisible and not be showed then

    "Hanson" <> schreef in bericht
    news:...
    > I have a datagrid control in an aspx page. The datagrid columns will be
    > dynamically generated according to the pre-page's search condition, I want
    > to hide some columns in the datagrid. is that anyway to do that?
    >
    >
     
    Hanson, Jun 11, 2004
    #7
  8. Hanson

    Ryan S Guest

    You could try something like:

    Dim dgcol As System.Web.UI.WebControls.DataGridColumn
    For Each dgcol In myDG.Columns
    If dgcol.HeaderText = "column to hide" Then dgcol.Visible = False
    Next

    in Page_Load after calling myDG.DataBind(). That will work with explicity
    defined columns. I'm not sure if that works with auto-generated columns.

    Good luck!

    -Ryan

    "Hanson" <> wrote in message
    news:uVBV3$...
    Hi Richard,

    Thanks for your reply first. However the problem is I can not access the
    autogenerated columns, and then I can not set the column's visible property.


    "Richard" <> wrote in message
    news:40c57f8e$0$13471$...
    Sorry Hanson, i misunderstood the question a bit,
    anyway,

    use a Visible = False

    to make it invisible and not be showed then

    "Hanson" <> schreef in bericht
    news:...
    > I have a datagrid control in an aspx page. The datagrid columns will be
    > dynamically generated according to the pre-page's search condition, I want
    > to hide some columns in the datagrid. is that anyway to do that?
    >
    >
     
    Ryan S, Jun 11, 2004
    #8
  9. Hi,
    found this on http://www.datagridgirl.com/faq.aspx

    Question: How do I hide a column in my Datagrid if AutoGenerateColumns
    is set to True?

    Answer: AutoGenerated columns do not appear in the Datagrid's Columns()
    collection, and so the usual method of hiding a Datagrid column will
    fail:

    'Will NOT work for AutoGenerated columns:
    Datagrid1.Columns(1).Visible = False

    So the place to handle this is in the ItemDataBound event of the
    Datagrid:

    <asp:DataGrid id="Datagrid1" runat="server" AutoGenerateColumns="True"
    OnItemDataBound="Datagrid1_OnItemDataBound"/>

    Private Sub DataGrid1_ItemDataBound(s As Object, e As
    DatagridItemEventArgs)
    e.Item.Cells(1).Visible = False
    End Sub





    *** Sent via Devdex http://www.devdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Peter Högström, Jul 2, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J Gao

    How to hide columns in Datagrid

    J Gao, Jul 22, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    5,713
    J Gao
    Jul 22, 2003
  2. Jason
    Replies:
    3
    Views:
    17,066
    Jan Hyde
    May 8, 2006
  3. linkswanted
    Replies:
    1
    Views:
    924
  4. rob thomson
    Replies:
    0
    Views:
    263
    rob thomson
    Sep 4, 2003
  5. ton
    Replies:
    2
    Views:
    207
    Alvin Bruney [MVP]
    Feb 11, 2004
Loading...

Share This Page