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

Posted on 2011-04-22
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 30

    Accepted Solution

    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
    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
    make the column visible=false
    LVL 2

    Assisted Solution

    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

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now