Untitled document
1. Take Gridview on your webpage.
2. Right Click on it , and choose "Add new Column" from that dialogBox select commandField from the combobox, which will allow you to add "select, edit/update and delete" buttons on your GridView.
3.Now implement following events of Gridview
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
.CS Code
static string ConnString ="ConnectionString";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Code to bind gridview
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
//Code to bind gridview
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = int.Parse(((Label)GridView1.Rows[e.RowIndex].FindControl("lblid")).Text);//ID
string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtname")).Text;
SqlConnection connect = new SqlConnection(ConnString);
connect.Open();
string q = "Update people set name=@name where id=@id";
SqlCommand comm = new SqlCommand(q, connect);
comm.Parameters.AddWithValue("name", name);
comm.Parameters.AddWithValue("id", id);
comm.ExecuteNonQuery();
connect.Close();
GridView1.EditIndex = -1;
//Code to bind gridview
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}