Prizes & Awards
My Profile
Active Members
TodayLast 7 Days
more...
|
Resources » Code Snippets » ASP.NET GridView »
Gridview with edit,delete operations
|
Description : This code shows basic gridview operations like Edit, Delete etc. in Asp.net
//HTML code
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" PageSize="3" onpageindexchanging="GridView1_PageIndexChanging"> <Columns> <asp:TemplateField HeaderText="EmployeeNo"> <ItemTemplate> <%#Eval("EmployeeNo")%> </ItemTemplate> <EditItemTemplate > <asp:TextBox ID="txtEmployeeNo" runat="server" Text=' <%#Eval("EmployeeNo")%> '> </asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <%#Eval("Name")%> </ItemTemplate> <EditItemTemplate > <asp:TextBox ID="txtName" runat="server" Text=' <%#Eval("Name")%> '> </asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Email"> <ItemTemplate> <%#Eval("Email")%> </ItemTemplate> <EditItemTemplate > <asp:TextBox ID="txtEmail" runat="server" Text=' <%#Eval("Email")%> '> </asp:TextBox> </EditItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
//int the .cs file //Gridview with edit,delete operations
public partial class GridOperations : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) //Bind the GridView from database bind(); } protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { //point to current index GridView1.EditIndex = e.NewEditIndex; //Bind the GrisView from database bind();
} protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; //Bind the GridView from database bind(); } protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { //Row delete GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; TextBox email = (TextBox)row.FindControl("txtEmail"); SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Db;Integrated Security=True"); conn.Open(); SqlCommand cmd = new SqlCommand("delete from tab1 where Email='" + email.Text + "'", conn); cmd.ExecuteNonQuery(); conn.Close(); bind();
} protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex]; TextBox name= (TextBox)row.FindControl("txtName"); TextBox empno = (TextBox)row.FindControl("txtEmployeeNo"); TextBox email = (TextBox)row.FindControl("txtEmail"); GridView1.EditIndex = -1; SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Db;Integrated Security=True"); conn.Open(); SqlCommand cmd = new SqlCommand("update tab1 set Name='" + name.Text + " ',Email='" + email.Text + "' where EmployeeNo='" + empno.Text + "'", conn); cmd.ExecuteNonQuery(); conn.Close(); bind();
} //Bind the GrisView from database
public void bind() { SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Db;Integrated Security=True"); conn.Open(); SqlDataAdapter da = new SqlDataAdapter("select EmployeeNo,Name,Email from tab1", conn); DataSet ds = new DataSet(); da.Fill(ds, "emp"); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); conn.Close(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; bind(); } }
|
Responses
|
No responses found. Be the first to respond and make money from revenue sharing program.
|
|