C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » ASP.NET GridView »

Edit,Delete in Gridview


Posted Date: 04 Sep 2009    Resource Type: Code Snippets    Category: ASP.NET GridView
Author: HygeenaMember Level: Gold    
Rating: 1 out of 5Points: 15



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

Attachments

  • Gridview Edit,Delet (32205-4025-gridview_edit_delete.doc)


  • Responses

    Author: vikas kumar gupta    11 Sep 2009Member 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(); }


    Feedbacks      
    Popular Tags   What are tags ?   Search Tags  
    Sign In to add tags.
    Gridview Edit  .  Delete  .  

    Post Feedback


    This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
    You must Sign In to post a response.
    Next Resource: Code for binding Dropdownlist inside Gridview and saving data
    Previous Resource: DataGrid in Dropdown using wpf
    Return to Discussion Resource Index
    Post New Resource
    Category: ASP.NET GridView


    Post resources and earn money!
     
    More Resources



    dotNet Slackers

    About Us    Contact Us    Privacy Policy    Terms Of Use