Directory Freebies VS CheatSheet Forum

RSS

Email

Translate

Home About Archive Privacy Contact Advertise Write for Dev102
Posted by Amit on Mar 5th, 2008 | Filed under .Net, C# |

Hi all.

it’s been a long time since I wrote and I promise I will pick up the pace. But now lets get down to business how to hide columns in a grid view:

The first thing that comes to mind is going to the definition of the columns in the GridView, go to the column “Go” and put the Visible property to false. When you run the code and access the value we see that there is nothing in that cell. This is because since. NET “releases”  the binding  because it sees that the Visible property is false. We want the DataBinding we just don’t want to show the column. To solve this we will have to register to the  RowCreated event  as follows:

protected void Grid_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.Cells.Count > 0)
            {
                e.Row.Cells[0].Visible = false;
            }
        }

This code will make sure that when a row is added the column with index 0 will be hidden.

Another and a better way in my opinion to solve this issue is using the DataBound event:

protected void Grid_DataBound(object sender, EventArgs e)
        {
            if (Grid.Columns[0].Visible)
            {
                Grid.Columns[0].Visible = false;
            }
        }

Amit

Tags: , ,

9 Responses to “How to Hide Columns of a GridView”


  1. Anand Singh Said on Oct 21, 2008 :

    Very good……..

  2. asp.net Said on Oct 24, 2008 :

    great article :)

  3. Balaji.G Said on Apr 7, 2009 :

    this was so so gud n cool for the beginners

  4. Ishan Said on Apr 27, 2010 :

    This articledid not solved my purpose.You don have the columns property available with it.Please suggest some otherway.

  5. Nandeesh Said on Oct 5, 2010 :

    thank u i got result

  6. prasy Said on Oct 23, 2010 :

    nice……………..

  7. aneesh Said on Dec 28, 2010 :

    Only 1st Method works Amit. Thanks though - also would be helpful to mention that one needs to add onrowcreated=”Grid_RowCreated” on the bgridview asp tag in the .aspx file.

  8. Jeff Said on Jan 22, 2011 :

    ‘RowCreated’: only works for first cell, all others return ArgumentOutOfRange error

    ‘DataBound’: doesn’t hide column, rather deletes it.

    How to actually “hide” the column? Thanks!

  9. Jithu Said on Nov 11, 2011 :

    Thank u….

Post a Comment

Write Article for Dev102

Write for Dev102!

We pay for user submitted tutorials and articles that we publish. Anyone can send in a contribution

Learn More