Here is the example code for that
ASPX:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:TemplateField HeaderText="EmpNO">
<ItemTemplate>
<%#Eval("Eno") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEno" runat="server" Text='<%#Eval("Eno") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIEno" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EName">
<ItemTemplate>
<%#Eval("Ename") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEname" runat="server" Text='<%#Eval("Ename") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIEname" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Salary">
<ItemTemplate>
<%#Eval("salary") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtloc" runat="server" Text='<%#Eval("salary") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtIloc" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterTemplate>
<asp:Button ID="btnSave" OnCommand="insert" Text="save" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>
.CS Code:
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Your Connection string");
if (!IsPostBack)
{
getdata();
}
}
SqlConnection con;
SqlCommand cmd;
private void getdata()
{
SqlDataAdapter da = new SqlDataAdapter("select * from empdet", con);
DataSet ds = new DataSet();
da.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
getdata();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox t1, t2, t3;
t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEno");
t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEname");
t3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtloc");
con.Open();
cmd = new SqlCommand("update empdet set eno=" + t1.Text + ",ename='" + t2.Text + "',salary=" + t3.Text + " where eno="+t1.Text+"", con);
cmd.ExecuteNonQuery();
con.Close();
}
public void insert(object s, EventArgs e)
{
TextBox t1, t2, t3;
t1 = (TextBox)GridView1.FooterRow.FindControl("txtIEno");
t2 = (TextBox)GridView1.FooterRow.FindControl("txtIEname");
t3 = (TextBox)GridView1.FooterRow.FindControl("txtIloc");
SqlConnection con = new SqlConnection("uid=sa; password=secret; data source=IN105-D\\SQL2012; database=SampleDB");
con.Open();
cmd = new SqlCommand("INSERT INTO empdet VALUES(" + t1.Text + ",'" + t2.Text + "'," + t3.Text + ")", con);
cmd.ExecuteNonQuery();
getdata();
con.Close();
}