Solved

Bulk update from Gridview to LINQ

Posted on 2014-01-26
8
49 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
I tested the code in my last comment and that solved the problem.
0
 

Author Closing Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

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.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

8 Experts available now in Live!

Get 1:1 Help Now