Link to home
Start Free TrialLog in
Avatar of g-spot
g-spot

asked on

How to specify the DataKey of a GridView

How do I programatically specify the DataKey of a GridView?

With a DataGrid I could use the following:

datagrid1.DataKeyField = "departmentID"

ASKER CERTIFIED SOLUTION
Avatar of Swapnil
Swapnil
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of g-spot
g-spot

ASKER

Thanks Netswap!
Avatar of g-spot

ASKER

I'm getting the following error:

"Index was outside the bounds of the array."
Hi g-spot,
you can do it in other tricky way as following as DataKeyNames only except array of strings.

Dim strKeys As String() = {"departmentID"}
GridView1.DataKeyNames = strKeys
Avatar of g-spot

ASKER

That's fixed it. Thanks netswap :-)