How to do Edit, Update, Delete in a GridView

The following code sample shows how to do Edit, Update, Delete in a GridView


public partial class AllinGridView : System.Web.UI.Page
{
search_user sh = new search_user();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindall();
}
}
public void bindall()
{
DataSet ds = sh.show_all_details();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindall();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bindall();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bindall();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;
sh.get_userid = id;

TextBox tx1=(TextBox)(GridView1.Rows[GridView1.EditIndex].Cells[0].FindControl("TextBox1"));
TextBox tx2=(TextBox)(GridView1.Rows[GridView1.EditIndex].Cells[1].FindControl("TextBox2"));
TextBox tx3=(TextBox)(GridView1.Rows[GridView1.EditIndex].Cells[2].FindControl("TextBox3"));

sh.get_username = tx1.Text;
sh.get_location = tx2.Text;
sh.get_contactno = tx3.Text;
sh.update_search();

GridView1.EditIndex = -1;
bindall();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = (int)GridView1.DataKeys[e.RowIndex].Value;
sh.get_userid = id;
sh.delete_search();
bindall();
}
}


Comments

Author: Kapil Dhawan17 Jun 2008 Member Level: Gold   Points : 2

Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil

Author: Phani17 Jun 2008 Member Level: Silver   Points : 1


I also want to know how to do this at client side please try for that and post it. Thanku

Author: Rakesh Kumar23 Jun 2008 Member Level: Bronze   Points : 1

Very Nice code , m looking for update in grid view

Thanks

Author: Bunty27 Jun 2008 Member Level: Gold   Points : 2

Hi,

Very nice piece of code on GridView.

The code is easy to understand.

Thanks for sharing your knowledge.

Keep posting.

Thanks and Regards
S.S.Bajoria

Author: SharePoint Developer27 Jun 2008 Member Level: Silver   Points : 2

.cs file

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgrid();
}
// cmd.EndExecuteNonQuery();
}
public void fillgrid()
{
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Initial Catalog=MTR;UID=kushi;PWD=mtr");
con.Open();
SqlCommand cmd = new SqlCommand("select * from mtr", con);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
// cmd.ExecuteNonQuery();
da.Fill(ds, "a");
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;


fillgrid();

}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtuname = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
TextBox txtpwd = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
TextBox Uid = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
string uname = txtuname.Text.Trim().ToString();
string pwdt = txtpwd.Text.Trim().ToString();
string id = txtpwd.Text.Trim().ToString();
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Initial Catalog=MTR;UID=kushi;PWD=mtr");
con.Open();
string sqlcom="update mtr set Username='"+ uname +"',Pwd='"+ pwdt +"'where Uid='"+ id+ "'";
SqlCommand cmd = new SqlCommand(sqlcom, con);
cmd.ExecuteNonQuery();
con.Close();
fillgrid();
GridView1.EditIndex = -1;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{

}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
fillgrid();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
fillgrid();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
Label label1 = (Label)GridView1.Rows[e.RowIndex].FindControl("Label1");
string id = label1.Text.Trim().ToString();
SqlConnection con = new SqlConnection("Integrated Security=SSPI;Initial Catalog=MTR;UID=kushi;PWD=mtr");
con.Open();
string sqlcmd="delete mtr where Uid='1'";
SqlCommand cmd=new SqlCommand(sqlcmd,con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex=-1;
fillgrid();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

}
}


.aspx file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>





Untitled Page




 

AutoGenerateColumns="False" BackColor="White" BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Horizontal" Height="272px" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" OnPageIndexChanging="GridView1_PageIndexChanging" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnRowCancelingEdit="GridView1_RowCancelingEdit" Width="372px" OnRowDeleting="GridView1_RowDeleting" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" PageSize="1">








































if u want any dought then mail me

Guest Author: Rehan10 Jul 2013

what does following line of code mean?

search_user sh = new search_user();

please explain. Thanks



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: