ASP.NET gridview field select

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom BeckCommented:
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  

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

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.