HIde a column in GridView using auto-generated columns...

Discussion in 'ASP .Net' started by Jason, May 5, 2006.

  1. Jason

    Jason Guest

    How do I hide a column in a GridView in ASP.NET 2.0 when all of the
    columns are autogenerated based on the datasource? I want to hide the
    first of three columns, but the following doesn't work:

    DataTable dt = new DataTable();
    DataColumn dc;
    dc = new DataColumn();
    dc.ColumnName = "blog_entry_id";
    dc.DataType = System.Type.GetType("System.Int32");
    dt.Columns.Add(dc);

    dc = new DataColumn();
    dc.ColumnName = "blog_entry_title";
    dc.DataType = System.Type.GetType("System.String");
    dt.Columns.Add(dc);

    dc = new DataColumn();
    dc.ColumnName = "blog_entry_date";
    dc.DataType = System.Type.GetType("System.DateTime");
    dt.Columns.Add(dc);

    SqlConnection loConnection = new
    SqlConnection(ConfigurationManager.AppSettings["Data_Connection_String"].ToString());
    SqlDataAdapter loAdapter = new SqlDataAdapter("SELECT
    blog_entry_id, blog_entry_title, blog_entry_date FROM tbblog_entry
    WHERE blog_blog_id = " + giBlogID.ToString() + " AND blog_entry_deleted
    = 0 ORDER BY blog_entry_date", loConnection);
    loAdapter.Fill(dt);
    gvBlogList.DataSource = dt;
    gvBlogList.DataBind();
    gvBlogList.Columns[0].Visible = false;

    I need the data in the first column later, so I want it returned - I
    just don't want to show it in the GridView...

    Thanks!
    Jason, May 5, 2006
    #1
    1. Advertising

  2. Jason

    Jan Hyde Guest

    "Jason" <>'s wild thoughts were
    released on 5 May 2006 05:29:34 -0700 bearing the following
    fruit:

    >How do I hide a column in a GridView in ASP.NET 2.0 when all of the
    >columns are autogenerated based on the datasource? I want to hide the
    >first of three columns, but the following doesn't work:
    >

    <SNIP>
    >
    >I need the data in the first column later, so I want it returned - I
    >just don't want to show it in the GridView...


    Wouldn't it be easier just to specify the columns you want
    to display and use the DataKeys collection to store your
    other data?








    Jan Hyde (VB MVP)

    --
    Did you hear about the guy who blamed arithmetic for his divorce?
    His wife put two and two together.

    (Art. Moger)
    Jan Hyde, May 5, 2006
    #2
    1. Advertising

  3. Jason

    Jason Guest

    Jan - thanks for the reply.

    What you suggest may be a much better idea. This is my first go 'round
    with GridView. So, could you provide an example or point me to an
    article that explains how to do this?

    Thanks again!
    Jason
    Jason, May 5, 2006
    #3
  4. Jason

    Jan Hyde Guest

    Jan Hyde <>'s wild thoughts
    were released on Fri, 05 May 2006 14:54:48 +0100 bearing the
    following fruit:

    >"Jason" <>'s wild thoughts were
    >released on 5 May 2006 05:29:34 -0700 bearing the following
    >fruit:
    >
    >>How do I hide a column in a GridView in ASP.NET 2.0 when all of the
    >>columns are autogenerated based on the datasource? I want to hide the
    >>first of three columns, but the following doesn't work:
    >>

    ><SNIP>
    >>
    >>I need the data in the first column later, so I want it returned - I
    >>just don't want to show it in the GridView...

    >
    >Wouldn't it be easier just to specify the columns you want
    >to display and use the DataKeys collection to store your
    >other data?
    >


    Ok, I'm working from memory here so my facts might be a
    little off.

    Anyhoo - select your datagrid and press f4, find the
    property 'DataKeyNames'

    Lets say you want to retrieve CustomerID from the database
    but don't want to display it in the grid.

    Add 'CustomerID' (without quotes) to the DataKeyNames
    collection, repeat this for your other fields.

    You can retrieve the data with code something like this

    GridView1.DataKeys(RowIndex).Item("CustomerID")



    Jan Hyde (VB MVP)

    --
    Man walks into a bookshop, picks up two books and goes to the counter.
    He cuts the bottom off one trouser leg and hands it to the assistant
    saying, "There's a turn-up for the books."
    Jan Hyde, May 8, 2006
    #4
    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. Hanson
    Replies:
    8
    Views:
    13,547
    Peter Högström
    Jul 2, 2004
  2. Keithb
    Replies:
    2
    Views:
    1,116
  3. linkswanted
    Replies:
    1
    Views:
    883
  4. Jim Mitchell

    Can you hide columns generated with AutoGenerateColumns?

    Jim Mitchell, Jul 17, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    168
    Jim Mitchell
    Jul 17, 2003
  5. RBM
    Replies:
    1
    Views:
    140
Loading...

Share This Page