Default GridView Edit, Update and Delete


Hello Guys,
Welcome once again, one of my friend asked about how to edit, update and delete in GridView itself. Actually I have never post like (majorly it is used for beginner) that kind post in my blog so here is my experiment to do so…
Here is my .aspx  code…
<div>
    <asp:GridView ID="gvInfoMaster" runat="server" AutoGenerateColumns="false"
            AllowPaging="true"
            onpageindexchanging="gvInfoMaster_PageIndexChanging" 
            AutoGenerateEditButton="true" AutoGenerateDeleteButton="true"
            onrowediting="gvInfoMaster_RowEditing"
            onrowcancelingedit="gvInfoMaster_RowCancelingEdit"
            onrowdeleting="gvInfoMaster_RowDeleting" onrowupdating="gvInfoMaster_RowUpdating"  DataKeyNames="InfoID"
             >
    <Columns>
    <asp:BoundField HeaderText="Info" DataField="InfoName" SortExpression="InfoName" />
    <asp:BoundField HeaderText="Address" DataField="Address1" SortExpression="Address1" />
    <asp:BoundField HeaderText="Pin" DataField="Pin" SortExpression="Pin" />
    <asp:BoundField HeaderText="Web" DataField="Web" SortExpression="Web" />
    <asp:BoundField HeaderText="Phone" DataField="Phone" SortExpression="Phone" />
    </Columns>
    </asp:GridView>
    </div>

Here is my .aspx.cs page..
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class AutoComplete : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridView();
        }
    }

    protected void BindGridView()
    {
            DataTable dtInfoMaster = null;
            clsDAL objDal = new clsDAL();
            string query = "SELECT InfoID,InfoName, Address1, Pin, Web, Phone FROM InfoMaster WHERE Active=1";
            dtInfoMaster = objDal.GetDataTable(query);
            if (dtInfoMaster.Rows.Count > 0)
            {
                gvInfoMaster.DataSource = dtInfoMaster;
                gvInfoMaster.DataBind();
            }
   
    }
   
    protected void gvInfoMaster_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvInfoMaster.PageIndex = e.NewPageIndex;
        BindGridView();
    }
    protected void gvInfoMaster_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvInfoMaster.EditIndex = e.NewEditIndex;
        BindGridView();
    }
    protected void gvInfoMaster_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int InfoID = Convert.ToInt32(gvInfoMaster.DataKeys[e.RowIndex].Value);
        TextBox txtInfoName = ((TextBox)gvInfoMaster.Rows[e.RowIndex].Cells[1].Controls[0]);
        TextBox txtAddress1 = ((TextBox)gvInfoMaster.Rows[e.RowIndex].Cells[2].Controls[0]);
        TextBox txtPin = ((TextBox)gvInfoMaster.Rows[e.RowIndex].Cells[3].Controls[0]);
        TextBox txtWeb = ((TextBox)gvInfoMaster.Rows[e.RowIndex].Cells[4].Controls[0]);
        TextBox txtPhone = ((TextBox)gvInfoMaster.Rows[e.RowIndex].Cells[5].Controls[0]);

        string updateQry = "UPDATE dbo.InfoMaster SET InfoName = '" + txtInfoName.Text + "', Address1 = '" + txtAddress1.Text + "', Pin = '" + txtPin.Text + "',Web = '" + txtWeb.Text + "',Phone = '" + txtPhone.Text + "' WHERE InfoID = " + InfoID + "";
        clsDAL objDal = new clsDAL();
        objDal.ExecuteNonQuery(updateQry);
        gvInfoMaster.EditIndex = -1;
        BindGridView();

    }
    protected void gvInfoMaster_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvInfoMaster.EditIndex = -1;
        BindGridView();
    }
    protected void gvInfoMaster_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int infoID = Convert.ToInt32(gvInfoMaster.DataKeys[e.RowIndex].Value);
        string deleteQry = "UPDATE dbo.InfoMaster SET Active=0 WHERE infoID = " + infoID + "";
        clsDAL objDal = new clsDAL();
        objDal.ExecuteNonQuery(deleteQry);
        gvInfoMaster.EditIndex = -1;
        BindGridView();
    }
}


Note: I have already declared my clsDAL class in past post..

Comments