get value of a cell in a gridview

G

graphicsxp

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 ?
 
M

Mark Rae

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;
 
G

graphicsxp

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 !
 
P

PeterKellner

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
 
G

graphicsxp

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
 
K

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
 
S

Stiphy

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
 
M

Mark Rae

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... :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top