Jason Nadal

Restless C#ding

GridView and DataKeyNames

If you're trying to create a gridview, automatically bound to either a SqlDataSource or an ObjectDataSource (the better of the two options), and want edit and delete functionality, do not forget to set the DataKeyNames property to the primary key of the table or object you are trying to delete. Otherwise Edit/Update seems to work, but Delete will complain that it cannot find the parameter, even if you are defining DeleteParameters in the DataSource itself.

Comments

Raj Kaimal said:


To add to that,

When using the SqlDataSource wizard (VS 2005), if the select method contains a primary key, the DataKeyNames will get set automatically.
# February 28, 2006 7:35 PM

Jonathan Roberts said:

You will not get a value back from GridViewItem.SelectedValue either
# March 1, 2006 6:01 PM

Andrew said:

This saved me. Took me the whole day and myriads of different methods before I stumbled upon this solution. Thanks.

# November 20, 2007 1:19 PM

GridView and DataKeyNames said:

Pingback from  GridView and DataKeyNames

# November 27, 2007 5:08 AM

mehregan said:

Thank you for your effort.

# January 3, 2008 6:14 AM

alexanderhowell said:

If I am using a custom select command, how do I tell the SqlDataSource which field is the primary key?

I have DataKeyNames set right, but it still doesn't work

# August 13, 2008 4:51 AM

Eric Shandil said:

Even adding extra datakeysnames will result into problems, if generate extra parameters is set to true.

Eric

www.businesssoftwaresolutions.com.fj

# October 22, 2008 5:24 PM

MasterMoez said:

I have a GridView with a unique DataKeyNames="ID" (working with 3.5), how do I retrieve the data key then, I don't want to add a hidden field.

Thanks a lot

# November 5, 2008 6:18 AM

Bharti said:

ya..it solves my problem...as i  m not able  to delete records...due to this

thnx

# July 1, 2009 3:06 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)