---
title: "Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net"  
description: "In this blog, I’m explaining how to add the textbox, dropdown, checkbox in editable gridview in asp.net  Step 1:Create an asp.net empty web applicatio"  
author: "Sumit Kesarwani"  
published: 2013-12-29  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/623/textbox-dropdown-checkbox-in-editable-gridview-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 5 minutes  

---

# Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net

In this blog, I’m explaining how to add the textbox, dropdown, checkbox in editable [gridview in asp.net](https://www.mindstick.com/forum/33607/how-to-use-sorting-in-gridview-in-asp-dot-net-c-sharp)\

##### Step 1:

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 it.

##### Step 2:

Add a gridview to the web from 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 add the columns to the gridview like this:

Add the below code to your .aspx file:

```
<asp:GridView ID="GridView1" runat="server"OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowCommand="GridView1_RowCommand"OnRowDataBound="GridView1_RowDataBound"OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"OnRowUpdating="GridView1_RowUpdating"ShowFooter="true" Width="75%" DataKeyNames="id" BorderColor="#CCCCCC" BorderStyle="Solid"BorderWidth="1px" AutoGenerateColumns="False" ><AlternatingRowStyle BackColor="#FFD4BA" /><FooterStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /><PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" /><HeaderStyle Height="30px" BackColor="#BC3670" Font-Size="15px" BorderColor="#CCCCCC"BorderStyle="Solid" BorderWidth="1px" /><RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"BorderWidth="1px" /><Columns><%--Name--%><asp:TemplateField HeaderText="Employee Name" HeaderStyle-Width="10%"><ItemTemplate><asp:Label ID="lblName" runat="server" Text='<%#Eval("name") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBox ID="txtName" runat="server" Text='<%#Eval("name") %>'></asp:TextBox></EditItemTemplate><FooterTemplate><asp:TextBox ID="txtAddName" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ValidationGroup="valGrp" ControlToValidate="txtAddName"ErrorMessage="*"></asp:RequiredFieldValidator></FooterTemplate></asp:TemplateField><%--Department--%><asp:TemplateField HeaderText="Department" HeaderStyle-Width="10%"><ItemTemplate><asp:Label ID="lblDepartment" runat="server" Text='<%#Eval("department") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:DropDownList ID="DropDownListDepartment" runat="server"Width="150"></asp:DropDownList></EditItemTemplate><FooterTemplate><asp:DropDownList ID="DropDownListAddDepartment" runat="server"Width="150"></asp:DropDownList><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate="DropDownListAddDepartment"ValidationGroup="valGrp"></asp:RequiredFieldValidator></FooterTemplate></asp:TemplateField><%--Salary--%><asp:TemplateField HeaderText="Salary" HeaderStyle-Width="10%"><ItemTemplate><asp:Label ID="lblSalary" runat="server" Text='<%#Eval("salary") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:TextBox ID="txtSalary" runat="server" Text='<%#Eval("salary") %>'></asp:TextBox></EditItemTemplate><FooterTemplate><asp:TextBox ID="txtAddSalary" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*"ControlToValidate="txtAddSalary" ValidationGroup="valGrp"></asp:RequiredFieldValidator></FooterTemplate></asp:TemplateField><%--Active--%><asp:TemplateField HeaderText="Active" HeaderStyle-Width="10%"><ItemTemplate><asp:Label ID="lblActive" runat="server" Text='<%#Eval("active") %>'></asp:Label></ItemTemplate><EditItemTemplate><asp:CheckBox ID="CheckBoxActive" Text="Active" runat="server" /></EditItemTemplate><FooterTemplate><asp:CheckBox ID="CheckBoxAddActive" Text="Active" runat="server" /></FooterTemplate></asp:TemplateField><%--Link--%><asp:TemplateField><ItemTemplate><asp:LinkButton ID="btnEdit" Text="Edit" CommandName="Edit" runat="server"/><asp:LinkButton ID="btnDelete" Text="Delete" CommandName="Delete" runat="server"/></ItemTemplate><EditItemTemplate><asp:LinkButton ID="btnUpdate" Text="Update" CommandName="Update" runat="server"/><asp:LinkButton ID="btnCancel" Text="Cancel" CommandName="Cancel" runat="server"/></EditItemTemplate><FooterTemplate><asp:Button ID="btnInset" Text="Insert" CommandName="Insert" runat="server"ValidationGroup="valGrp"/></FooterTemplate></asp:TemplateField></Columns></asp:GridView>
```

Your gridview will look like this.

\
![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/2c4ec755-e3b2-47f3-a20e-a354840df165.png)

And also add the below events to the gridview:

\
![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/a22c1658-9add-401b-b881-2bc4729d5172.png)

## Step 3:

Now add codes to the events which you have created:

protected void Page_Load(object sender, EventArgs e)

{

if (!IsPostBack)

{

LoadData();

}

}

public void LoadData()

{

string query = "select * from EmployeeTable;";

[SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString());

conn.Open();

[SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) cmd = new SqlCommand();

cmd.Connection = conn;

cmd.CommandText = query;

cmd.ExecuteNonQuery();

[SqlDataAdapter](https://www.mindstick.com/interview/33991/what-is-sqldataadapter) adap = new SqlDataAdapter(cmd);

[DataTable](https://www.mindstick.com/blog/195/datatable-in-ado-dot-net) dt = new DataTable();

adap.Fill(dt);

GridView1.DataSource = dt;

GridView1.DataBind();

}

private DataTable GetEmpDept()

{

//Get Employee department

DataTable dt = new DataTable();

dt.Columns.Add("DepName");

DataRow rw1 = dt.NewRow();

rw1[0] = "IT";

dt.Rows.Add(rw1);

DataRow rw2 = dt.NewRow();

rw2[0] = "Finance";

dt.Rows.Add(rw2);

DataRow rw3 = dt.NewRow();

rw3[0] = "Security";

dt.Rows.Add(rw3);

return dt;

}

## RowUpdating

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

bool blActive = false;

int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

TextBox name = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");

[DropDownList](https://www.mindstick.com/articles/324837/how-to-bind-dropdownlist-in-mvc-core-from-database-using-entity-framework) department = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownListDepartment");

TextBox salary = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSalary");

CheckBox active = (CheckBox)GridView1.Rows[e.RowIndex].FindControl("CheckBoxActive");

if (active.Checked)

blActive = true;

else

blActive = false;

using (SqlConnection Sqlcon = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString()))

{

using (SqlCommand cmd = new SqlCommand())

{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandType = CommandType.Text;

cmd.CommandText = "update EmployeeTable set name ='" + name.Text + "',department= '" + department.SelectedValue + "', salary = '" + salary.Text + "', active='" + blActive + "' where id='" + id + "' ";

cmd.ExecuteNonQuery();

}

}

GridView1.EditIndex = -1;

LoadData();

lblMessage.Text = "Record updated successfully!";

}

## RowEditing

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

LoadData();

}

## RowDeleting

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

using (SqlConnection Sqlcon = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString()))

