locked
Data from GridView invisible columns RRS feed

  • Question

  • Hi all,
    This seems like a question there are probably millions of answers to out there already, but I have not found any of them.

    I have a GridView bound to a query my database. I don't want the user to have to see the db_id field (just a arbitrary number), but I want to use the ID for other things behind the scenes. If I make the ID column invisible, I don't know how to use its contents in my code.
    Anyone know how this is done?

    Thanks a lot,
    Benjamin
    Monday, August 15, 2005 10:47 PM

Answers

  • Hi Benjamin,

      I don't think there are as many answers to this question as you say (I haven't found any either), but I did figure out how to do this.  All you have to do is specify the primary key column in the GridView.DataKeyNames property.  Then, you can access the column value (for the selected record) using GridView.SelectedValue.  The DataKeyNames property can contain various column names, so it's possible to have various invisible columns and access their data, but I haven't tried that yet.  Hope this helps.

     

    Tuesday, November 28, 2006 3:07 PM

All replies

  • Hi Benjamin,

      I don't think there are as many answers to this question as you say (I haven't found any either), but I did figure out how to do this.  All you have to do is specify the primary key column in the GridView.DataKeyNames property.  Then, you can access the column value (for the selected record) using GridView.SelectedValue.  The DataKeyNames property can contain various column names, so it's possible to have various invisible columns and access their data, but I haven't tried that yet.  Hope this helps.

     

    Tuesday, November 28, 2006 3:07 PM
  • Hi Ben,

       First your gridview make the bound col db_id to invisible. Then add that column to keys def property i.e. to GridView.DataKeyNames. You could have multiple keys usually by comma separated. The while RowDataBound() or on your wish you could access the the values by GridView1.DataKeys[e.Row.RowIndex].Values["db_id"].ToString()
    • Proposed as answer by sGirach Monday, July 19, 2010 10:48 AM
    Monday, May 7, 2007 6:25 AM
  • Thank you !

     

    I did a lot of looking before I happened on Harish's post.  My configuration didn't want the "Row.", but after I dropped Row. I had :

     

    GridView1.DataKeys[e.RowIndex].Values["db_id"].ToString()

     

    (

    my implementation was

     

    int intID = (int)GridView1.DataKeys[e.RowIndex].Values[0];

     

    So as I understand, DataKeys[e.RowIndex] specifies the keys from the selected row; the 0 seems to get the value from my one key column, the ID column; and I convert for integer output.

     

    )

     

     

    That was the missing piece in my project, it's working for me now.  Hope this helps the next code seeker.

     

     

    Wednesday, February 27, 2008 4:16 PM
  • I have tried this options of gettings values in case of getting the value of a column in datagrid that is invisible but it showing an error message that  Public member 'Values' on type 'String' not found

     

    Thursday, May 8, 2008 10:44 AM
  • more accurately -->

    GridView gv = (GridView)sender;

    string str0 = gv.DataKeyNames[0];

    int ind = e.NewSelectedIndex;

    string link = gv.DataKeys[ind][str0].ToString();

    Thursday, July 10, 2008 2:14 PM