Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a delete button in GridView?

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?
like image 923
Or Betzalel Avatar asked Sep 18 '09 23:09

Or Betzalel


People also ask

How to add Delete Button in GridView asp net?

You can use the RowDeleting event, by storing the user name in the data key collection you can access it programmatically. Then, in the code behind use the data key to delete the record.

How to Delete a row in GridView in asp net using link Button?

When the Delete Button is clicked, the OnRowDeleting event handler is executed. Inside the OnRowDeleting event handler, the Index of the GridView Row is determined and and it is used to delete the Row from the DataTable. Finally the DataTable is saved back to the ViewState and the GridView is again populated.

How enable edit and delete in GridView in ASP NET?

The GridView is populated using the records from the Customers table inside the Page Load event of the ASP.Net page. When the Edit Button is clicked, the GridView's OnRowEditing event handler is triggered. Here simply the EditIndex of the GridView is updated with the Row Index of the GridView Row to be edited.

How to remove row from GridView in asp net using c#?

Count; for (int i = 0; i <= iCount; i++) { GVGLCode. DeleteRow(i); } GVGLCode. DataBind(); c#


2 Answers

Here is a great article about typical usages of DataGrid.

Enjoy.

like image 155
Eran Betzalel Avatar answered Sep 21 '22 10:09

Eran Betzalel


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); 
}
like image 29
Phaedrus Avatar answered Sep 22 '22 10:09

Phaedrus