Solved

Bulk update from Gridview to LINQ

Posted on 2014-01-26
8
54 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
 

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now