Gridview - Add, Edit and delete
In this article we create a Gridview from database and add some additional operations such a insert, edit and delete and bind data in the GridView control. In the previous article , we learned how to create a simple GridView at runtime .
First of all we create following table in SQL DATABASE:-
Create table tblRecord ( Id int primary key, Name varchar(200),Branch varchar(50) )
Now we can add following code in our project or design as following code:-
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
After design complete we add following code
Default.aspx.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Data; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data source=PS-PC\\SANTOSH; Database=dbSantoshTest; Integrated Security=true"); //connection is created..... protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { loadRecord(); } } protected void loadRecord() //bind gridview..... { con.Open(); SqlCommand cmd = new SqlCommand("Select * from tblRecord", con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); int count = ds.Tables[0].Rows.Count; con.Close(); if (ds.Tables[0].Rows.Count > 0) { gvDetail.DataSource = ds; gvDetail.DataBind(); } else { ds.Tables[0].Rows.Add(ds.Tables[0].NewRow()); gvDetail.DataSource = ds; gvDetail.DataBind(); int columncount = gvDetail.Rows[0].Cells.Count; lblMessage.Text = " No data found !!!"; } } //ask confirm for delete record..... protected void gvDetail_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string id = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Id")); Button lnkbtnresult = (Button)e.Row.FindControl("ButtonDelete"); if (lnkbtnresult != null) { lnkbtnresult.Attributes.Add("onclick", "javascript:return deleteConfirm('" +id + "')"); } } } //go for edit..... protected void gvDetail_RowEditing(object sender, GridViewEditEventArgs e) { gvDetail.EditIndex = e.NewEditIndex; loadRecord(); } //canceling edit code...... protected void gvDetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvDetail.EditIndex = -1; loadRecord(); } //insert record..... protected void gvDetail_RowCommand(object sender, GridViewCommandEventArgs e) { try { if (e.CommandName.Equals("AddNew")) { TextBox id = (TextBox)gvDetail.FooterRow.FindControl("insId"); TextBox inName = (TextBox)gvDetail.FooterRow.FindControl("insName"); TextBox inBranch = (TextBox)gvDetail.FooterRow.FindControl("insBranch"); con.Open(); SqlCommand cmd = new SqlCommand("insert into tblRecord values(@Id,@Name,@Branch)", con); cmd.Parameters.AddWithValue("@Id", id.Text); cmd.Parameters.AddWithValue("@Name", inName.Text); cmd.Parameters.AddWithValue("@Branch", inBranch.Text); int result = cmd.ExecuteNonQuery(); con.Close(); if (result == 1) { loadRecord(); lblMessage.BackColor = Color.Green; lblMessage.ForeColor = Color.White; lblMessage.Text = id.Text + " Added successfully...... "; } else { loadRecord(); lblMessage.BackColor = Color.Red; lblMessage.ForeColor = Color.White; lblMessage.Text = id.Text + " Error while adding row....."; } } } catch (Exception ex) { lblMessage.Text = "Invalid input data...Try Again!"; } } //update code protected void gvDetail_RowUpdating(object sender, GridViewUpdateEventArgs e) { try { TextBox txtName = (TextBox)gvDetail.Rows[e.RowIndex].FindControl("txtName"); TextBox txtBranch = (TextBox)gvDetail.Rows[e.RowIndex].FindControl("txtBranch"); string id = gvDetail.DataKeys[e.RowIndex].Values["Id"].ToString(); con.Open(); SqlCommand cmd = new SqlCommand("update tblRecord set Name=@Name,Branch=@Branch where Id=@Id", con); cmd.Parameters.AddWithValue("@Name", txtName.Text); cmd.Parameters.AddWithValue("@Branch", txtBranch.Text); cmd.Parameters.AddWithValue("@Id", id); cmd.ExecuteNonQuery(); con.Close(); lblMessage.BackColor = Color.Blue; lblMessage.ForeColor = Color.White; lblMessage.Text = id + " Updated successfully........ "; gvDetail.EditIndex = -1; loadRecord(); } catch (Exception ex) { lblMessage.Text = "Invalid input data...Try Again!"; } } //delete record..... protected void gvDetail_RowDeleting(object sender, GridViewDeleteEventArgs e) { string id = gvDetail.DataKeys[e.RowIndex].Values["Id"].ToString(); con.Open(); SqlCommand cmd = new SqlCommand("delete from tblRecord where Id=" + id, con); int result = cmd.ExecuteNonQuery(); con.Close(); if (result == 1) { loadRecord(); lblMessage.BackColor = Color.Red; lblMessage.ForeColor = Color.White; lblMessage.Text = id + " Deleted successfully....... "; } } }