How to delete selected row from gridview and database in c#

Deleting Selected Row from GridView and Database in C#

First, let’s assume you have a GridView bound to a database table or any data source. We will use the example of a GridView named “GridView1” bound to a “Customers” table with “CustomerID” as the primary key.

In order to delete a selected row from the GridView and the corresponding record from the database, you will need to follow these steps:

  1. Handle the GridView’s RowDeleting event
  2. Get the selected row’s data
  3. Delete the row from the GridView
  4. Delete the corresponding record from the database

Step 1: Handle the GridView’s RowDeleting event

Open your ASP.NET page containing the GridView and handle the RowDeleting event. You can do this either in the code-behind file (.cs) or in the markup file (.aspx).

“`csharp
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Code to delete the row and corresponding record
}
“`

Step 2: Get the selected row’s data

In the RowDeleting event handler, you can access the selected row’s data using the RowIndex property of the GridViewDeleteEventArgs parameter. You can then use this index to get the data from the GridView’s Rows collection.

“`csharp
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowIndex = e.RowIndex;
GridViewRow row = GridView1.Rows[rowIndex];

// Now you can access the row’s data using row.Cells[columnIndex].Text or by finding controls within the row
}
“`

Step 3: Delete the row from the GridView

In the RowDeleting event handler, after getting the selected row’s data, you can remove the row from the GridView’s Rows collection using the RemoveAt method, passing the row’s index.

“`csharp
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowIndex = e.RowIndex;
GridViewRow row = GridView1.Rows[rowIndex];

GridView1.Rows.RemoveAt(rowIndex);
}
“`

Step 4: Delete the corresponding record from the database

Finally, in the RowDeleting event handler, after removing the row from the GridView, you can proceed to delete the corresponding record from the database. You can use ADO.NET or any data access technology to perform the deletion operation.

“`csharp
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int rowIndex = e.RowIndex;
GridViewRow row = GridView1.Rows[rowIndex];

GridView1.Rows.RemoveAt(rowIndex);

string customerID = row.Cells[0].Text; // Assuming CustomerID is in the first column

// Code to delete the record from the database using the customerID
}
“`

Make sure to replace the placeholder comments (// Code to delete…) with the actual code specific to your project and the database technology you are using (e.g., ADO.NET, Entity Framework, etc.).

This is a basic example to demonstrate the process of deleting a selected row from a GridView and the corresponding record from a database. You can enhance and customize the solution based on your requirements and the structure of your GridView and database.

Leave a comment