get value of a cell in a gridview

Discussion in 'ASP .Net' started by graphicsxp, May 22, 2006.

  1. graphicsxp

    graphicsxp Guest

    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 ?
    graphicsxp, May 22, 2006
    #1
    1. Advertising

  2. graphicsxp

    Mark Rae Guest

    "graphicsxp" <> wrote in message
    news:...

    > 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;
    Mark Rae, May 22, 2006
    #2
    1. Advertising

  3. graphicsxp

    graphicsxp Guest

    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 !
    graphicsxp, May 22, 2006
    #3
  4. graphicsxp

    PeterKellner Guest

    On 22 May 2006 13:52:00 -0700, "graphicsxp"
    <> 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
    PeterKellner, May 22, 2006
    #4
  5. graphicsxp

    graphicsxp Guest

    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
    graphicsxp, May 22, 2006
    #5
  6. graphicsxp

    Mark Rae Guest

    "graphicsxp" <> wrote in message
    news:...

    > 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!
    Mark Rae, May 22, 2006
    #6
  7. graphicsxp

    K B Guest

    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 ***
    K B, May 22, 2006
    #7
  8. graphicsxp

    Stiphy Guest

    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" <> wrote in message
    > news:...
    >
    > > 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!
    Stiphy, May 24, 2006
    #8
  9. graphicsxp

    Mark Rae Guest

    "Stiphy" <> wrote in message
    news:...

    > 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... :)
    Mark Rae, May 25, 2006
    #9
  10. graphicsxp

    infinitevs

    Joined:
    Oct 27, 2006
    Messages:
    1
    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!
    infinitevs, Oct 27, 2006
    #10
  11. graphicsxp

    n1patrick

    Joined:
    Jun 20, 2007
    Messages:
    1
    Sorry but I agree with Stiphy this is a counter intuitive way of doing things.

    I have data that legitimately belongs in a hidden column. I want to access it in RowDataBound or RowCreated event. If I do this:
    MyGridView.Columns[0].Visible = true;

    The girdview goes off to the database and happily get the recordset to rebind.

    Then I do this "MyGridView.Columns[0].Visible = false;" and guess what, off to the database again.

    So 2 db trips and what's more because it is in an event associated with row creation I get an endless loop. Lovely work MS!
    n1patrick, Jun 20, 2007
    #11
  12. graphicsxp

    Tanveer1909

    Joined:
    Aug 27, 2007
    Messages:
    3
    Tanveer1909, Aug 27, 2007
    #12
  13. graphicsxp

    brianguy16

    Joined:
    Feb 20, 2008
    Messages:
    2
    Mark is right!

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

    THIS WORKS LIKE A CHARM
    brianguy16, Feb 20, 2008
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G. Whiz

    Gridview: Get the value in the cell

    G. Whiz, Mar 1, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    1,940
    Ken Cox - Microsoft MVP
    Mar 1, 2006
  2. Chris

    Get GridView Cell Value

    Chris, Apr 5, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    76,331
    imranwahabit
    Jun 14, 2009
  3. Mike

    get cell value within gridview

    Mike, Oct 18, 2007, in forum: ASP .Net
    Replies:
    14
    Views:
    2,725
  4. RJ
    Replies:
    1
    Views:
    241
    Eliyahu Goldin
    Feb 17, 2005
  5. Dave Hewes

    How to get Gridview cell value on select?

    Dave Hewes, Feb 28, 2007, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    227
    Dave Hewes
    Feb 28, 2007
Loading...

Share This Page