---
title: "how to edit update in gridview in c#.net"  
description: "how to edit update in gridview in c#.net"  
author: "Anonymous User"  
published: 2015-11-18  
updated: 2015-11-18  
canonical: https://www.mindstick.com/forum/33608/how-to-edit-update-in-gridview-in-c-sharp-dot-net  
category: ".net"  
tags: ["c#", "asp.net"]  
reading_time: 2 minutes  

---

# how to edit update in gridview in c#.net

I want to edit [update data](https://www.mindstick.com/forum/380/how-achieve-batch-update-data) in [gridview](https://www.mindstick.com/articles/873/update-delete-edit-gridview-in-asp-dot-net) how to do this please help me.

## Replies

### Reply by Anonymous User

```
   <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" OnRowCommand="GridView1_RowCommand1" >        <Columns>            <asp:TemplateField HeaderText="Emp_ID"  >                <ItemTemplate>                    <%#Eval("Emp_ID") %> <asp:HiddenField ID="EmpID" runat="server" Value='<%#Eval("Emp_ID") %>' />                </ItemTemplate>                <ControlStyle Width="100px"></ControlStyle>                <HeaderStyle Width="100px" />                <ItemStyle HorizontalAlign="Center" />            </asp:TemplateField>            <asp:TemplateField HeaderText="Emp_Name"  >                <ItemTemplate>                    <%#Eval("Emp_Name") %>                </ItemTemplate>                <EditItemTemplate>                    <asp:TextBox ID="txtname" runat="server" Text='<%# Bind("Emp_Name") %>'></asp:TextBox>                </EditItemTemplate>                <ControlStyle Width="300px"></ControlStyle>                <HeaderStyle Width="200px" />            </asp:TemplateField>            <asp:TemplateField HeaderText="Emp_Sal" >                <ItemTemplate>                    <%#Eval("Emp_Sal") %>                </ItemTemplate>                 <EditItemTemplate>                    <asp:TextBox ID="txtsal" runat="server" Text='<%# Bind("Emp_Sal") %>'></asp:TextBox>                </EditItemTemplate>                <ControlStyle Width="200px"></ControlStyle>                <HeaderStyle Width="200px" />            </asp:TemplateField>                <asp:TemplateField HeaderText="Emp_Sal" >                <ItemTemplate>                    <asp:Button ID="btnedit" runat="server" Text="Edit" CommandName="Change"  CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />                </ItemTemplate>                 <EditItemTemplate>                     <asp:Button ID="btnupdate" runat="server" Text="Update" CommandName="Save"  CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />                </EditItemTemplate>                <ControlStyle Width="200px"></ControlStyle>                <HeaderStyle Width="200px" />            </asp:TemplateField>        </Columns>        <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />        <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />        <RowStyle BackColor="White" ForeColor="#330099" />        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />        <SortedAscendingCellStyle BackColor="#FEFCEB" />        <SortedAscendingHeaderStyle BackColor="#AF0101" />        <SortedDescendingCellStyle BackColor="#F6F0C0" />        <SortedDescendingHeaderStyle BackColor="#7E0000" />    </asp:GridView>
```

```
using System;using System.Collections.Generic;using System.Configuration;using System.Data;using System.Data.SqlClient;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;
namespace Forumasp
{
    public partial class GridviewSorting : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridView1.DataSource = GetRecord(); //set DataSource GridView
                GridView1.DataBind(); //binding GridView
            }
        }

        public DataTable GetRecord()
        {
            DataTable dt = new DataTable();
            string constr = ConfigurationManager.ConnectionStrings["forumConnectionString"].ToString(); // connection string
            SqlConnection con = new SqlConnection(constr);
            con.Open();
            SqlCommand com = new SqlCommand("select *from Employee", con); // table name
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            dt = ds.Tables[0];
            return dt;
        }

        protected void GridView1_RowCommand1(object sender, GridViewCommandEventArgs e)
        {
            int index = Convert.ToInt32(e.CommandArgument.ToString());
            if (e.CommandName == "Change")
            {
                GridView1.EditIndex = index;
                GridView1.DataSource = GetRecord(); //set DataSource GridView
                GridView1.DataBind(); //binding GridView
            }
            else if (e.CommandName == "Save")
            {
                HiddenField EmpID = GridView1.Rows[index].FindControl("EmpID") as HiddenField;
                TextBox txtname = GridView1.Rows[index].FindControl("txtname") as TextBox;
                TextBox txtsal = GridView1.Rows[index].FindControl("txtsal") as TextBox;
                string constr = ConfigurationManager.ConnectionStrings["forumConnectionString"].ToString(); // connection string
                SqlConnection con = new SqlConnection(constr);
                con.Open();
                string sqlstr = "update Employee set Emp_Name='" + txtname.Text + "', Emp_Sal='" + txtsal.Text + "' where Emp_ID=" + EmpID.Value + "";
                SqlCommand cmd = new SqlCommand(sqlstr, con); // table name
                cmd.ExecuteNonQuery();
                con.Close();
                GridView1.EditIndex = -1;
                GridView1.DataSource = GetRecord(); //set DataSource GridView
                GridView1.DataBind(); //binding GridView
            }
        }

    }
}
```

```

```


---

Original Source: https://www.mindstick.com/forum/33608/how-to-edit-update-in-gridview-in-c-sharp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
