Jump to content
Xtreme .Net Talk

ASP.Net GridView RowDeleting : retrieving the object to delete


Recommended Posts

Posted

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 :

<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 :

 

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 :

 

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.

  • 1 month later...
Posted

When you say:

e.RowItem = 0 that is the row index which user has clicked in. So you can use this like this:


GridUsers.DataKeys(e.RowItem).Value.ToString()
'(sorry it's in vb.net)
[/Code]

Let me know if this works.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...