Ramani Sandeep's Blog

DotNetting – Fast , Easy Way of Developing Applications

How to hide Gridview column programmatically?

Posted by ramanisandeep on April 7, 2009

Question :  How to hide Gridview column programmatically?

Solution :

The Columns collection only stores the explicitly declared columns, so if you’re using autogenerated columns, the count will be zero.
If you’re using autogenerated column, after databind you could loop through the rows collection and make the appropriate cells invisible, like:

GridView1.DataBind();

if (GridView1.Columns.Count > 0)
GridView1.Columns[0].Visible = false;
else
{
GridView1.HeaderRow.Cells[0].Visible = false;
foreach (GridViewRow gvr in GridView1.Rows)
{
gvr.Cells[0].Visible = false;
}
}

6 Responses to “How to hide Gridview column programmatically?”

  1. kaka said

    Thanks for sharing this.
    I’ve been looking how to hide the column… and it works! ^_^

  2. Greg said

    Thanks a lot!!!!
    Great explanation!
    Works like a charm!

  3. Shivraj said

    coollllllllllllllllllllllllll

    Solution
    Greattttttttttt Job ramanisandeep

  4. Ajit said

    Hi,

    How to hide a column ( Primary key) in Data Grid View if they are later used for editing purpose on click of context menu.

  5. Michael said

    Hi
    I’m filling up gridview with the data and then hide some columns.
    For one column I change header by the value from one cell from the same row. Everything goes fine but when I’m hiding a column where I just taken a text for header I’m losing a header text as well.

    I very appreciate your help.

    • ramanisandeep said

      if u want to retain header text then save it in hidden field.
      if you save it in header & when we hide header we make it visible false..it means it does get rendered so you will not have it value.. so try hidden field..it will work..

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>