Solved

Bulk update from Gridview to LINQ

Posted on 2014-01-26
8
60 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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