Gridview RowDeleting Event in Asp.Net by using C#


In this article i'm going to explain about "How to Delete record from Gridview Row in Asp.net by using c# coding" with an example, you can find source code of this example below.

HTML Markup Language - Add Gridview to WebForm1.aspx

I'm adding Gridview control to my webpage and binding student data from database into gridview control by using an method called BindGridviewStudent .

<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

C# Coding : Adding Namespace

i'm adding this namespace, to create database connection and also retrieved data from database to bind gridview control by using this object {i.e SqlConnection , SqlCommand and SqlDataAdapter and also DataSet .

C# Coding : BindGridviewStudent Method

In this section i'm creating private method to bind data from database to gridview.


 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();
 }

 //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 :

GridViewRow gdRow = (GridViewRow)GridView1.Rows[e.RowIndex]; this line give index of row, i.e nothing but which row we are going to delete that row index value given based on that value i'm retrieving StudentID value from gridview and passing that studenid value to database to delete that record from database.

0 Komentar untuk "Gridview RowDeleting Event in Asp.Net by using C# "

Back To Top