ASP.NET gridview field select

Posted on 2011-10-20
Last Modified: 2012-05-12
I have a GridView in ASP.NET that displays 10 fields.  I have the DataKeyNames field set to the correct field.  I have a separate data source that pulls data based on this field.  However, I would now like to add a second field to the query that uses one of the other fields on the GridView as a parameter.  How would I pull the value out of the gridview?
Question by:zintech
    1 Comment
    LVL 38

    Accepted Solution

    You can set more than one value in DataKeyNames. Just separate with a comma. Then in the GridView RowDataBound event, you can access each DataKey by name (or index) for each row.

    string key_1 = GridView1.DataKeys[e.Row.RowIndex]["keyValue_1"].ToString()    //key value 1 by name  
    string key_2 = GridView1.DataKeys[e.Row.RowIndex]["keyValue_2"].ToString()    //key value 2 by name

    Or by index:

    string key_1 = GridView1.DataKeys[e.Row.RowIndex][0].ToString()    //key value 1 by index  
    string key_2 = GridView1.DataKeys[e.Row.RowIndex][1].ToString()    //key value 2 by index

    Or it can be written like this:

    string key_1 = GridView1.DataKeys[e.Row.RowIndex].Values[0].ToString()    //key value 1 by index  
    string key_2 = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString()    //key value 2 by index  

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    This video discusses moving either the default database or any database to a new volume.

    728 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

    20 Experts available now in Live!

    Get 1:1 Help Now