Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Gridview Updating and Deleting Using LinqToSql.
Posted
Comments
girishkumarjha 21-Dec-11 0:11am    
Hi... this is the solution how to gridview update ,edit, insert into linqtosql....it works......

Implementing GridView Edit, Update and Delete:
Let’s start with implementing the edit and the update feature. First you need to add the ComandField buttons with will represent the edit and update link buttons.
You can always use designer to add the edit, update and cancel buttons.
<pre lang="c#">&lt;asp:commandfield ShowEditButton="True"&gt;
&lt;/asp:commandfield&gt;
&lt;asp:commandfield ShowDeleteButton="True"&gt;
&lt;/asp:commandfield&gt;
Next task is to get the GridView into edit mode. This is pretty simple and can be achieved by using the GridView_RowEditing event.
protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e)
{
gvProducts.EditIndex = e.NewEditIndex;
PopulateProducts();
}
Next, let’s check out the Update method. The code for the Update is implemented inside the GridView_RowUpdating event of the GridView control.
protected void gvProducts_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//write code for Editing
}
First I created a new instance of the NorthwindRepository. Then I get the primary key of the Product which is set as the DataKeyNames property in the GridView control. I get the edited text from the textbox. I get the Product instance from the repository using the productID. And then finally I called the SaveProduct method. You can also call SubmitChanges but in SaveProduct I am removing the Products from the application cache.
public void SaveProduct()
{
// remove the products from the cache



CacheRepository.Remove("Products");
base.SubmitChanges();
}
</pre>


In the image above you can see that the product name has been updated.
 
Share this answer
 
v2
Comments
Abhi KA 21-Dec-11 1:45am    
what to write in update
Abhi KA 21-Dec-11 1:45am    
any sample code i try but not working?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900