Gridview RowDeleting Event in Asp.Net by using C#

Deleting Records from GridView in ASP.NET using C#

In this tutorial, we'll delve into the process of deleting records from a GridView in ASP.NET using C# coding. This example demonstrates a step-by-step guide, including HTML markup and C# coding to showcase the seamless integration of deleting records from a GridView.

HTML Markup Language - Add Gridview to WebForm1.aspx

The HTML markup adds the GridView control to the webpage, binding student data from the database. The BindGridviewStudent method efficiently handles the data binding process.


<asp:GridView ID="GridView1" CssClass = "Grid" runat="server" OnRowDeleting="OnRowDeleting" AutoGenerateColumns = "false">
<Columns>
 <asp:TemplateField HeaderText="StudentID">
  <ItemTemplate>
   <asp:Label ID="lblSID" runat="server" Text='<%# Bindundefined"studentid") %>'></asp:Label>
  </ItemTemplate>
  <ItemStyle Width="100px" />
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Name">
  <ItemTemplate>
   <asp:Label ID="lblSName" runat="server" Text='<%# Bindundefined"studentname") %>'></asp:Label>
   </ItemTemplate>
   <ItemStyle Width="100px" />
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Contact No">
  <ItemTemplate>
   <asp:Label ID="lblSPhone" runat="server" Text='<%# Bindundefined"studentphone") %>'></asp:Label>
  </ItemTemplate>
  <ItemStyle Width="100px" />
 </asp:TemplateField>
 <asp:TemplateField HeaderText="Contact No">
   <ItemTemplate>
    <asp:LinkButton ID="lnkDelete" runat="server" CommandName="delete"> Delete </asp:LinkButton>
   </ItemTemplate>
   <ItemStyle Width="100px" />
 </asp:TemplateField>
</Columns>
</asp:GridView

C# Coding : Adding Namespace

To establish a database connection and retrieve data for GridView binding, we include the necessary namespaces. These include SqlConnection, SqlCommand, SqlDataAdapter, and DataSet.


using System.Data;
using System.Data.SqlClient;

C# Coding : BindGridviewStudent Method

In the Page_Load event, we call the BindGridviewStudent method to populate the GridView with student data fetched from the database.


 public void Page_Load(-----)
 {
   if(!IsPostBack)
   {
     BindGridviewStudent();
   }
 }

 private void BindGridviewStudent()
 {
  SqlConnection con = new SqlConnection("Data Source=.; DataBase=master; User ID=sa; Password=12345;");
  SqlDataAdapter da = new SqlDataAdapter("Select * from Student", con);
  DataSet ds = new DataSet();
  da.fill(ds);
  GridView1.DataSource = ds;
  GridView1.DataBind();
 }
 
 
 

C# Coding: GridView RowDeleting Event

The GridView1_RowDeleting event handles the deletion process. It identifies the selected row index and retrieves the corresponding StudentID value. This value is then used to delete the record from the database.


 //Add here Gridview RowDeleteing Event
 protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
  GridViewRow gdRow = (GridViewRow)GridView1.Rows[e.RowIndex];
  Label lblID = (Label)gdRow.FindControl("lblSID");
  SqlConnection con = new SqlConnection("Data Source=.; DataBase=master; User ID=sa; Password=12345;");
  SqlCommand cmd = new SqlCommand("delete from Student where studentid='"+lblID.Text+"'", con);
  con.Open();
  int i = cmd.ExecuteNonQuery();
  con.Close();
  if (i > 0)
   {
     BindGridviewStudent();
   }
 }
 

Note :

The line GridViewRow gdRow = (GridViewRow)GridView1.Rows[e.RowIndex]; obtains the index of the row to be deleted. This index is then used to retrieve the StudentID value from the GridView and subsequently delete the corresponding record from the database.

This comprehensive guide provides a clear understanding of how to implement record deletion from a GridView in an ASP.NET application using C# coding.

Post a Comment

0 Comments