Javascript required
Skip to content Skip to sidebar Skip to footer

How to Clear Gridview in Asp Net Using Javascript

  • Updated date May 22, 2017
  • 2m
  • 39

In this article you will learn how to 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.     <head id="Head1"  runat= "server" >
  5.         <title>Untitled Page</title>
  6.         <style type="text/css" >
  7.             .Gridview {
  8.                 font-family: Verdana;
  9.                 font-size: 10pt;
  10.                 font-weight: normal;
  11.                 color: black;
  12.             }
  13.         </style>
  14.         <script type="text/javascript" >
  15.         </script>
  16.     </head>
  17.     <body>
  18.         <form id="form1"  runat= "server" >
  19.             <div>
  20.                 <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" >
  21.                     <Columns>
  22.                         <asp:BoundField DataField="id"  HeaderText= "S.No."  />
  23.                         <asp:BoundField DataField="name"  HeaderText= "Name"  />
  24.                         <asp:BoundField DataField="address"  HeaderText= "address"  />
  25.                         <asp:BoundField DataField="country"  HeaderText= "Country"  />
  26.                         <asp:CommandField ShowEditButton="true"  />
  27.                         <asp:CommandField ShowDeleteButton="true"  /> </Columns>
  28.                 </asp:GridView>
  29.             </div>
  30.             <div>
  31.                 <asp:Label ID="lblresult"  runat= "server" ></asp:Label>
  32.             </div>
  33.         </form>
  34.     </body>
  35.     </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 textName = (TextBox) row.Cells[0].Controls[0];
  60.         TextBox textadd = (TextBox) row.Cells[1].Controls[0];
  61.         TextBox textc = (TextBox) row.Cells[2].Controls[0];
  62.         GridView1.EditIndex = -1;
  63.         conn.Open();
  64.         SqlCommand cmd =new  SqlCommand( "update detail set name='"  + textName.Text + "',address='"  + textadd.Text + "',country='"  + textc.Text + "'where id='"  + userid + "'" , conn);
  65.         cmd.ExecuteNonQuery();
  66.         conn.Close();
  67.         gvbind();
  68.     }
  69. protected void  GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
  70.         GridView1.PageIndex = e.NewPageIndex;
  71.         gvbind();
  72.     }
  73. protected void  GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) {
  74.         GridView1.EditIndex = -1;
  75.         gvbind();
  76.     }
  77. }

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").

How to Clear Gridview in Asp Net Using Javascript

Source: https://www.c-sharpcorner.com/UploadFile/9f0ae2/gridview-edit-delete-and-update-in-Asp-Net/