Attention: We are retiring the ASP.NET Community Blogs.
Learn more >
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.
8 Comments
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.
You will not get a value back from GridViewItem.SelectedValue either
This saved me. Took me the whole day and myriads of different methods before I stumbled upon this solution. Thanks.
Thank you for your effort.
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
Even adding extra datakeysnames will result into problems, if generate extra parameters is set to true.
Eric
www.businesssoftwaresolutions.com.fj
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
ya..it solves my problem...as i m not able to delete records...due to this
thnx