views:

342

answers:

3

I made another Column in my GridView called delete. When delete is clicked, the row should be deleted or in other words, I need to get the current row's user name to delete it.

  1. Which event should I use? (RowDeleting, Rowdeleted etc...)
  2. How do I get the username from the current row?
A: 
tablename.Rows.RemoveAt(datagrid1.currentcell.rowindex);
lod3n
I want to delete the user from the database thus deleting him from the Gridview. I need to get the username
Or Betzalel
+1  A: 

Here is a great article about typical usages of DataGrid.

Enjoy.

Eran Betzalel
+3  A: 

You can use the RowDeleting event, by storing the user name in the data key collection you can access it programmatically.

<asp:GridView DataKeyNames="UserName" ID="GridView1" 
     runat="server" OnRowDeleting="GridView1_RowDeleting">

Then, in the code behind use the data key to delete the record.

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
  string userName = (string) GridView1.DataKeys[e.RowIndex].Value;
  DeleteUser(userName); 
}
Phaedrus