CodeSnip: Hiding Columns using GridView
Published: 15 Aug 2006
Unedited - Community Contributed
A simple technique for hiding certain columns in an ASP.NET GridView control. The secret is knowing when to set the Visible property to false.
by Sachin Dedhia
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 51940/ 76


When displaying data in a grid, there are instances when you would want the data column not to be visible to the client and still require it for further processing. This can be achieved by simply setting the column’s visibility property to False.  This can be done in versions prior to ASP.NET 2.0, but how do you achieve this in ASP.NET 2.0?  Read on to find out…

The Problem

The GridView of ASP.NET 2.0 has replaced the DataGrid and the scenario mentioned above just does not work in ASP.NET 2.0.

When the column’s visibility property is set to False, the GridView does not bind the data to the column.  When you try to retrieve data from the hidden column, you get an empty string.

Result?  You start debugging your application and end up being lost in the web.

I hope the solution mentioned below saves some of your precious development time.

The Solution

Add the GridView’s RowCreated event and set the cell’s visibility property to False.

Listing 1

Public Sub myGridView_OnRowCreated(ByVal sender As ObjectByVal e As _
       Web.UI.WebControls.GridViewRowEventArgs) Handles myGridView.RowCreated
  'Hide the cell of the column you do not want to display as each row gets
  'created, eventually leading to the entire column being hidden.
  e.Row.Cells(0).Visible = False
End Sub

The GridView’s RowCreated event gets called after the data is bound to the grid.  So the first column is hidden only after it is databound.  It is possible to hide the other columns by changing the cell index.

In a Nutshell

In this article you have learned how to hide columns using GridView control using ASP.NET 2.0. Happy coding!

User Comments

Title: 2012 NFL jerseys   
Name: NIKE NFL jerseys
Date: 2012-05-20 11:31:25 PM
[/pre]Cheap NFL,NBA,MLB,NHL
[url=]Jerseys From China[/url]
[url=]2012 nike nfl Jerseys[/url]
[url=]cheap China Jerseys[/url]
[url=]Sports Jerseys China[/url]
[url=]NFL Jerseys China[/url]
[url=]NBA Jerseys China[/url]
NHL Jerseys China
[url=]MLB Jerseys China[/url]NFL jerseys For Sale online.All Our Jerseys Are Sewn On and Directly From Chinese Jerseys Factory
[pre]We Are Professional China jerseys Wholesaler
[url=]Wholesale cheap jerseys[/url]Cheap mlb jerseys
[url=]2012 mlb all atar jerseys[/url]
[url= [/url]Cheap China Wholesael[/url]
[url=]Wholesale jerseys From China[/url]
[url=]2012 nike nfl Jerseys[/url]Free Shipping,Cheap Price,7 Days Deliver
We are professional jerseys manufacturer from china,wholesal
sports [url=]Jerseys From China[/url]
[url=]NFL jerseys China[/url]
[url=]NHL Jerseys China[/url]
[url=]NBA Jerseys China[/url]
[url=]MLB Jerseys China[/url]
[url=]China Jerseys[/url],Free Shipping
We are professional jerseys manufacturer from china,wholesal
sports [url=]cheap jerseys sale online [/url]
[url=]2012 nike nfl Jerseys[/url]
[url=]cheap NFL jerseys China[/url]
[url=]NHL Jerseys C
Title: dss   
Name: dfsdfs
Date: 2012-04-30 6:06:37 AM
Title: Miss.   
Name: Akansha
Date: 2011-10-05 4:23:22 AM
Thank you soooo much...U code made it so easy..!!!
Title: Mr.   
Name: Sanjay Khandagale
Date: 2011-09-13 10:26:20 AM
You can handle the GridView_RowBound event and make the cell of which the column need to hide invisible like followin

protected void gdvAgency_RowDataBound(object sender, GridViewRowEventArgs e)
e.Row.Cells[1].Visible = false;

Here my first column was select button and second one agency Id I am hiding autoid column here.
Title: thanks a lot   
Name: chaitanya
Date: 2010-08-02 4:06:36 AM
your articel is very helpfull :)
Title: ss   
Name: snehal
Date: 2010-02-24 1:23:54 AM
I was tring to 'hide' the visibility of the gridview column after databound ... that was not working.
in page load
Title: Thanks   
Name: S
Date: 2010-02-22 4:17:05 PM
Thanks alot.

I was using the ObjectDataSource with my gridview.

I was tring to 'hide' the visibility of the gridview column after databound ... that was not working.

I was just about to give up on using the ObjectDataSource and do the databinding in code.

Your solution works great.

Thanks alot.
Title: Another solution - correction   
Name: Gunnar
Date: 2010-01-27 12:19:53 PM
//Sorry some of the code was missing
protected void ExpandCol(object sender, EventArgs e)
GridView1.Columns[0].Visible = true;

for (int i = 0; i < GridView1.Rows[i].Cells.Count; i++)
for (int j = 0; j < GridView1.Rows[i].Cells.Count; j++)
GridView1.Rows[i].Cells[j].Visible = true;
Title: Another Solution   
Name: Gunnar
Date: 2010-01-27 12:03:31 PM
Thanks for your solution.
Another approach ist to databind first and then hide columns in the code behind page
GridView1.Columns[0].Visible = false;
//function (codebehind) which shows the data
//called from any
protected void ExpandCol(object sender, EventArgs e)
GridView1.Columns[0].Visible = true;
Title: Ms   
Name: Dee
Date: 2009-08-28 6:37:01 AM
I get index out of Arg out range exception?? This does not work
Title: Great Thanks!!   
Name: Nripin Babu
Date: 2009-07-29 6:39:22 AM
Great Thanks!!
Title: A better way   
Name: Waqar Ahmad
Date: 2009-05-22 2:06:44 AM
on page load event

myGridView.Column[0].Visisble = false;

Row Created event will be called upon each row.. however there are some cases where we may need to use this event
Title: Thank u so much   
Name: Viji
Date: 2009-05-16 5:30:02 AM
wow... Thank you so much... I was breaking my head to do this... This article helped me a lot... Thank u..
Title: Nice solution   
Name: Angel
Date: 2009-04-15 1:56:24 AM
tnks men, you save my money and time.
Title: Saved Time   
Name: Ed McPeak
Date: 2009-02-23 7:29:00 PM
Excellent! You rock! Saved me tons of hours!!!
Title: How I can make it work on C#   
Name: Sachin Dedhia
Date: 2008-12-04 1:07:14 AM
Select the GridView control on you page. Bring up the properties window. Click on the "Events" botton (looks like flash/thunder symbol). You will get a list of events from where you double-click "RowCreated" event.

Further, since you are new to ASP.NET, I suggest you take some help from your colleagues.
Title: How I can make it work on C#   
Name: Nelson
Date: 2008-12-02 11:03:06 PM
I am new on ASP, and couldn't make it work on C sharp, were on code should I put it?
Title: Works Great   
Name: Yvette
Date: 2008-11-22 3:27:28 PM
I kept getting an Argument Out of Range exception so I put the code in a Try block and it solved all my problems. Thanks!

Product Spotlight
Product Spotlight 

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2021  |  Page Processed at 2021-12-02 7:38:00 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search