• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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

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!
3 Solutions
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.


Imran Javed ZiaConsultant Software Engineer - .NET ArchitectCommented:
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

Kamal KhaleefaInformation Security SpecialistCommented:
make the column visible=false
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;;
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now