Solved

Bulk update from Gridview to LINQ

Posted on 2014-01-26
8
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

734 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