How to use edit, delete in gridview

Hello Friends,

Today i am sharing one example for gridview edit and delete functionality. Example is for beginners who want to learn gridview edit and delete functionality.

To apply edit, delete functionality in your grid you should follow below steps.
In aspx page declare gridview control and add event OnRowEditing, OnRowUpdating, OnRowCancelingEdit and OnRowDeleting also add DataKeyNames for row value. Now, add CommandField in your grid. In my example i have declare grid as follow.

        
            
                
                    
                        
                    
                    
                        <%# Eval("BusinessEntityID")%>
                    
                
                
                    
                        First Name
                    
                    
                        <%# Eval("FirstName") %>
                    
                    
                        
                    
                
                
                    
                        Last Name
                    
                    
                        <%# Eval("LastName") %>
                    
                    
                        
                    
                
                
                    
                        Address
                    
                    
                        <%# Eval("AddressLine1") %>
                    
                    
                        
                    
                
                
                    
                        City
                    
                    
                        <%# Eval("City") %>
                    
                    
                        
                    
                
                
                    
                        Postal Code
                    
                    
                        <%# Eval("PostalCode") %>
                    
                    
                        
                    
                
                
            
        

In EditItemTemplate you can add textbox, radiobutton or checkbox etc…
In .cs file do following code

    protected void gvData_Editing(object sender, GridViewEditEventArgs e)
    {
        gvData.EditIndex = e.NewEditIndex;
        BindData();
    }

    protected void gvData_Updating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = gvData.Rows[e.RowIndex];
        int id = 0;
        int.TryParse(e.Keys["BusinessEntityID"].ToString(), out id);

        TextBox txtFirstName = (TextBox)row.FindControl("txtFirstName");
        TextBox txtLastName = (TextBox)row.FindControl("txtLastName");
        TextBox txtAddressLine1 = (TextBox)row.FindControl("txtAddressLine1");
        TextBox txtCity = (TextBox)row.FindControl("txtCity");
        TextBox txtPostalCode = (TextBox)row.FindControl("txtPostalCode");

        DBM.SqlCommandEx sqlCmd = new DBM.SqlCommandEx(
                    String.Format("UPDATE Person.Person" +
                    " SET firstName='{0}', lastName='{1}'" +
                    " WHERE BusinessEntityID = {2}"
                    , txtFirstName.Text, txtLastName.Text, id) +

                    String.Format(" UPDATE Person.Address" +
                    " SET addressLine1='{0}', city='{1}', postalCode='{2}'" +
                    " WHERE addressID = (SELECT addressID FROM Person.BusinessEntityAddress WHERE BusinessEntityID = {3})"
                    , txtAddressLine1.Text, txtCity.Text, txtPostalCode.Text, id)

                    );

        DataTable i = sqlCmd.GetDataTable();

        gvData.EditIndex = -1;
        BindData(); 
    }

    protected void gvData_Deleting(object sender, GridViewDeleteEventArgs e)
    {
        GridViewRow row = gvData.Rows[e.RowIndex];
        int id = 0;
        int.TryParse(e.Keys["BusinessEntityID"].ToString(), out id);

        // Delete Code Goes here Try it yourself

        gvData.EditIndex = -1;
        BindData();
    }

    protected void gvData_CancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvData.EditIndex = -1;
        BindData();
    }

EditIndex is used to edit the gridview row. It will tell that grid is in edit mode. Default EditIndex is -1 means no row in editmode.

That’s it all done now, you are ready.
I am attaching demo here GridView Edit Delete Example

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>