Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3948146
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:20:23+00:00 2026-05-20T01:20:23+00:00

I am experiencing an issue setting the ItemsSource property of a DataGrid to the

  • 0

I am experiencing an issue setting the ItemsSource property of a DataGrid to the result of a LINQ query. The exact error is:

Cannot access a disposed object.
Object name: ‘DataContext accessed after Dispose.’.

Now, before you start snickering and whip off an answer regarding deferred query evaluation and disposing of your database contexts, know that I do understand all of that. I am calling ToList() on the result of the query and assigning that to the ItemsSource property. So, query executed, results read into memory, should be ok.

Yeah, not so much. At first I thought that there must be something about the property itself causing this, i.e., some weird data binding thing that I don’t know about (learning WPF and linq2Sql at the moment). After thinking some more about it, I still couldn’t explain the problem as the DataGrid shouldn’t have any notion of the DataContext, just the list (though returning a List from a test method and iterating through it later did not throw an exception).

It was the DataContext that was being accessed after its destruction, but that doesn’t help me understand either because I am calling ToList() specifically to execute the query before the using block exits. I can however solve the problem as shown below:

private void button1_Click( object sender, RoutedEventArgs e )
{
    using( NorthwindDataContext db = new NorthwindDataContext() )
    {
        db.DeferredLoadingEnabled = false;  // works, but why is it necessary at all?
        grid.ItemsSource = (from o in db.Orders
                            where o.CustomerID == "VINET"
                            select o).ToList();
    }
}

So yeah, I’d like to understand this behavior before moving on too far. Thanks in advance.

EDIT: xaml for the main window and DataGrid

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="495" Width="722">
    <Grid>
        <DataGrid AutoGenerateColumns="true" Height="403" HorizontalAlignment="Left" Margin="12,41,0,0" Name="grid" VerticalAlignment="Top" Width="676" />
        <Button Content="Do it" Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    </Grid>
</Window>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:20:24+00:00Added an answer on May 20, 2026 at 1:20 am

    Your code indicates that setting DeferredLoadingEnabled to false fixed this? If so I’d assume that your databinding was accessing properties of a related object, which, when accessed, was trying to fire off a new query to retrieve said object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently experiencing the issue mentioned here (and several other places): Subsonic 3 Linq
I am experiencing the well known issue where after setting a TreeNode's font to
I am experiencing an issue where, when a server error occurs in my web
I am experiencing an issue whereby I cannot completely redirect output from an executable.
I was experiencing an issue with a repository that would error with a 'No
I'm experiencing an issue when setting a minimum width for an area. The problem
I am experiencing an issue with v1 of Ninject and resetting the StandardKernel. I
Some reasons i am experiencing this issue on my website. When the website loads
from today I've been experiencing an issue with Google Maps resulting into not centering
I am experiencing a weird issue with PUT and POST ajax CORS requests in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.