Gridview Edit Delete And Update In ASP.NET

Introduction

Websites often display thousands of data in a GridView in ASP.Net. Usually admin can view the registered users on the website, but when an admin wants to edit or delete any fraud or duplicate or damaged data from the table there is a method in GridView to edit, delete and update. See the note section below.

Source Code

  1. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3.     <html xmlns="http://www.w3.org/1999/xhtml">  
  4.   
  5.     <head id="Head1" runat="server">  
  6.         <title>Untitled Page</title>  
  7.         <style type="text/css">  
  8.             .Gridview {  
  9.                 font-family: Verdana;  
  10.                 font-size: 10pt;  
  11.                 font-weight: normal;  
  12.                 color: black;  
  13.             }  
  14.         </style>  
  15.         <script type="text/javascript">  
  16.         </script>  
  17.     </head>  
  18.   
  19.     <body>  
  20.         <form id="form1" runat="server">  
  21.             <div>  
  22.                 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="id" OnPageIndexChanging="GridView1_PageIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">  
  23.                     <Columns>  
  24.                         <asp:BoundField DataField="id" HeaderText="S.No." />  
  25.                         <asp:BoundField DataField="name" HeaderText="Name" />  
  26.                         <asp:BoundField DataField="address" HeaderText="address" />  
  27.                         <asp:BoundField DataField="country" HeaderText="Country" />  
  28.                         <asp:CommandField ShowEditButton="true" />  
  29.                         <asp:CommandField ShowDeleteButton="true" /> </Columns>  
  30.                 </asp:GridView>  
  31.             </div>  
  32.             <div>  
  33.                 <asp:Label ID="lblresult" runat="server"></asp:Label>  
  34.             </div>  
  35.         </form>  
  36.     </body>  
  37.   
  38.     </html>  

Design

The design part will look as in the following image:

GridView1.jpg

Code behind

  1. using System;  
  2. using System.Configuration;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Web;  
  8. using System.Web.Security;  
  9. using System.Web.UI;  
  10. using System.Web.UI.HtmlControls;  
  11. using System.Web.UI.WebControls;  
  12. using System.Web.UI.WebControls.WebParts;  
  13. using System.Xml.Linq;  
  14. public partial class _Default: System.Web.UI.Page {  
  15.     private SqlConnection conn = new SqlConnection("Data Source=NEHASHAMA;Integrated Security=true;Initial Catalog=rp");  
  16.     protected void Page_Load(object sender, EventArgs e) {  
  17.         if (!IsPostBack) {  
  18.             gvbind();  
  19.         }  
  20.     }  
  21.     protected void gvbind() {  
  22.         conn.Open();  
  23.         SqlCommand cmd = new SqlCommand("Select * from detail", conn);  
  24.         SqlDataAdapter da = new SqlDataAdapter(cmd);  
  25.         DataSet ds = new DataSet();  
  26.         da.Fill(ds);  
  27.         conn.Close();  
  28.         if (ds.Tables[0].Rows.Count > 0) {  
  29.             GridView1.DataSource = ds;  
  30.             GridView1.DataBind();  
  31.         } else {  
  32.             ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());  
  33.             GridView1.DataSource = ds;  
  34.             GridView1.DataBind();  
  35.             int columncount = GridView1.Rows[0].Cells.Count;  
  36.             GridView1.Rows[0].Cells.Clear();  
  37.             GridView1.Rows[0].Cells.Add(new TableCell());  
  38.             GridView1.Rows[0].Cells[0].ColumnSpan = columncount;  
  39.             GridView1.Rows[0].Cells[0].Text = "No Records Found";  
  40.         }  
  41.     }  
  42.     protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) {  
  43.         GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];  
  44.         Label lbldeleteid = (Label) row.FindControl("lblID");  
  45.         conn.Open();  
  46.         SqlCommand cmd = new SqlCommand("delete FROM detail where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);  
  47.         cmd.ExecuteNonQuery();  
  48.         conn.Close();  
  49.         gvbind();  
  50.     }  
  51.     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) {  
  52.         GridView1.EditIndex = e.NewEditIndex;  
  53.         gvbind();  
  54.     }  
  55.     protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) {  
  56.         int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());  
  57.         GridViewRow row = (GridViewRow) GridView1.Rows[e.RowIndex];  
  58.         Label lblID = (Label) row.FindControl("lblID");  
  59.         //TextBox txtname=(TextBox)gr.cell[].control[];  
  60.         TextBox textName = (TextBox) row.Cells[0].Controls[0];  
  61.         TextBox textadd = (TextBox) row.Cells[1].Controls[0];  
  62.         TextBox textc = (TextBox) row.Cells[2].Controls[0];  
  63.         //TextBox textadd = (TextBox)row.FindControl("txtadd");  
  64.         //TextBox textc = (TextBox)row.FindControl("txtc");  
  65.         GridView1.EditIndex = -1;  
  66.         conn.Open();  
  67.         //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);  
  68.         SqlCommand cmd = new SqlCommand("update detail set name='" + textName.Text + "',address='" + textadd.Text + "',country='" + textc.Text + "'where id='" + userid + "'", conn);  
  69.         cmd.ExecuteNonQuery();  
  70.         conn.Close();  
  71.         gvbind();  
  72.         //GridView1.DataBind();  
  73.     }  
  74.     protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {  
  75.         GridView1.PageIndex = e.NewPageIndex;  
  76.         gvbind();  
  77.     }  
  78.     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {  
  79.         GridView1.EditIndex = -1;  
  80.         gvbind();  
  81.     }  
  82. }  

Save all or press "Ctrl+S" and hit "F5" to run the page, the page will look as in the following image:

GridView2.jpg
Click on "Edit the GridView", it will display Textboxes in each cell as in the following image:

GridView3.jpg
Edit the value(s) here and click on the Update link, it will update all the data or to remove it click on the "Delete" link above the image shown.

Note

One note will be helpful for you, while describing Columns in GridView if you are using a boundfield then create objects of the control using cells[index] in the rowupdateing event of GridView, but if you are using controls itself like Label or textboxes etc then use Fincontrol("stringid").


Similar Articles