Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 277
  • Last Modified:

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?
0
zintech
Asked:
zintech
1 Solution
 
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  
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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