Click here to Skip to main content
12,817,318 members (36,339 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 6 Jan 2011

Cannot reference non-visible cell value in gridview but it works in datagrid

, 6 Jan 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Difference of non-visible cell between DataGrid and Gridview

I found one interesting thing that I just want to share with you all.

There is one difference between DataGrid and Gridview. During my .NET Fx 1.1 time, I set the column visibility to false and still access that column through my code behind using code like e.cells[0].Text like that.

Now if you do the same thing with Gridview, it would return blank instead of value. Strange!!!

The reason I found after binging (because I like Microsoft :) ).

Basically, Microsoft has decided that hidden columns in DataGrid were almost always used to store primary keys of records, and that maintaining them in ViewState represented a security risk. Therefore, in the GridView object introduced in Fx 2.0, hidden columns have been removed from ViewState by default, which is why the columns themselves are still there(otherwise you couldn’t reference them in the CodeFile), but their values are blank.

The work around for Gridview is to use following way…

grdObject.DataSource = <datasource>;
grdObject.Columns[0].Visible = true;
grdObject.Columns[0].Visible = false
or you can take HTML hiddenfield and make it runat=server and set the value using EVAL. Then you can reference this control from code-behind.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

De, Subhendu
Technical Lead
India India
A community lover and consultant working with Microsoft suite of products. Since 2005, he has been working in application development and maintenance using different Microsoft products. During his leisure time, he prefers to serve different Microsoft communities and learning new technologies. You can reach him at his blog DotNet Artisan

You may also be interested in...


Comments and Discussions

GeneralInteresting Pin
saanj6-Jan-11 18:21
membersaanj6-Jan-11 18:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170308.1 | Last Updated 6 Jan 2011
Article Copyright 2011 by De, Subhendu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid