ASP.Net GridView RowDeleting : retrieving the object to delete

XyBoy

Newcomer
Joined
Oct 29, 2003
Messages
18
Location
France
Hi,

I have some little experience with asp.net but I'm juste beginning with VS2008 and Linq. I'm trying to do a little control to manage a "User group" (allowing to add and remove users from the group).

So I defined a GridView like this :
Code:
<asp:GridView ID="GridUsers" runat="server" DataKeyNames="UserID" 
	onrowdeleting="GridUsers_RowDeleting" >
(with a "Delete" command field).

And in the code behind I have the following databinding :

Code:
GridUsers.DataSource = from u in DC.OJC_UserGroups_Users
where u.UserGroupID == this.UserGroupID
select new { u.UserID, u.User.FirstName, u.User.LastName };
GridUsers.DataBind();

But in the "GridUsers_RowDeleting" method, I can't find how to retrieve the UserID for the deleted row :

Code:
protected void GridUsers_RowDeleting(object sender, GridViewDeleteEventArgs e) {
int userID = (int)e.Keys[0];
Group.Users.Remove(Group.Users.First<OJC_UserGroups_Users>(x => x.UserID == userID));
DC.SubmitChanges();
}


I've looked everywhere on the web but I can't find the right way to do it.
I found this topic : http://forums.asp.net/t/1107245.aspx but the answer given by "adefwebserver" does not work (e.RowItem is not the Key : when I have on single row in my table, with UserID=2, e.RowItem = 0 !).

Please tell me if I'm engaged in a wrong way.

Thank you.
Olivier.
 
When you say:
e.RowItem = 0 that is the row index which user has clicked in. So you can use this like this:
Code:
GridUsers.DataKeys(e.RowItem).Value.ToString()
'(sorry it's in vb.net)
Let me know if this works.
 
Back
Top