Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > get value of a cell in a gridview

Reply
Thread Tools

get value of a cell in a gridview

 
 
graphicsxp
Guest
Posts: n/a
 
      05-22-2006
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 ?

 
Reply With Quote
 
 
 
 
Mark Rae
Guest
Posts: n/a
 
      05-22-2006
"graphicsxp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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;


 
Reply With Quote
 
 
 
 
graphicsxp
Guest
Posts: n/a
 
      05-22-2006
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 !

 
Reply With Quote
 
PeterKellner
Guest
Posts: n/a
 
      05-22-2006
On 22 May 2006 13:52:00 -0700, "graphicsxp"
<(E-Mail Removed)> 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
 
Reply With Quote
 
graphicsxp
Guest
Posts: n/a
 
      05-22-2006
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

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      05-22-2006
"graphicsxp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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!


 
Reply With Quote
 
K B
Guest
Posts: n/a
 
      05-22-2006
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 ***
 
Reply With Quote
 
Stiphy
Guest
Posts: n/a
 
      05-24-2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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!


 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      05-25-2006
"Stiphy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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...


 
Reply With Quote
 
infinitevs infinitevs is offline
Junior Member
Join Date: Oct 2006
Posts: 1
 
      10-27-2006
Let be the one to say "Thanks Mark!"..

This seems logical to me too in a microsofty kind of way

Anyways I can see where everyone is coming from but there is no point whinging as long as there is a work around for it who cares..

thanks again!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Get GridView Cell Value Chris ASP .Net 4 06-14-2009 07:59 AM
get cell value within gridview Mike ASP .Net 14 10-18-2007 06:18 PM
How to get Gridview cell value on select? Dave Hewes ASP .Net Web Controls 1 02-28-2007 06:19 PM
Gridview: Get the value in the cell G. Whiz ASP .Net 1 03-01-2006 10:21 PM
moving to cell Y if cell X has value Z. Clarke Rice Computer Support 0 06-30-2003 09:35 AM



Advertisments