---
title: "Editable GridView in Asp.Net"  
description: "In this blog, I’m explaining how to make a gridview editable in asp.net     Step 1:             First create an asp.net empty web application and add"  
author: "Sumit Kesarwani"  
published: 2013-12-29  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/621/editable-gridview-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 3 minutes  

---

# Editable GridView in Asp.Net

In this blog, I’m explaining how to make a [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) editable in [asp.net](https://www.mindstick.com/articles/934/default-folders-available-inside-the-asp-dot-net-application-folder) Step 1:

First create an asp.net empty [web application](https://www.mindstick.com/articles/13069/progressive-web-application-pwas-all-you-need-to-know-about) and add a web form to the solution.

##### Step 2:

Now add a gridview by [drag and drop](https://www.mindstick.com/forum/454/how-to-drag-and-drop-multiple-image-from-one-canvas-to-onther-canvas-in-html5) from the toolbox and true these [properties](https://www.mindstick.com/articles/23331/nootropics-7-different-types-and-their-unique-properties):

· AutoGenerateDeleteButton

· AutoGenerateEditButton

\
![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/f05b2209-e638-486b-ad95-726af4ddaee7.png)

As you make true to these properties, your gridview will look like this:

\
![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/7cf1af08-8b6a-4659-9bb4-579a55344915.png)

```
<asp:GridView ID="gridViewEmployee" runat="server" AutoGenerateDeleteButton="True"AutoGenerateEditButton="True" BackColor="#6699FF" BorderColor="#CCFF66"BorderWidth="3px"><AlternatingRowStyle BackColor="#FF33CC" BorderColor="#FFFF99" BorderStyle="Solid" BorderWidth="3px" /><EditRowStyle BackColor="#FFFF99" BorderColor="Lime" BorderWidth="3px" /></asp:GridView>
```

##### Step 3:

Now add the below code in the .aspx.cs file:

```
protected void Page_Load(object sender, EventArgs e)        {            if (!IsPostBack)            {                bindData();            }        }public void bindData(){  SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString());            conn.Open();            SqlCommand cmd = new SqlCommand();            cmd.CommandText = "select * from Employee";            cmd.Connection = conn;            cmd.ExecuteNonQuery();            SqlDataAdapter adap = new SqlDataAdapter(cmd);            DataTable dt = new DataTable();            adap.Fill(dt);            gridViewEmployee.DataSource = dt;            gridViewEmployee.DataBind();}
```

When you run the above code, this will display all values from [Employee](https://www.mindstick.com/articles/85431/remote-employee-training-tips-tricks) table.

##### Step 4:

To make the gridview editable – add the following [events](https://www.mindstick.com/blog/102/events-in-c-sharp) to the gridview:

· RowEditing

· RowUpdating

· RowDeleting

· RowCancelingEdit

\
![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/d8e67c3b-9457-49d4-ba3a-dd60f49f6d3c.png)

When you add the above events to the gridview , the code in aspx file will look like this:

```
<asp:GridView ID="gridViewEmployee" runat="server" AutoGenerateDeleteButton="True"AutoGenerateEditButton="True" OnRowCancelingEdit="gridViewEmployee_RowCancelingEdit"OnRowDeleting="gridViewEmployee_RowDeleting" OnRowEditing="gridViewEmployee_RowEditing"OnRowUpdating="gridViewEmployee_RowUpdating" BackColor="#6699FF" BorderColor="#CCFF66"BorderWidth="3px">AlternatingRowStyle BackColor="#FF33CC" BorderColor="#FFFF99" BorderStyle="Solid"BorderWidth="3px" /><EditRowStyle BackColor="#FFFF99" BorderColor="Lime" BorderWidth="3px" /></asp:GridView>
```

##### Step 5:

Now add the code to the events:

##### RowEditing

```
protected void gridViewEmployee_RowEditing(object sender, GridViewEditEventArgs e)        {            gridViewEmployee.EditIndex = e.NewEditIndex;            bindData();        }
```

##### RowDeleting

```
protected void gridViewEmployee_RowDeleting(object sender, GridViewDeleteEventArgs e)        {            string id = gridViewEmployee.Rows[e.RowIndex].Cells[1].Text;            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString());            conn.Open();            SqlCommand cmd = new SqlCommand();            cmd.CommandText = "delete from Employee where id="+id+"";            cmd.Connection = conn;            cmd.ExecuteNonQuery();            bindData();        }
```

##### RowUpdating

```
protected void gridViewEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e)        {            string id = ((TextBox)gridViewEmployee.Rows[e.RowIndex].Cells[1].Controls[0]).Text;            string name = ((TextBox)gridViewEmployee.Rows[e.RowIndex].Cells[2].Controls[0]).Text;            string designation = ((TextBox)gridViewEmployee.Rows[e.RowIndex].Cells[3].Controls[0]).Text;            string salary = ((TextBox)gridViewEmployee.Rows[e.RowIndex].Cells[4].Controls[0]).Text;            string telephone = ((TextBox)gridViewEmployee.Rows[e.RowIndex].Cells[5].Controls[0]).Text;            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString());            conn.Open();            SqlCommand cmd = new SqlCommand();            cmd.CommandText = "Update Employee set name='"+name+"',designation='"+designation+"',salary='"+salary+"',telephone='"+telephone+"' where id="+id+"";            cmd.Connection = conn;            cmd.ExecuteNonQuery();            gridViewEmployee.EditIndex = -1;            bindData();        }
```

##### RowCancelingEdit

```
protected void gridViewEmployee_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)        {            gridViewEmployee.EditIndex = -1;            bindData();        }
```

Adding the above codes to the appropriate events will make the gridview editable and allow you to edit, update and delete the records from gridview.

##### Output

When you run the project, it will show you all the values from the database.

![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/05ec022e-d985-4037-aa19-1580a6f9465b.png)

When you click the edit [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net):

![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/18d95853-3fb8-4169-9fb9-d54ebdda4f89.png)

Update the values according to your need or you can cancel it:

I have updated the salary of “Mark David” from 180000 to 100000:

![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/39aac86b-3b47-4cc7-99f0-ed8435da8574.png)

When you click the delete button:

I have [deleted](https://www.mindstick.com/forum/145533/restore-the-accidentally-deleted-bookmarks-on-chrome) the “Mark David” record.

![Editable GridView in Asp.Net](https://www.mindstick.com/blogs/75207ec0-bc5a-492e-a303-9cfefb3980d1/images/3c072e09-6c35-49da-a61c-1688ab259eff.png)

---

Original Source: https://www.mindstick.com/blog/621/editable-gridview-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
