By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,814 Members | 1,671 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,814 IT Pros & Developers. It's quick & easy.

get value of a cell in a gridview

P: n/a
Hi,
How can I get the value of a cell that belongs to a hidden column of
gridview ?
Here's how I normally access the value of a cell, but it doesn't work
if the column is invisible:

Code :
Me.grdPublication.SelectedRow.Cells(1).Text

I've used a gridview with auto-generated columns to false, so I can
display those I want only. Yet the column that are not visible, do
exist, but the text value is always "".

Can you help ?

May 22 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"graphicsxp" <sa*************@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
Can you help ?


I certainly can! What you've stumbled on is a new feature of the GridView
object, which differs from the DataGrid object in v1.x

Basically, Microsoft have decided that hidden columns in DataGrids 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 v2, 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.

Fortunately, there is an easy workaround. Simply set the column(s) to
Visible immediately before invoking the GridView's DataBind() method, and
then hide them again immediately after. E.g.

MyGridView.DataSource = <some sort of DataSet, SqlDataReader, whatever>
MyGridView.Columns[0].Visible = true;
MyGridView.DataBind();
MyGridView.Columns[0].Visible = false;
May 22 '06 #2

P: n/a
hu ??? I can't try right now, will give it a go tomorrow at work. But
it seems totally weird to me. I don't see how that can do the trick!
Hopefully you are right !

May 22 '06 #3

P: n/a
On 22 May 2006 13:52:00 -0700, "graphicsxp"
<sa*************@googlemail.com> wrote:
Hi,
How can I get the value of a cell that belongs to a hidden column of
gridview ?
Here's how I normally access the value of a cell, but it doesn't work
if the column is invisible:

Code :
Me.grdPublication.SelectedRow.Cells(1).Text

I've used a gridview with auto-generated columns to false, so I can
display those I want only. Yet the column that are not visible, do
exist, but the text value is always "".

Can you help ?


another easy way to make them accessible is to add the column to the
gridview attribute datakeynames. (ie:
datakeynames=[address,city,salary,id])
Peter Kellner
http://peterkellner.net
May 22 '06 #4

P: n/a
yeah probably I should do that. I did it for the primary key, can't
remember why i didn't do it for the other 'hidden' fields

May 22 '06 #5

P: n/a
"graphicsxp" <sa*************@googlemail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com...
But it seems totally weird to me.
Really? Seems perfectly logical to me...
I don't see how that can do the trick!
OK.
Hopefully you are right !


Let's hope so!
May 22 '06 #6

P: n/a
K B
IMHO this is the easiest way. This way you don't have to play with your
column settings and you can still hide the column. You MUST have
DataKeyNames set in your gridview HTML.

Dim key As DataKey = gvQuestions.SelectedDataKey
Session("QuestionID") = key.Value

HTH,
Kit

*** Sent via Developersdex http://www.developersdex.com ***
May 22 '06 #7

P: n/a
What is the point of having a visible property to the column if it
doesn't do anything? All this serves to do is confuse developers who
have for years stored data in hidden fields with the expecation that
they can access it programatically.

Furthermore, their PK argument is hogwash as with AJAX style call backs
I can think of very good reasons that people would want to hide a
column only to have a callback retrieve the text of the column on a
callback and display it to the user.

At the very least they should remove the visible property entirely as
it is useless if it doesn't dislay data to the user (expected behavior)
AND doesn't store the data in viewstate.

I'm sorry to say that this is just another example of MSFT missing the
mark which is frustrating as hell for those of us who have relied on
them for so many years for our businesses.

Sean

Mark Rae wrote:
"graphicsxp" <sa*************@googlemail.com> wrote in message
news:11*********************@j73g2000cwa.googlegro ups.com...
But it seems totally weird to me.


Really? Seems perfectly logical to me...
I don't see how that can do the trick!


OK.
Hopefully you are right !


Let's hope so!


May 24 '06 #8

P: n/a
"Stiphy" <st******@yahoo.com> wrote in message
news:11**********************@j73g2000cwa.googlegr oups.com...
What is the point of having a visible property to the column if it
doesn't do anything? All this serves to do is confuse developers who
have for years stored data in hidden fields with the expecation that
they can access it programatically.
Things evolve. This behaviour has been identified as a potential security
risk, so has been modified.
At the very least they should remove the visible property entirely as
it is useless if it doesn't dislay data to the user (expected behavior)
AND doesn't store the data in viewstate.
Rubbish! All that has changed is that for GridView columns to store hidden
column data in ViewState, the columns need to be visible at the moment the
data is bound to the DataGrid. They can then be set straight back to hidden
if required. I'm sure you can manage it.
I'm sorry to say that this is just another example of MSFT missing the
mark which is frustrating as hell for those of us who have relied on
them for so many years for our businesses.


Then this tiny change in behaviour really oughtn't to present you with much
of a coding challenge... :-)
May 25 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.