Database Search Solution
(New Version) Search Control
Free ASP.NET Controls
ASP.NET Media Player Control
Flash Video Player Control
ASP.NET Telecommute Jobs
Free IP Location Lookup
Test .Net Regular Expressions
CSS/Table/DIV Page Layouts
Article Sites Master List
.NET Windows Forms
General .NET Framework
Accepting Credit Cards
GridView Hidden Column Problem (And Two Common Solutions)
GridView is one of the new controls in ASP.Net 2.0 that replaced the DataGrid. If you've been working in ASP.Net 2.0 since some time, you would have come to face a problem. When you design the GridView and create a bound field... In earlier versions of .Net when you didn't want the data to be visible to the client, but wanted the column for processing of data, you simply set the Column visibility to False.
However, this does not work in ASP.Net 2.0. When a column's visibility is set to False, then the Grid does not bind data to the column, and thus when you try to retrieve data from the hidden column, it either blows up or returns an empty string.
This presents a really big problems to developers, and in this small article I present a solution to this dilemma. I hope this can be helpful for your development needs.
This tutorial assumes that you own a copy of Visual Studio 2005 or Visual Web Developer Express. It also assumes that you are familiar with ASP.Net 2.0 basics.
Create the Row Created event handler and do the following:
Public Sub myGrid_OnRowCreated(ByVal sender As Object, ByVal e As Web.UI.WebControls.GridViewRowEventArgs) Handles myGrid.RowCreated
'Those columns you don't want to display you config here,
'you could use a for statement if you have many :)
e.Row.Cells(1).Visible = False
Why this works? Because the event is called after the data is bound to the grid... This ensures that the column has been databound and then it is hidden.
Another solution can be to use the following to bind the data to the GridView.
Public Sub myTestFunction()
'To hide a column, set its width to zero or use MappingType
Dim strCON As String = "<Connection String>"
Dim strQuery As String = "<QueryString>"
Dim da As Data.SqlClient.SqlDataAdapter
Dim ds As Data.DataSet
Dim conn As New Data.SqlClient.SqlConnection(strCON)
da = New Data.SqlClient.SqlDataAdapter(strQuery, CON)
ds = New Data.DataSet
Catch ex As Exception
'Do error handling here...
'Here you can HIDE the Column
ds.Tables("tblData").Columns(colIndex).ColumnMapping = Data.MappingType.Hidden
myGrid.DataSource = ds.Tables("tblData")
And all things must come to an end
Although GridView is very powerful control which can be improved by adding new features, every of this improvements takes your valuable coding and debugging time. In simple tasks, like hidden column problem in this tutorial, you can solve it fast. However, in real developer life it is often better to avoid losing too much time and get some more professional solution, like APNSoft Datagrid. This control has built in fancy features like Ajax support, scrolling, optimized working with large tables, context menus, skins etc., and can be life saver if you have short deadlines.
The main emphasis in this article was on ASP.Net 2.0 GridView hidden columns, providing a solution to the problem faced by many developers. I hope you found this article interesting and informative. I am open for suggestions and remarks, both negative and positive. Feel free to contact me at firstname.lastname@example.org.
Tutorial toolbar: Tell A Friend | Add to favorites | Feedback |