hiding a column

Discussion in 'ASP .Net Datagrid Control' started by Guest, Jun 17, 2005.

  1. Guest

    Guest Guest

    Hi
    Where is the proper place to hide a column dynamically? I tried putting
    this line in ItemDataBound,

    myDataGrid.Columns[2].Visible = false;

    but is giving me index out of range error! Am I putting this in a wrong
    place (ItemDataBound)?

    Any idea?
    TIA

    --
    Guest, Jun 17, 2005
    #1
    1. Advertising

  2. Guest

    Guest Guest

    Well, I fixed the problem. We can not have AutoGenerateColumns="true", if
    you want to set Column Visible property in ItemDataBound / Page_Load !!?

    Can we have the AutoGenerateColumns="true", and still be able to set Column
    Visible property elsewhere?
    Anyone knows?

    <dl> wrote in message news:%...
    > Hi
    > Where is the proper place to hide a column dynamically? I tried putting
    > this line in ItemDataBound,
    >
    > myDataGrid.Columns[2].Visible = false;
    >
    > but is giving me index out of range error! Am I putting this in a wrong
    > place (ItemDataBound)?
    >
    > Any idea?
    > TIA
    >
    > --
    >
    >
    >
    Guest, Jun 17, 2005
    #2
    1. Advertising

  3. Guest

    Kim Quigley Guest

    I tried setting AutoGenerateColumns to true and then changing column
    visibility in the code behind but the column count is always 0. I can think
    of 3 ways to get around this:

    1-Don't use AutoGenerateColumns
    2-Modify the select statement so that only certain columns are returned, if
    binding to a database.
    3-Create all of the columns programmatically.

    Is there a reason that you have to use AutoGenerateColumns?

    <dl> wrote in message news:...
    > Well, I fixed the problem. We can not have AutoGenerateColumns="true", if
    > you want to set Column Visible property in ItemDataBound / Page_Load !!?
    >
    > Can we have the AutoGenerateColumns="true", and still be able to set
    > Column
    > Visible property elsewhere?
    > Anyone knows?
    >
    > <dl> wrote in message news:%...
    >> Hi
    >> Where is the proper place to hide a column dynamically? I tried putting
    >> this line in ItemDataBound,
    >>
    >> myDataGrid.Columns[2].Visible = false;
    >>
    >> but is giving me index out of range error! Am I putting this in a wrong
    >> place (ItemDataBound)?
    >>
    >> Any idea?
    >> TIA
    >>
    >> --
    >>
    >>
    >>

    >
    >
    Kim Quigley, Jun 17, 2005
    #3
  4. Guest

    Saladin Guest

    try this (from datagridgirl.com)

    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

    Graeme

    <dl> wrote in message news:%...
    > Hi
    > Where is the proper place to hide a column dynamically? I tried putting
    > this line in ItemDataBound,
    >
    > myDataGrid.Columns[2].Visible = false;
    >
    > but is giving me index out of range error! Am I putting this in a wrong
    > place (ItemDataBound)?
    >
    > Any idea?
    > TIA
    >
    > --
    >
    >
    >
    Saladin, Jun 18, 2005
    #4
  5. Yes, you can have AutoGenerateColumns="true" and set columns properties in
    ItemCreated event.

    Eliyahu

    <dl> wrote in message news:...
    > Well, I fixed the problem. We can not have AutoGenerateColumns="true", if
    > you want to set Column Visible property in ItemDataBound / Page_Load !!?
    >
    > Can we have the AutoGenerateColumns="true", and still be able to set

    Column
    > Visible property elsewhere?
    > Anyone knows?
    >
    > <dl> wrote in message news:%...
    > > Hi
    > > Where is the proper place to hide a column dynamically? I tried putting
    > > this line in ItemDataBound,
    > >
    > > myDataGrid.Columns[2].Visible = false;
    > >
    > > but is giving me index out of range error! Am I putting this in a wrong
    > > place (ItemDataBound)?
    > >
    > > Any idea?
    > > TIA
    > >
    > > --
    > >
    > >
    > >

    >
    >
    Eliyahu Goldin, Jun 19, 2005
    #5
    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. Michelle Stone
    Replies:
    0
    Views:
    410
    Michelle Stone
    Oct 15, 2003
  2. beccak
    Replies:
    4
    Views:
    4,942
    beccak
    Jun 9, 2004
  3. David Freeman

    DataGrid - Hiding a Column - Not Working

    David Freeman, Oct 22, 2004, in forum: ASP .Net
    Replies:
    7
    Views:
    498
  4. BobRoyAce
    Replies:
    2
    Views:
    4,504
    BobRoyAce
    Jan 20, 2005
  5. Ste
    Replies:
    41
    Views:
    794
    Thomas 'PointedEars' Lahn
    Aug 1, 2007
Loading...

Share This Page