How to specify the DataKey of a GridView

Posted on 2006-06-27
Last Modified: 2008-01-09
How do I programatically specify the DataKey of a GridView?

With a DataGrid I could use the following:

datagrid1.DataKeyField = "departmentID"

Question by:g-spot
  • 3
  • 2
LVL 16

Accepted Solution

Swapnil Piparia earned 125 total points
ID: 16991347
Hi g-spot,
GridView1.DataKeyNames(0) = "departmentID"


Author Comment

ID: 16991881
Thanks Netswap!

Author Comment

ID: 16993011
I'm getting the following error:

"Index was outside the bounds of the array."
LVL 16

Expert Comment

by:Swapnil Piparia
ID: 16998469
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

Author Comment

ID: 16999325
That's fixed it. Thanks netswap :-)

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

21 Experts available now in Live!

Get 1:1 Help Now