Delete data from Gridview using Command Field

In this article, I am going to explain how to delete a record from Gridview.This is very easy process and steps for this is as follows:

1. Add New Webpage in your Webapplication.

2. Drag Gridview from toolbox and bind it to database.

Note:If you need any help in binding the gridview to the database you can view my article which is:

3. Now we proceed further after successfully binding of data.The Steps for Deleting records from gridview are:

a: Select Edit columns from properties of Gridview.

b:You will see dialog box appears for Field:

c:You have to select the Command Fields option from the available field.In Command Fields you will find 3 events which are:
  • Edit,Update and Cancel.
  • Select
  • Delete
From the above Points select Delete Button.

d: Now fire the event RowDeleteing from Gridview Properties

Coding to delete the data from gridview on Event Row Deleting is:

            protected void GridView1_RowDeleting(object sender,          
      GridViewDeleteEventArgs e)
         {
           con.Open();
           Label id =(Label)GridView1.
           Rows[e.RowIndex].FindControl("Label5");
           SqlCommand save = new SqlCommand();
           save.Connection = con;
           save.CommandText="delete from tbtablename where id='"+     
           id.Text+"'";
           save.ExecuteNonQuery();
           save.Dispose();
           con.Close();
           luck(); //bind function name
          }

 If you any query in this article do ask me.

Thank You

           

Comments

  1. Pretty section of content. I just stumbled upon your weblog and in accession capital to say that I get in fact enjoyed account your weblog posts. Anyway I’ll be subscribing to your feeds or even I fulfillment you get entry to consistently quickly.

    Great Solution!

    ReplyDelete

Post a Comment

Popular posts from this blog

The Page Life Cycle of an ASP.NET and its controls.

Embed Tiny Editor in Your Web Application:

Introduction to .NET and .NET Architecture