Store Hidden GridView Columns in a DataKey

DataKeyName

When binding a DataTable to a GridView, I always map every column in the DataTable to a new column in GridView (whether it be manually or automatic). I would then hide the columns I did not want to display. In this way, I could still access values in the hidden columns on GridView events such as RowDataBound. A common example of this situation is creating a hidden column for the row’s unique database id.

Since then, I have learned usage practices for a DataKey, which is native to the GridView control. In a high level overview of a DataKey, we simply specify the names of a DataTable’s columns (in which we originally wanted to hide in a GridView) in a comma separated list. The values can then be accessed via the GridView.

For a concrete example, let us assume we have the following information retrieved from a database. The column name id is our primary key from the database:

id firstName lastName age gender
1 Victor Chen 23 M
2 John Doe 26 M
3 Jane Smith 19 F
4 Mary Ann 35 F

We only want to display on the GridView a firstName, lastName, and gender. However, during GridView events, we still want access to the id and age. To setup the GridView, it only takes 3 easy steps!

  1. Place a GridView control on your aspx page.
  2. Manually setup visible columns for firstName, lastName, and gender. (A tutorial on this step)
  3. In the GridView properties and find DataKeyNames (There is an image on what this looks like at the top of this tutorial). Insert a comma separated list for that property: id,age

Now, when you compile and run the page, you will see your GridView is populated with 3 columns. Now to access the hidden values id and age only takes an if statement and one line per hidden column! We will access these values on the RowDataBound event:

protected void gvTemplate_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int rowIndex = e.Row.DataItemIndex;
        int id = Convert.ToInt32(gvTemplate.DataKeys[rowIndex].Values["id"]);
        int age = Convert.ToInt32(gvTemplate.DataKeys[rowIndex].Values["age"]);
    }
}

In our example above, gvTemplate is the name of our GridView. We access the DataKeys property and specifically access the rowIndex of interest. Because we specified two items id and age in the GridView, we can access them by name. You can also access it by index, but using the actual index’s name makes the code more readable. The final step in the line is to convert the object into the variable type of your choice. If you want, you can shorthand the code with:

gvTemplate.DataKeys[rowIndex]["age"]

You will still need to convert the object into the variable type of your choice. In this case, the best choice is to convert an age it into a integer. And there we have it! We have successfully created a GridView without hidden columns, but still accessible hidden values!

Share and Enjoy:
  • Digg
  • DotNetKicks
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Live
  • MySpace
  • Netvibes
  • Reddit
  • StumbleUpon
You can leave a response, or trackback from your own site.

9 Responses to “Store Hidden GridView Columns in a DataKey”

  1. Tommy says:

    Your helped me and sved a lot of time.
    Thanks.

  2. Juergen_G says:

    Great aid – i was searching for a solution like that.
    Thank you

    Nice greetings from Austria

  3. Jeff Allen says:

    Wow! I never knew you could assign more than one value to DataKeyNames! This article was a godsend.

  4. sanagarwl says:

    thanks for the help…, had no clue multiple values could be stored in DataKeyNames

  5. sonx says:

    JUST WHAT I NEED!
    THANKS A BUNCH!

  6. Etiene says:

    Thank a lot, me sirvio mucho tu ejemplo. muchas gracias.

  7. Erick says:

    Thanx Victor… u really help me.
    this is a real solution.

  8. Hariprasad says:

    Hi,

    I am workign on VS 2008 Enterprise Edition.
    I tried this amd could find only the first three proprties in the property window but was not able to locate Data property. Please can u help me in locating it. I am not sure if my question is funny but I can tell u that I am a new start to Dot Net

    • Victor Chen says:

      I would confirm you’re looking at the correct web controls property by making sure the dropdown on the property window says System.Web.UI.WebControls.GridView (similar to that of the screenshot from the post). Note that this screenshot was taken from Visual Studio 2008 Professional.

Leave a Reply