Friday 8 February 2013

update,cancel,edit,delete in gridview


how to delete, update, edit, cancel in gridview....

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowEditing="editRecord"
            OnRowUpdating="updateRecord" OnRowCancelingEdit="cancelRecord">
            <Columns>
                <asp:TemplateField HeaderText="EmpId">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpId" runat="server" Text='<%#Bind("Emp_Id")%>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EmpName">
                    <ItemTemplate>
                        <asp:Label ID="lblEmpName" runat="server" Text='<%#Bind("Emp_Name")%>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="txtEmpName" runat="server" Text='<%# Bind("Emp_Name") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Operation">
                    <ItemTemplate>
                        <asp:Button ID="btn_edit" CommandName="Edit" runat="server" Text="EDIT" />
                    </ItemTemplate>
                    <EditItemTemplate>
                    <asp:Button ID="btn_update" CommandName="Update" runat="server" Text="Update" />
                    <asp:Button ID="btn_cancel" CommandName="Cancel" runat="server" Text="Cancel" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



----------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
   

public partial class Default3 : System.Web.UI.Page
{

    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader rd;
    public Default3()
    {
        con = new SqlConnection("initial catalog=practicecac; data source=om-pc; integrated security=yes");
    }
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if(!IsPostBack)
        {
            gridfill();
        }

    }
     
        public void gridfill()
    {
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        cmd = new SqlCommand("select * from Employee_Test",con);
        rd=cmd.ExecuteReader();
        GridView1.DataSource = rd;
        GridView1.DataBind();
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
    }
    protected void cancelRecord(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        gridfill();
    }
    protected void editRecord(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        gridfill();
    }
    protected void updateRecord(object sender, GridViewUpdateEventArgs e)
    {

        con.Open();
       Label id =GridView1.Rows[e.RowIndex].FindControl("lblEmpId") as Label;
       TextBox txtEmpName=GridView1.Rows[e.RowIndex].FindControl("txtEmpName") as TextBox;
        cmd = new SqlCommand("update Employee_Test set Emp_Name='"+txtEmpName.Text+"' where Emp_Id='"+id.Text+"'",con);
        int t=cmd.ExecuteNonQuery();
        if (t > 0)
        {
            gridfill();
            Response.Write("recored updated successfully");
        }
       
    }

 
}