[Last Call] Learn how to a build a cloud-first strategyRegister Now


How to hide Record ID in gridview and still access the value?

Posted on 2011-04-22
Medium Priority
Last Modified: 2012-05-11
I have a gridview that peforms actions based on the record id of the row, called RecID.  The functions work great when I have the RecID visible in the grid, but when I set visible=false, it fails on this line:

int RecID = Convert.ToInt32(gvTracks.Rows[ii].Cells[0].Text);

How do I reference the value when it's set to visible=false?  Thanks!
Question by:JT_SIRO
LVL 31

Accepted Solution

MlandaT earned 168 total points
ID: 35450064
The GridView control has the functionality to allow multiple DataKey values to be assigned to it using the DataKeyNames property. However, the method of retrieving a DataKey that is not the primary key is, for the most part, undocumented. The Values property will allow you to get any DataKey value.


Open in new window


Open in new window

Where GridView is the ID of the GridView control, Index1 is the index of the current row in the GridView, and Index2 is the name of the DataKey.


LVL 16

Assisted Solution

by:Imran Javed Zia
Imran Javed Zia earned 164 total points
ID: 35451383
You can use any of following

If you hide the column with css rule display:none, as opposed to setting Visible property to false, you can access it as any other column.

or Use DataKeys with DataKeyNames

LVL 16

Expert Comment

by:Kamal Khaleefa
ID: 35455257
make the column visible=false

Assisted Solution

aswathi earned 168 total points
ID: 35465985
Make the column visible just before databind and  make it invisible just after it.
Also set Visible = true in the property of that column in grid

gvTracks.Columns[0].Visible = true;   // index of column RecID
gvTracks.DataSource = datatable;
gvTracks.Columns[0].Visible =false;;

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Integration Management Part 2
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question