You can do in LinQ. You can even submit multiple changes at once.
From the grid view event get the key for the row, which can be used to query the linQ object.
For example assume you have Products table in your dataContext class.
1)Instantiate it.
2) Get Product LinQ object you want.
3) Assign the changes
4) then call SubmitChanges() function.
5) Don't forgot to Bind the datagrid view.
DataClassesDataContext dc = new DataClassesDataContext();
Product product = dc.Products.Single(name => name.ProductName == "Product1");
product.ProductName = "Product2";
dc.SubmitChanges();
GridView1.DataBind();
Improved Answer
----------------------
As per your comment, Yes it is possible automatically.
Once you created the datacontext classes
1) Add/ Drag a linQ datasource to your page. Configure it by selecting the datacontext.
It will show options like
Enable Insert, Enable Update, Enable Delete
. Check those boxes.
2) Add the data grid view and assign the datasource to the linQ datasource. The gridview also shows options like the above enables. Check those values.
At runtime there will be edit and delete link. Edit will populate an edit template. Once done click the update link it will be automatically update to the database.
No code need to be written to manually handle updates and deletes.
Improved Answer2
-------------------------
For windows application I think we need to explicitly call the
Submit Changes
function. As an example add a datagrid view, a binding source and a button to the form. Set the
datagrid's datasource to the binding source
. Datagrid's allow edit should be true. Binding sources allowables set true and autogenerate member also true. Assume the data context has a Product object which mapped to products table. The datacontext object need to be maintain as a global variable to the form in order to submit the changes. Follow the code below.
public partial class Form1 : Form
{
DataClasses1DataContext dc;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dc = new DataClasses1DataContext();
dataGridView1.AutoGenerateColumns = true;
bindingSource1.DataSource = dc;
bindingSource1.DataMember = "Products";
}
private void button1_Click(object sender, EventArgs e)
{
bindingSource1.EndEdit();
dc.SubmitChanges();
}
}