Share Blog

Thursday, May 15, 2014

Update, Delete, Edit GridView in Asp.Net

Gridview :: Editable, Updateable and Deleteable in ASP.NET.
The process of making GridView Editable, Updateable and Deleteable is almost used in the entire ASP.NET project. So, making GridView Editable, Updateable and Deleteable is one of the most important aspects according to any ASP.NET project.
Let’s create a simple demonstration to understand the concept of making GridView Editable, Updateable and Deleteable in much better manner.
In order to see it practically you just need to follow the following steps.

Step1: – Create a simple ASP.NET Web Application for that just open Visual Studio >> go to >> File >> New >> Project >> Web >> Select ASP.NET Empty Web Application.

select ASP.NET Empty Web Site

Step2: - Now, simply just add a Web Form to your ASP.NET
Empty Web Application for that just go to >> Solution Explorer >> Right Click on the project name >> Add >> New Item >> Select Web Form.


Now, simply just drag and drop GridView to your Web Form and allow the below properties to true.

1. Allow AutoGenerateEditButton to True.
2. Allow AutoGenerateDeleteButton to True.

Now, as soon as you set the above two properties you will see the GridView like below diagram.



Step3: - Now, let bind the GridView with Data for that just add the below
code snippet in to your WebForm.aspx.cs file.
Step4: – This is the most important step while making GridView Editable, Updateable and Deleteable.
Now, simply just add the below Events of the GridView control in to your
Web Application.
1. OnRowEditing.
2. OnRowCancelingEdit.
3. OnRowUpdating.
4. OnRowDeleting

using System;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.UI;

namespace gridview_edit_updt_del
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //Below variable holds the Connection String.
        SqlConnection con = new SqlConnection("Data Source=SUNIL;Initial Catalog=adonetpractices;Integrated Security=True");

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GridBind();
            }
           
        }
        public void GridBind()
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from emp1",con);
            GridView1.DataSource = cmd.ExecuteReader();
            GridView1.DataBind();
            con.Close();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            GridBind();
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //The below line of code will hold the Cells[1] data of th GridView Control
            string id = GridView1.Rows[e.RowIndex].Cells[1].Text;
            SqlCommand cmd = new SqlCommand("delete from emp1 where id='"+id+"'",con);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            GridBind();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
            string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
            con.Open();
            SqlCommand cmd = new SqlCommand("update emp1 set name='"+name+"',address='"+address+"' where id='"+id+"'",con);
            cmd.ExecuteNonQuery();
            con.Close();
            GridBind();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            GridBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string Fullurl = "WebForm2.aspx/";
            OpenNewBrowserWindow(Fullurl, this);

        }
        public static void OpenNewBrowserWindow(string Url, Control control)
        {
            ScriptManager.RegisterStartupScript(control, control.GetType(), "Open", "window.open('" + Url + "');", true);
        }

    }
}

Result:

Step5: - Now, let’s run your Web Application and see the respective results.
Let’s first see the result for deleting ID-5

In the above result of diagram you can see that the data has been deleted.

Similarly, let’s see the result for updating the id -2 name-'sunil' in the GridView control.
Now, as soon as you click on the edit link you will see something like below diagram.

No comments:

Post a Comment