{

using (SqlCommand cmd = new SqlCommand())

{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandType = CommandType.Text;

cmd.CommandText = "delete from EmployeeTable where id ="+id+"";

cmd.ExecuteNonQuery();

}

}

GridView1.EditIndex = -1;

LoadData();

lblMessage.Text = "Record Deleted";

}

## RowCancelingEdit

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e

{

GridView1.EditIndex = -1;

LoadData();

}

## RowCommand

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)

{

bool blActive = false;

if (e.CommandName.Equals("Insert"))

{

TextBox name = (TextBox)GridView1.FooterRow.FindControl("txtAddName");

DropDownList department = (DropDownList)GridView1.FooterRow.FindControl("DropDownListAddDepartment");

TextBox salary = (TextBox)GridView1.FooterRow.FindControl("txtAddSalary");

CheckBox active = (CheckBox)GridView1.FooterRow.FindControl("CheckBoxAddActive");

if (active.Checked)

blActive = true;

else

blActive = false;

using (SqlConnection Sqlcon = new SqlConnection(WebConfigurationManager.ConnectionStrings["Connect"].ToString()))

{

using (SqlCommand cmd = new SqlCommand())

{

Sqlcon.Open();

cmd.Connection = Sqlcon;

cmd.CommandType = CommandType.Text;

cmd.CommandText = "insert into EmployeeTable (name, department, salary, active)values('" + name.Text.Trim() + "','" + department.SelectedValue.ToUpper() + "','" + salary.Text.Trim() + "','" + blActive + "') ";

cmd.ExecuteNonQuery();

}

}

GridView1.EditIndex = -1;

LoadData();

lblMessage.Text = "Record inserted successfully!";

}

}

## RowDataBound

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

DataRowView drview = e.Row.DataItem as DataRowView;

if (e.Row.RowType == DataControlRowType.DataRow)

{

if ((e.Row.RowState & DataControlRowState.Edit) > 0)

{

DropDownList dpEmpdept = (DropDownList)e.Row.FindControl("DropDownListDepartment");

DataTable dt = GetEmpDept();

dpEmpdept.DataSource = GetEmpDept();

dpEmpdept.DataTextField = "DepName";

dpEmpdept.DataValueField = "DepName";

dpEmpdept.DataBind();

dpEmpdept.SelectedValue = drview[2].ToString();

CheckBox chkb = (CheckBox)e.Row.FindControl("CheckBoxActive");

if (drview[4].ToString() == "True")

{ chkb.Checked = true; }

else { chkb.Checked = false; }

}

}

if (e.Row.RowType == DataControlRowType.Footer)

{

DropDownList dp = (DropDownList)e.Row.FindControl("DropDownListAddDepartment");

dp.DataSource = GetEmpDept();

dp.DataTextField = "DepName";

dp.DataValueField = "DepName";

dp.DataBind();

}

}

## Output

When you run the project, you will get all the [record from database](https://www.mindstick.com/forum/159842/how-to-read-record-from-database-in-asp-mvc-core-using-entity-framework-core-1-0).

![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/c7ad5867-942c-4253-b459-1bac04244018.png)

When you click the update button, you will see the Employee Name change to textbox, Department Change to dropdown, Salary change to textbox and active change to checkbox.

![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/015a8943-6b72-4386-8768-58cbae3239e1.png)

Now you can update the record according to your need or you can cancel it.

![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/5f5d5cdb-bef8-4119-8692-1024ce7911b3.png)

I have updated the department and salary of “Mark David” record , once you have updated the record you will get a message.

Click on delete button to delete a particular record.

I have deleted the “Mark David” record and you will get a message like this:

\
![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/ee3320cd-5073-454a-a499-a05cbe11c281.png)

If you want to insert a new record you can insert like this:

![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/89384e3e-b65f-42bb-ba7a-00ef2dd0bc14.png)

The new record “Mark David” will insert when you click on insert button and you will get a message.

\

\
![Textbox, Dropdown, Checkbox in Editable GridView in Asp.Net](https://www.mindstick.com/blogs/0b0e785f-e45d-4632-9d19-bfc5c379341e/images/09322899-2542-45ee-9ae6-cc3a143f55e8.png)

---

Original Source: https://www.mindstick.com/blog/623/textbox-dropdown-checkbox-in-editable-gridview-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
