Click here to Skip to main content
15,889,556 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there !

Please assume I bind a LINQ query to DataSource property of a DataGridView object, now is it possible to submit users changes (like updates, deletes and inserts) to real database?
I used SubmitChanges() method of my DataContext object but it not work ! I mean user changes don't apply on database, and when you load the data again, user changes will discards!

(excuse me for my bad English)

Thanks for your attention!
Posted
Updated 22-Mar-11 6:10am
v2
Comments
Sandeep Mewara 22-Mar-11 12:06pm    
Elaborate more on what do you mean by 'it not work'?
Majid_grok 22-Mar-11 12:28pm    
Thanks for your advice !
Sandeep Mewara 22-Mar-11 12:34pm    
Do add the error if any.
Majid_grok 22-Mar-11 12:42pm    
There is not any error!
actually, if you set it's DataSource property to a DataTable object, you can simply apply changes to database by calling dataAdapter1.Update(datatable1) !! but is it possible to use LINQ query for it's DataSource property, and (by any way) apply all DataGridView changes to database?

1 solution

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.

C#
DataClassesDataContext dc = new DataClassesDataContext(); //Data context object
Product product = dc.Products.Single(name => name.ProductName == "Product1"); // get  a single product
product.ProductName = "Product2";// assign a value
dc.SubmitChanges();// submit the changes
GridView1.DataBind();// bind the gridview


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.

C#
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();
     }
 }
 
Share this answer
 
v3
Comments
Majid_grok 23-Mar-11 12:17pm    
Thanks AlbinAbel!
but I mean can it done automatically like when we use dataTable assigned to dataGridView?
however I know that it can be done manually with get tracking of changes on a dataGridView object (by it's events) !
Albin Abel 24-Mar-11 1:11am    
You may look at my improved answer
Majid_grok 27-Mar-11 7:10am    
Thanks for your attention dear AlbinAbel !
but I think you write the way for ASP.NET, isn't it?
I want to do it in C# applications...
Albin Abel 27-Mar-11 13:31pm    
For windows I think it needs to call the submit changes. I have added an example
Majid_grok 2-Apr-11 13:35pm    
Thanks a lot dear AlbinAbel !
You are right! I have test it and it worked properly...
I appreciate your attention!
God bless you.

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