You must Sign In to post a response.

How to edit update delete in gridview

Hi!


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

Thanks

Kani


Comments

Author: Anil Kumar Pandey01 Nov 2011 Member Level: Diamond   Points : 0

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

Author: Paritosh Mohapatra01 Nov 2011 Member Level: Gold   Points : 4

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
------


























































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();
}

Author: mandarapuamala02 Nov 2011 Member Level: Silver   Points : 2

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



<script runat="server">

protected void Page_Load(object sender, EventArgs e)
{

}
</script>



Untitled Page







>

















<%--














--%>























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();
}

}


Author: sugandha02 Nov 2011 Member Level: Gold   Points : 4

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:

onrowcancelingedit="gvEdit_RowCancelingEdit"
onrowediting="gvEdit_RowEditing" onrowupdating="gvEdit_RowUpdating"
AutoGenerateDeleteButton="True" AutoGenerateEditButton="True"
onrowdeleting="gvEdit_RowDeleting">