---
title: "Update, Delete, Edit GridView in Asp.Net"  
description: "The process of making GridView Editable, Updateable and Deleteable are almost used in the entire ASP.NET project..."  
author: "chandra sekhar"  
published: 2012-02-13  
updated: 2019-11-19  
canonical: https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 4 minutes  

---

# Update, Delete, Edit GridView in Asp.Net

#####

## Update, Delete, Edit GridView in Asp.Net

The process of making GridView Editable, Updateable and Deleteable are almost used in the entire ASP.NET project. So, making GridView Editable, Updateable and Deletable is one of the most important aspects according to any ASP.NET project.

Let’s create a simple demonstration to understand the concept of making **GridView Editable, Updateable, and Deleteable** in a much better manner.

In order to see it practically, you just need to follow the following steps.

Step1: – Create a simple **ASP.NET Web Application** for that just open Visual Studio **>> go to >> File >> New >> Project >> Web >> Select ASP.NET Empty Web Application.**

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/3b722ad6-58b8-4efc-a766-a0a05b51e13d.png)

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/4cfab6f6-b6ac-4efb-8cf9-f34cc37c2f1f.png)

Step2: - Now, simply just add a **Web Form** to your **ASP.NET**\
**Empty Web Application** for that just **go to >> Solution Explorer >> Right Click on the project name >> Add >> New Item >> Select Web Form.**

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/c185bf1b-6727-430c-8b61-9c63df65c5ce.png)

Now, simply just drag and drop **GridView** to your **Web Form** and allow the below **properties** to true**.**

**1.** Allow **AutoGenerateEditButton** to **True.**

**2.** Allow **AutoGenerateDeleteButton** to **True.**

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/3fca528e-c290-413d-8bfb-845723680949.png)

Now, as soon as you set the above **two properties** you will see the **GridView** like the below diagram.

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/1420ac53-1136-4739-a2d3-ad59b21af181.png)

Step3: - Now, let **bind** the **GridView** with Data for that just add the below\
**the code snippet** into your **WebForm.aspx.cs** file.

```
        //Below variable holds the Connection String.        string str = ConfigurationManager.AppSettings["Connect"];         protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                GridViewData();            }
}//Created a GridViewData method to bind data to GridView        //from the SQL Server DataBase Table.        public void GridViewData()        {            SqlConnection con = new SqlConnection(str);            con.Open();            SqlCommand com = new SqlCommand();            com.CommandText = "select * from Book";            com.Connection = con;            com.ExecuteNonQuery();            SqlDataAdapter adap = new SqlDataAdapter(com);            DataSet ds = new DataSet();            adap.Fill(ds);            GridView1.DataSource = ds;            GridView1.DataBind();        }
```

Step4: – This is the most important step while making **GridView Editable, Updateable and Deletable.**

Now, simply just add the below **Events** of the **GridView** control into your\
**Web** **Application**.

**1. OnRowEditing.**

**2. OnRowCancelingEdit.**

**3. OnRowUpdating.**

**4. OnRowDeleting.**

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/d91f86b7-9ca7-45d3-a6c1-dd160542c99c.png)

**Step5**: - Now, just add the below code snippet to make necessary changes to the respective events of the **GridView** control.

**1.** **OnRowEditing** adds the below code snippet.

```
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)        {            GridView1.EditIndex = e.NewEditIndex;            GridViewData();       }
```

**2.** **OnRowCanelingEdit** adds the below code snippet.

```
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)        {            GridView1.EditIndex = -1; GridViewData();        } 
```

**2.** OnRowDeleting adds the below code snippet.

```
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)       {            //The below line of code will hold the Cells[1] data of th GridView Control.            string id = GridView1.Rows[e.RowIndex].Cells[1].Text;            SqlConnection con = new SqlConnection(str);            con.Open();            SqlCommand com = new SqlCommand();            com.CommandText = "delete from Book where Book_Id = '"+id+"'";            com.Connection = con;            com.ExecuteNonQuery();            GridViewData();        }
```

**3.** OnRowUpdating adds the below code snippet.

```
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)        {            string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;            string bookname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;             string
author = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string price = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string quantity = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text; SqlConnection
con = new SqlConnection(str); con.Open(); SqlCommand com = new SqlCommand();
com.CommandText = "update Book set Book_Name='" + bookname +
"',Book_Author='" + author + "',Book_Price='" + price +
"',Book_Quantity='" + quantity + "' where Book_Id= '" + id
+ "'"; com.Connection = con; com.ExecuteNonQuery(); GridViewData(); }
```

**Note: -** You can modify your code according to your requirements.

Step6: - Now, let’s run your Web Application and see the respective results.

Let’s first see the result for deleting.

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/7aefb186-459a-4494-9f7d-8fc61e76da8e.png)

The above diagram is my **output** of the loaded **GridView** and note that I am going to delete the **circled** row data from the **GridView** control.

**Now,** as soon as you click on the delete link will see the result like below diagram.

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/b1c06c61-4103-4dfd-94d4-44d007debb4f.png)

In the above result of diagram you can see that the data has been deleted.

Similarly, let’s see the result for **updating** the **below circled row data** in the **GridView** control.

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/322830ec-6d80-4337-8f27-1e46acb02e68.png)

Now, as soon as you click on the edit link you will see something like below diagram.

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/7244dc84-3057-42a7-8920-ed65631313df.png)

Now, let’s modify the Book_Author Name as Kalim Shaikh and click on the Update link and see whether the data is updated or not.

![Update, Delete, Edit GridView in Asp.Net](https://www.mindstick.com/mindstickarticle/d434cb61-50dc-4068-9601-e4f2a94a7f8d/images/e13d025e-03a4-4159-b03c-29b96a422ab5.png)

In the above output diagram you can clearly see that now the **Book_Author** name is modified to **Kalim Shaikh**.

\

You should also read this Article - **[Select Insert Update and Delete using Stored Procedure in ASP.NET MVC4](https://www.mindstick.com/articles/1110/select-insert-update-and-delete-using-stored-procedure-in-asp-dot-net-mvc4)**

---

Original Source: https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
