Article
    C#
    ADO.Net
    .NET
    ASP.Net & Web Forms
    Custom Controls
    Web Development
    Exception Handling
    XML
    Database
    Security in .Net
    Testing
    Web Services
    Windows Services
    Windows Controls
    WCF
    AJAX
    WPF
    XAML
    Reporting
    Setup
    VB.Net
    LINQ
    JQuery
    SilverLight
    JavaScript
    HTML5
    Crystal Report
    Cloud Computing
    Share Point
    Visual C++
    MVC
    Android
    PHP
    Java
    HTML
    WordPress
    Joomla
    Products
    Drupal
    Windows Phone
    JSON
    LightSwitch
    iPhone/iPad
    Ruby on Rails
    IIS 7
    Windows 8
    CSS/CSS3
    Excel
    MS Access
    Shortcut Keys
    Visual SourceSafe
    Team Foundation Server
    APIs
Follow Us
Follow _MindStick_ on Twitter View MindStick Software's LinkedIn profile View MindStick Software's Facebook profile
Top Contributor
Advertisement
Advertise with Us
Mindstick
Article Article  Forum Forum  Blog Blog  Quiz Quiz  Beginner Beginner  Careers Careers  Contact Contact  Login Login  
Home | Product | Services | About Us | Interview | DeveloperSection | Submit an Article | Submit Blog

Home >> ASP.Net & Web Forms >> Update, Delete, Edit GridView in Asp.Net
Update, Delete, Edit GridView in Asp.Net
Update, Delete, Edit GridView in Asp.Net


by chandra sekhar on 2/13/2012 8:20:48 PM

Views: 2130       Comments: 1

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.

Update, Delete, Edit GridView in Asp.Net

Update, Delete, Edit GridView in Asp.Net

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.

Update, Delete, Edit GridView in Asp.Net

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.

Update, Delete, Edit GridView in Asp.Net

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

Update, Delete, Edit GridView in Asp.Net

Step3: - Now, let bind the GridView with Data for that just add the below
code snippet in to your WebForm.aspx.cs file.

        //Below variable holds the Connection String.

        string str = ConfigurationManager.AppSettings["Connect"];

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                GridViewData();

            } }

//Created a GridViewData method to bind data to GridView

        //from the SQL Server DataBase Table.

        public void GridViewData()

        {

            SqlConnection con = new SqlConnection(str);

            con.Open();

            SqlCommand com = new SqlCommand();

            com.CommandText = "select * from Book";

            com.Connection = con;

            com.ExecuteNonQuery();

            SqlDataAdapter adap = new SqlDataAdapter(com);

            DataSet ds = new DataSet();

            adap.Fill(ds);

            GridView1.DataSource = ds;

            GridView1.DataBind();

        }

 

 

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.

Update, Delete, Edit GridView in Asp.Net

Step5: - now, just add the below code snippet to make necessary changes on the respective events of the GridView control.

1.       OnRowEditing add the below code snippet.

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

        {

            GridView1.EditIndex = e.NewEditIndex;

            GridViewData();

       }

 

 

2. OnRowCanelingEdit add the below code snippet.

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

        {

            GridView1.EditIndex = -1; GridViewData();

        }

 

 

2.       OnRowDeleting add the below code snippet.

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;

            SqlConnection con = new SqlConnection(str);

            con.Open();

            SqlCommand com = new SqlCommand();

            com.CommandText = "delete from Book where Book_Id = '"+id+"'";

            com.Connection = con;

            com.ExecuteNonQuery();

            GridViewData();

        }

 

 

3.       OnRowUpdating add the below code snippet.

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

            string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;

            string bookname = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;             string author = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text; string price = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text; string quantity = ((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text; SqlConnection con = new SqlConnection(str); con.Open(); SqlCommand com = new SqlCommand(); com.CommandText = "update Book set Book_Name='" + bookname + "',Book_Author='" + author + "',Book_Price='" + price + "',Book_Quantity='" + quantity + "' where Book_Id= '" + id + "'"; com.Connection = con; com.ExecuteNonQuery(); GridViewData(); }

 

 

Note: - You can modify your code according to your requirements.

Step6: - Now, let’s run your Web Application and see the respective results.

Let’s first see the result for deleting.

Update, Delete, Edit GridView in Asp.Net

The above diagram is my output of the loaded GridView and note that I am going to delete the circled row data from the GridView control.

Now, as soon as you click on the delete link will see the result like below diagram.

Update, Delete, Edit GridView in Asp.Net

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

Similarly, let’s see the result for updating the below circled row data in the GridView control.

Update, Delete, Edit GridView in Asp.Net

Now, as soon as you click on the edit link you will see something like below diagram.

Update, Delete, Edit GridView in Asp.Net

Now, let’s modify the Book_Author Name as Kalim Shaikh and click on the Update link and see whether the data is updated or not.

Update, Delete, Edit GridView in Asp.Net

In the above output diagram you can clearly see that now the Book_Author name is modified to Kalim Shaikh.

Report Abuse Form
Reason:    
 


row updating not working
by Arijit Bhadra 8/19/2012 7:04:32 PM
sir, row updating not working properly. a runtime exception has occured - "The object reference has not set to an instance of an object". kindly help me sir as soon as possible.
Report Abuse
Title :
Comment :
Text ColorBackground Color
BoldItalicUnderline
LeftCenterRightJustify
Ordered ListBulleted List
IndentOutdent
Horizontal Rule
SubscriptSuperscript
HyperlinkImage
Design ModeDesign
View HtmlHtml
     
 
Latest Article by chandra sekharRSS Feed
    
More...
Latest BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Top Viewed ArticlesRSS Feed
    
    
    
    
    
    
    
    
    
    
Top Viewed BlogsRSS Feed
    
    
    
    
    
    
    
    
    
    
Latest Interview QuestionsRSS Feed
    
    
    
    
    
    
    
    
    
    
More...
Total Online Users: 2680
Advertisement
MindStick DataConver
Advertise with Us
  
Copyright © 2009 - 2013MindStick. All Rights Reserved.