code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace gridviewedit
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection _con = new SqlConnection("Data Source=db205.my-hosting-panel.com;Initial Catalog=goggery_sample;Persist Security Info=True;User ID=goggery_sample;Password=sample1.0");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
SqlCommand _cmd = new SqlCommand("select * from emp_nandu", _con);
DataSet _ds = new DataSet();
SqlDataAdapter _da = new SqlDataAdapter(_cmd);
_da.Fill(_ds);
GridView1.DataSource = _ds;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int EMP_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
Label Lbl = (Label)row.FindControl("lblemp_id");
TextBox txtename = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtename");
TextBox txtmanager = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmanager");
TextBox txtdept = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdept");
TextBox txtsal = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsal");
GridView1.EditIndex = -1;
0
SqlCommand _cmd = new SqlCommand("update emp_nandu set ENAME='"+txtename.Text+"',MANAGER_ID='"+txtmanager.Text+"',DEPT='"+txtdept.Text+"', SAL='"+txtsal.Text+"' where EMP_ID="+EMP_ID+"", _con);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["EMP_ID"].ToString());
// string username = GridView1.DataKeys[e.RowIndex].Values["UserName"].ToString();
_con.Open();
SqlCommand _cmd = new SqlCommand("delete from emp_nandu where EMP_ID='"+id+"'", _con);
_cmd.ExecuteNonQuery();
_con.Close();
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
}
}
asp page code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridviewedit.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 257px; width: 627px">
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EMP_ID"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
Height="211px"
Width="617px" >
<Columns>
<asp:TemplateField HeaderText="EMP_ID">
<EditItemTemplate>
<asp:TextBox ID="txtemp_id" runat="server" Text='<%# Eval("EMP_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblemp_id" runat="server" Text='<%# Eval("EMP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ENAME">
<EditItemTemplate>
<asp:TextBox ID="txtename" runat="server" Text='<%# Eval("ENAME") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblename" runat="server" Text='<%# Eval("ENAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MANAGER">
<EditItemTemplate>
<asp:TextBox ID="txtmanager" runat="server" Text='<%# Eval("MANAGER_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblmanager" runat="server" Text='<%# Eval("MANAGER_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPT">
<EditItemTemplate>
<asp:TextBox ID="txtdept" runat="server" Text='<%# Eval("DEPT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltext" runat="server" Text='<%# Eval("DEPT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SAL">
<EditItemTemplate>
<asp:TextBox ID="txtsal" runat="server" Text='<%# Eval("SAL") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsal" runat="server" Text='<%# Eval("SAL") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowCancelButton="true" />
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace gridviewedit
{
public partial class WebForm1 : System.Web.UI.Page
{
SqlConnection _con = new SqlConnection("Data Source=db205.my-hosting-panel.com;Initial Catalog=goggery_sample;Persist Security Info=True;User ID=goggery_sample;Password=sample1.0");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void bind()
{
SqlCommand _cmd = new SqlCommand("select * from emp_nandu", _con);
DataSet _ds = new DataSet();
SqlDataAdapter _da = new SqlDataAdapter(_cmd);
_da.Fill(_ds);
GridView1.DataSource = _ds;
GridView1.DataBind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
int EMP_ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
Label Lbl = (Label)row.FindControl("lblemp_id");
TextBox txtename = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtename");
TextBox txtmanager = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtmanager");
TextBox txtdept = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtdept");
TextBox txtsal = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtsal");
GridView1.EditIndex = -1;
0
SqlCommand _cmd = new SqlCommand("update emp_nandu set ENAME='"+txtename.Text+"',MANAGER_ID='"+txtmanager.Text+"',DEPT='"+txtdept.Text+"', SAL='"+txtsal.Text+"' where EMP_ID="+EMP_ID+"", _con);
_con.Open();
_cmd.ExecuteNonQuery();
_con.Close();
bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["EMP_ID"].ToString());
// string username = GridView1.DataKeys[e.RowIndex].Values["UserName"].ToString();
_con.Open();
SqlCommand _cmd = new SqlCommand("delete from emp_nandu where EMP_ID='"+id+"'", _con);
_cmd.ExecuteNonQuery();
_con.Close();
bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
}
}
asp page code:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="gridviewedit.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body style="height: 257px; width: 627px">
<form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EMP_ID"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
Height="211px"
Width="617px" >
<Columns>
<asp:TemplateField HeaderText="EMP_ID">
<EditItemTemplate>
<asp:TextBox ID="txtemp_id" runat="server" Text='<%# Eval("EMP_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblemp_id" runat="server" Text='<%# Eval("EMP_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ENAME">
<EditItemTemplate>
<asp:TextBox ID="txtename" runat="server" Text='<%# Eval("ENAME") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblename" runat="server" Text='<%# Eval("ENAME") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="MANAGER">
<EditItemTemplate>
<asp:TextBox ID="txtmanager" runat="server" Text='<%# Eval("MANAGER_ID") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblmanager" runat="server" Text='<%# Eval("MANAGER_ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="DEPT">
<EditItemTemplate>
<asp:TextBox ID="txtdept" runat="server" Text='<%# Eval("DEPT") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbltext" runat="server" Text='<%# Eval("DEPT") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SAL">
<EditItemTemplate>
<asp:TextBox ID="txtsal" runat="server" Text='<%# Eval("SAL") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblsal" runat="server" Text='<%# Eval("SAL") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowCancelButton="true" />
<asp:CommandField ShowEditButton="true" />
<asp:CommandField ShowDeleteButton="true" />
</Columns>
</asp:GridView>
</form>
</body>
</html>