This snippet is for editing and deleting data within gridview
Editing and deleting in gridview is quiet simple compared to datagrid.First we need to create template for fields.For eg: we have three fields in gridview sid,sname,mark.So we need to create three templates.Gridview->properties->columns
After that drag label to itemtempalte and textbox to edititem template. Gridview->edittemplate->coulmn0.For column 0 we need label in itemtemplate field only.We are editing the details based on sid.for remaing name and mark add label in itemtemplate field and textbox in editItemtemplate field.
Next we are binding data by using EVal .So the design part should look like this
Next we are binding data to label and text box using EVal( To see coding ,find attachment)
Code Behind
Code for binding data in gridview
void fill_grid() { con.Open(); sql = "select * from student"; cmd.Connection = con; cmd.CommandText = sql; SqlDataAdapter adp = new SqlDataAdapter(); DataSet ds = new DataSet(); adp.SelectCommand = cmd; adp.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); con.Close(); }
For gridview edit,we need to set 2 events.Row editing and Row updating Row editing Event
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e) { GridView1.EditIndex = e.NewEditIndex; fill_grid(); }
Row Updating Event
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { string sid; string name; string mark; Label sidd = (Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("lblsid"); TextBox tname = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtsname"); TextBox tmark = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("txtmark"); sid = sidd.Text; name = tname.Text; mark = tmark.Text; con.Open(); sql = "update student set name='" + name.ToString() + "',mark=" + mark.ToString() + " where sid=" + sid + ""; cmd.Connection = con; cmd.CommandText = sql; cmd.ExecuteNonQuery(); con.Close(); GridView1.EditIndex = -1; fill_grid(); }
Row Deleting Event
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) { string sid; Label sidd = (Label)GridView1.Rows[e.RowIndex].Cells[0].FindControl("lblsid"); sid = sidd.Text; con.Open(); sql = "delete from student where sid=" + sid + ""; cmd.Connection = con; cmd.CommandText = sql; cmd.ExecuteNonQuery(); con.Close(); fill_grid(); }
Row Cancelling Event
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GridView1.EditIndex = -1; fill_grid(); }
Hope this snippet will help you.I am also attaching code with screen shots
AttachmentsGridview Edit,Delet (32205-4025-gridview_edit_delete.doc)
|
| Author: vikas kumar gupta 11 Sep 2009 | Member Level: Silver Points : 2 |
void fill_grid() { con.Open(); sql = "select * from student"; cmd.Connection = con; cmd.CommandText = sql; SqlDataAdapter adp = new SqlDataAdapter(); DataSet ds = new DataSet(); adp.SelectCommand = cmd; adp.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); con.Close(); }
|