You must Sign In to post a response.
  • Category: ASP.NET

    How to edit update delete in gridview

    Hi!


    I want How to edit, update delete in gridview control.Pls give example codings.

    Thanks

    Kani
  • #639876
    Please check this below article for Delete Update and edit in a grid view.


    geekswithblogs.net/dotNETvinz/archive/2009/02/22/gridview-insert-edit-update-and-delete--the-ado.net-way.aspx

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #639882
    I have used the following table structure, you can change it as per your requirement:


    CREATE TABLE student(
    sid CHAR(5) PRIMARY KEY,
    sname VARCHAR(20) NOT NULL,
    sadd VARCHAR(50),
    smarks INT NOT NULL)



    Please check the following code:

    Design
    ------


    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowEditing="GridView1_RowEditing">
    <Columns>
    <asp:TemplateField HeaderText="ID">
    <ItemTemplate>
    <asp:Label ID="LblSId" runat="server" Text='<%#Eval("sid") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Label ID="LblEditSid" runat="server" Text='<%#Eval("sid") %>'></asp:Label>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID="LblSName" runat="server" Text='<%#Eval("sname") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="TxtEditSName" runat="server" Text='<%#Eval("sname") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Address">
    <ItemTemplate>
    <asp:Label ID="LblSAdd" runat="server" Text='<%#Eval("sadd") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="TxtEditSAdd" runat="server" Text='<%#Eval("sadd") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Marks">
    <ItemTemplate>
    <asp:Label ID="LblSMarks" runat="server" Text='<%#Eval("smarks") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="TxtEditSMarks" runat="server" Text='<%#Eval("smarks") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Edit">
    <ItemTemplate>
    <asp:Button ID="BtnEdit" runat="server" CommandName="Edit" Text="Edit" />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Button ID="BtnUpdate" runat="server" CommandName="Update" Text="Update" OnClick="BtnUpdate_Click" />
    <asp:Button ID="BtnCancel" runat="server" CommandName="Cancel" Text="Cancel" OnClick="BtnCancel_Click" />
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete">
    <ItemTemplate>
    <asp:Button ID="BtnDelete" runat="server" Text="Delete" OnClick="BtnDelete_Click" />
    </ItemTemplate>
    <EditItemTemplate>
    </EditItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>


    C# Code Behind
    --------------


    SqlConnection con;
    DataSet ds;
    SqlDataAdapter sqlda;
    SqlCommand com;

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!Page.IsPostBack)
    {
    BindData();
    }
    }

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

    protected void BtnDelete_Click(object sender, EventArgs e)
    {
    Button btn = sender as Button;
    GridViewRow grow = (GridViewRow)btn.NamingContainer;
    string sid = ((Label)grow.FindControl("LblSId")).Text;
    string query = "DELETE student WHERE sid = '" + sid + "'";
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
    com = new SqlCommand(query, con);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    BindData();
    }

    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
    Button btn = sender as Button;
    GridViewRow grow = (GridViewRow)btn.NamingContainer;
    string sid = ((Label)grow.FindControl("LblEditSId")).Text;
    string sname =((TextBox)grow.FindControl("TxtEditSName")).Text;
    string sadd = ((TextBox)grow.FindControl("TxtEditSAdd")).Text;
    string smarks = ((TextBox)grow.FindControl("TxtEditSmarks")).Text;
    string query = "UPDATE student SET sname ='" + sname + "', sadd ='" + sadd + "', smarks ='" + smarks + "' WHERE sid = '" + sid + "'";
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
    com = new SqlCommand(query, con);
    con.Open();
    com.ExecuteNonQuery();
    con.Close();
    GridView1.EditIndex = -1;
    BindData();
    }

    protected void BtnCancel_Click(object sender, EventArgs e)
    {
    GridView1.EditIndex = -1;
    BindData();
    }

    public void BindData()
    {
    con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlcon"].ToString());
    ds = new DataSet();
    sqlda = new SqlDataAdapter("SELECT * FROM student", con);
    sqlda.Fill(ds, "student");
    GridView1.DataSource = ds;
    GridView1.DataBind();
    }


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #639903
    hi,
    please check example code what i used for my program.'
    Design page:
    <%@ Page Language="C#" AutoEventWireup="true" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    </script>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
    #form1
    {
    height: 17px;
    }
    </style>
    </head>
    <body>

    <form id="form1" runat="server">

    <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="margin-top: 0px"
    >
    <Columns>
    <asp:TemplateField HeaderText="ID">
    <ItemTemplate>
    <asp:Label ID="LblSId" runat="server" Text='<%#Eval("username") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Label ID="LblEditSid" runat="server" Text='<%#Bind("username") %>'></asp:Label>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID ="LblSName" runat="server" Text='<%#Eval("password") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="TxtEditSName" runat="server" Text='<%#Bind("password") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <%--<asp:TemplateField HeaderText="Address">
    <ItemTemplate>
    <asp:Label ID ="LblSAdd" runat="server" Text ='<%#Eval("sadd") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate >
    <asp:TextBox ID ="TxtEditSAdd" runat ="server" Text ='<%Bind("sadd") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Marks">
    <ItemTemplate>
    <asp:Label ID="LblSMarks" runat ="server" Text ='<%Eval("smarks") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate >
    <asp:TextBox ID="TxtEditSMarks" runat ="server" Text ='<%Bind("smarks") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>--%>
    <asp:TemplateField HeaderText ="Edit">
    <ItemTemplate>
    <asp:Button ID ="BtnEdit" runat ="server" CommandName ="Edit" Text ="Edit" />
    </ItemTemplate>
    <EditItemTemplate >
    <asp:Button ID ="BtnUpdate" runat ="server" CommandName ="Update" Text ="Update" OnClientClick="BtnUpdate" />
    <asp:Button ID ="BtnCancel" runat ="server" CommandName ="Cancel" Text ="Cancel" OnClientClick ="BtnCancel" />
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText ="Delete">
    <ItemTemplate >
    <asp:Button ID ="BtnDelete" runat ="server" CommandName ="Delete" Text ="Delete" OnClientClick="BtnDelete" />
    </ItemTemplate>
    <EditItemTemplate >
    </EditItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>
    </form>
    </body>
    </html>

    C# Code:
    using System;
    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;
    using System.Data.SqlClient;

    public partial class _Default : System.Web.UI.Page
    {
    SqlConnection con;
    DataSet ds;
    SqlDataAdapter da;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {

    if (Page.IsPostBack)
    {
    BindData();
    }
    }
    public void BindData()
    {
    con = new SqlConnection("server=.\\SQLEXPRESS;database=master;integrated security=true");
    ds = new DataSet();
    da = new SqlDataAdapter("SELECT * FROM users", con);
    da.Fill(ds, "users");
    GridView1.DataSource = ds;
    GridView1.DataBind();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    GridView1.EditIndex = e.NewEditIndex;
    BindData();
    }

    protected void BtnDelete_click(object sender, EventArgs e)
    {
    Button btn = sender as Button;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    string username = ((Label)row.FindControl("LblSId")).Text;
    string query = "DELETE users WHERE username = '" + username + "'";
    con = new SqlConnection("server=.\\SQLEXPRESS;database=master;integrated security=true");
    cmd = new SqlCommand(query, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    BindData();
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
    Button btn = sender as Button;
    GridViewRow row = (GridViewRow)btn.NamingContainer;
    string username = ((Label)row.FindControl("LblEditSId")).Text;
    string password = ((TextBox)row.FindControl("TxtEditSName")).Text;
    //string sadd = ((TextBox)row.FindControl("TxtEditSAdd")).Text;
    //string smarks = ((TextBox)row.FindControl("TxtEditSmarks")).Text;
    string query = "UPDATE student SET password ='" + password + "', WHERE username = '" + username + "'";
    con = new SqlConnection("server=.\\SQLEXPRESS;database=master;integrated security=true");
    cmd = new SqlCommand(query, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    GridView1.EditIndex = -1;
    BindData();
    }
    protected void BtnCancel_Click(object sender, EventArgs e)
    {
    GridView1.EditIndex = -1;
    BindData();
    }

    }

  • #640055
    Try below code:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    area.Visible = false;
    BindGrid();
    }
    }
    public void BindGrid()
    {
    string connection = "Data Source=dell;Initial Catalog=master;Integrated Security=true";
    SqlConnection con = new SqlConnection(connection);
    string query = "select * from employees where isactive=1";
    SqlDataAdapter da = new SqlDataAdapter(query, con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    gvEdit.DataSource = dt;
    gvEdit.DataBind();
    }
    protected void gvEdit_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    gvEdit.EditIndex = -1;
    BindGrid();
    }
    protected void gvEdit_RowEditing(object sender, GridViewEditEventArgs e)
    {
    gvEdit.EditIndex = e.NewEditIndex;
    BindGrid();
    }
    protected void gvEdit_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string connection = "Data Source=dell;Initial Catalog=master;Integrated Security=true";
    SqlConnection con = new SqlConnection(connection);
    SqlCommand mySqlUpdate = new SqlCommand("update employees set name = @name, notes = @notes where id = @id",con);




    // FindControl function used to get the reference to textbox controls
    // placed inside the EditItemTemplate of GridView control
    TextBox txtName = (TextBox)gvEdit.Rows[e.RowIndex].Cells[2].FindControl("txtName");

    TextBox txtNotes = (TextBox)gvEdit.Rows[e.RowIndex].Cells[3].FindControl("txtNotes");
    Label lblId = (Label)gvEdit.Rows[e.RowIndex].Cells[1].FindControl("lblId");

    // parameters passed to the SQL Update Command
    mySqlUpdate.Parameters.Add("@name", SqlDbType.VarChar).Value = txtName.Text;

    mySqlUpdate.Parameters.Add("@notes", SqlDbType.VarChar).Value = txtNotes.Text;

    mySqlUpdate.Parameters.Add("@id", SqlDbType.Int).Value = Convert.ToInt32(lblId.Text);
    con.Open();
    mySqlUpdate.ExecuteNonQuery();
    con.Close();
    gvEdit.EditIndex = -1;
    BindGrid();


    }
    protected void gvEdit_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    string connection = "Data Source=dell;Initial Catalog=master;Integrated Security=true";
    SqlConnection con = new SqlConnection(connection);
    SqlCommand mySqlDelete = new SqlCommand("update employees set isactive=0 where id=@id", con);
    Label lblId = (Label)gvEdit.Rows[e.RowIndex].Cells[1].FindControl("lblId");
    mySqlDelete.Parameters.Add("@id", SqlDbType.Int).Value = lblId.Text;
    con.Open();
    mySqlDelete.ExecuteNonQuery();
    con.Close();
    gvEdit.EditIndex = -1;
    BindGrid();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
    area.Visible = true;
    btnAddNew.Visible = false;
    txtEmp_Name.Text = "";
    txtEmp_Notes.Text = "";
    txtEmp_Name.Focus();

    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {

    string conn = "Data Source=dell;Initial Catalog=master;Integrated Security=true";
    SqlConnection con = new SqlConnection(conn);
    string myquery = "Insert into employees(name,notes,isactive) values('"+txtEmp_Name.Text+"','"+txtEmp_Notes.Text+"',1)";
    con.Open();
    SqlCommand cmd = new SqlCommand(myquery, con);
    cmd.ExecuteNonQuery();
    con.Close();
    BindGrid();
    area.Visible = false;
    btnAddNew.Visible = true;
    }


    Sourc epage code:

    <asp:GridView ID="gvEdit" runat="server" AutoGenerateColumns="False"
    onrowcancelingedit="gvEdit_RowCancelingEdit"
    onrowediting="gvEdit_RowEditing" onrowupdating="gvEdit_RowUpdating"
    AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
    onrowdeleting="gvEdit_RowDeleting">
    <Columns>
    <asp:TemplateField HeaderText="ID">
    <ItemTemplate>
    <asp:Label ID="lblId" runat="server" Text='<%# Eval("id") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Category Name">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Notes">
    <ItemTemplate>
    <asp:Label ID="lblNotes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtNotes" runat="server" Text='<%# Eval("notes") %>'></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>

    </Columns>
    </asp:GridView>

    Regards,
    Sugandha
    Microsoft Certified Technology Specialist
    MY Blog..


This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.