AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=950&pId=-1
CodeSnip: Hiding Columns using GridView
page
by Sachin Dedhia
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 51944/ 62

Introduction

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!


Product Spotlight
Product Spotlight 

©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-29 10:33:42 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search