Solved

Bulk update from Gridview to LINQ

Posted on 2014-01-26
8
55 Views
Last Modified: 2016-02-10
I have a gridview, which I'm populating via linq, like so:

DataClassesDataContext dc = new DataClassesDataContext();
var EGxE = from iterVar in dc.ExerciseGroup_X_Exercises
 where iterVar.ExerciseGroupId == 1
 select new
 {
	 iterVar.Id,
	 iterVar.ExerciseId,
	 iterVar.ImpressionId,
	 iterVar.Note
 };

Open in new window


After the user makes changes to a dropdownlist (for ImpressionId) and a textbox (for Note), I want to iterate through the GridView and update its changes to LINQ.

To iterate through the GridView, I know to do this:
foreach (GridViewRow row in GridView1.Rows)
{
...
}

Open in new window

but I'm drawing a blank on how to efficiently (bulk?) update the datacontext via LINQ.
0
Comment
Question by:cdakz
  • 5
  • 3
8 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39813018
If you are binding to the LINQ output, then the GridView should update the entity object in the sequence.  You shouldn't have to manually loop through GridView rows to achieve that.

I would create a single instance of the data context, so that it can take advantage of caching, and lazy loading.
0
 

Author Comment

by:cdakz
ID: 39813241
So Gridview will automatically perform the update (even if I'm not using a LinqDataSource control)?
 
Part of the reason I ask is that when I looked at the Gridview in Layout mode, and click it's "Gridview Tasks' arrow, it does NOT give me a choice of enabling Edit. I assume this is because I haven't declaratively set a DataSource for the GridView.

I've attached a screenshot of what I'm talking about.
GridViewTaskMenu.png
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39813316
I assume that you are using the Entity Framework data context, and if that is true, then you can verify the changes after editing in the GridView by looking at the DbChangeTracker.Entries list.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:cdakz
ID: 39814110
Nope, using LINQ to SQL.

Also, I don't want to update the database as a GridRow has been changed, but rather allow the user to make changes to some/all of the rows, and then when done click a Save button that commits all the changes from the GridView back to the database.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39815007
For LINQ-to-SQL, that would be DataContext.GetChangeSet.  I wasn't suggesting saving after an update.  You can make as many updates to the grid, and when you click save, the change set should have all the changes that you have made during the session.
0
 

Assisted Solution

by:cdakz
cdakz earned 0 total points
ID: 39815752
You're past me. I need assistance on performing the updates.

I'm thinking at this point that what I should do is, for each gridview row, perform a NEW LINQ query against the existing EGxE collection, and then perform the update that way. Something like:

      var EGxE_Single = (from updateVar in EGxE
                         where updateVar.Id == Id
                         select updateVar).First();
EGxE_Single.Note = "These are notes...";

Open in new window


Then after iterating through all Gridview Rows, perform a dc.SubmitChanges();

That should propagate back from EGxE_Single to EGxE, and then to the original database shouldn't it?
0
 

Author Comment

by:cdakz
ID: 39816225
I tested the code in my last comment and that solved the problem.
0
 

Author Closing Comment

by:cdakz
ID: 41380987
I answered my own question through research and testing. Bob's answer will help with EF implementations, down the line though.
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question