DataGrid - How to get DataKey from SelectedIndexChanged

bri189a

Senior Contributor
Joined
Sep 11, 2003
Messages
1,004
Location
VA
Basically in my endevour to learn DataGrid's better I've set up a parent/child data grid page. Click on the parent grid item will open up the details for that item in the child grid. The EditItem, DeleteItem, and all but the SelectedIndexChanged events allow you to get to the DataKey property, selected index changed doesn't. So what I'm trying to figure out, is how to determined what the value of my ID column is from the SelectedIndexChanged event. It's probably simple but I'm missing it.

By the way, it appears the DataSetIndex property is a short-cut to get the ID field... is this true?
 
Okay figured out:

Visual Basic:
 Response.Write(String.Format("Key Index {0} selected", DirectCast(dg.DataKeys.Item(DirectCast(sender, DataGrid).SelectedIndex + (dg.PageSize * dg.CurrentPageIndex)), Integer)))

Also found what the DataSetIndex actually does....

If my code above could be imporved somehow let me know.

Oh...forum guys... maybe you can figure out some sort of word wrap functionality for those of use who type our code with word wrap on... just a thought.
 
Back
Top