ASP.NET How to set and get key values for every gridrow in a gridview.

Setting primary key values during data binding:

gvUsersInRoles.DataKeyNames = new string[] { "UserId", "RoleId" };
This example uses composite primary key for showing more complicated usage.

Getting primary key values during performing row command (command, insert, delete, update):

Guid uid = new Guid(gvUsersInRoles.DataKeys[e.RowIndex]["UserId"].ToString());
int pid = int.Parse(gvUsersInRoles.DataKeys[e.RowIndex]["RoleId"].ToString());

No comments:

Post a Comment