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 1091647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:33:43+00:00 2026-05-16T23:33:43+00:00

I have a Datagrid connected to Datatable, which needs to load a very large

  • 0

I have a Datagrid connected to Datatable, which needs to load a very large amount of rows.

To speed things up, I load 10% of the rows, and display the form. Most of the time the user only needs those 10% (they are the most recent entries). In a background thread I load the remaining 90% of the rows into another datatable (SecondData). Then I merge both datatables:

FirstData.BeginLoadData()
FirstData.Merge(SecondData, False)
FirstData.EndLoadData()

This works fine, but the Merge operation takes a very long time. If I reverse the operation (merging SecondData with FirstData), it takes much less time. But then I have to re-assign an itemsource (SecondData) to the Datagrid, and the user looses the current scrolling position, selected row, etc.

I also tried adding the rows directly to FirstData from the background thread, and it appears to work just fine. But when I scroll the Datagrid after that, I get freezes, and “DataTable internal index is corrupted”, after that.

What would be the correct way of doing this?

  • 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-16T23:33:44+00:00Added an answer on May 16, 2026 at 11:33 pm

    Here is a somewhat hacked additional version that shows how to load a DataGrid when binding to a DataView using still BeginInvoke.
    The code still loads one row at a time into the DataGrid.
    You’ll need to modify as needed; I am loading from the AdventureWorks sample using the Loaded event.

    Here is how the ViewModel works:

    1. First load the columns using a SQL statement with a Where clause of 1=0
    2. Call Dispatcher.BeginInvoke to first load a subset of data
    3. Then call Dispatcher.BeginInvoke again to load the remaining data

    Here is the window:

    <Window x:Class="DatagridBackgroundWorker.Views.MainView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:WpfToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
        Loaded="Window_Loaded"
        Title="Main Window" Height="400" Width="800">
      <DockPanel>
        <Grid>
            <WpfToolkit:DataGrid  
                Grid.Column="1"
                SelectedItem="{Binding Path=SelectedGroup, Mode=TwoWay}"
                ItemsSource="{Binding Path=GridData, Mode=OneWay}" >
            </WpfToolkit:DataGrid>
        </Grid>
      </DockPanel>
    </Window>
    

    Here is the Window code-behind with the Loaded event:

    public partial class MainView : Window
    {
      ViewModels.MainViewModel _mvm = new MainViewModel();
    
      public MainView()
      {
         InitializeComponent();
         this.DataContext = _mvm;
      }
    
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
         Dispatcher d = this.Dispatcher;
         _mvm.LoadData(d);
      }
    }
    

    Here the ViewModel:

    public class MainViewModel : ViewModelBase
    {
      public MainViewModel()
      {
         // load the connection string from the configuration files
         _connectionString = ConfigurationManager.ConnectionStrings["AdventureWorks"].ConnectionString;
    
         using (SqlConnection conn = new SqlConnection(ConnectionString))
         {
            conn.Open();
    
            // load no data 1=0, but get the columns...
            string query =
               "SELECT [BusinessEntityID],[Name],[SalesPersonID],[Demographics],[rowguid],[ModifiedDate] FROM [Sales].[Store] Where 1=0";
            SqlCommand cmd = conn.CreateCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = query;
    
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(_ds);
         }
      }
    
      // only show grid data after button pressed...
      private DataSet _ds = new DataSet("MyDataSet");
      public DataView GridData
      {
         get
         {
            return _ds.Tables[0].DefaultView;
         }
      }
    
      private void AddRow(SqlDataReader reader)
      {
         DataRow row = _ds.Tables[0].NewRow();
         for (int i = 0; i < reader.FieldCount; i++)
         {
            row[i] = reader[i];
         }
         _ds.Tables[0].Rows.Add(row);
      }
    
      public void LoadData(Dispatcher dispatcher)
      {
         // Execute a delegate to load the first number on the UI thread, with a priority of Background.
         dispatcher.BeginInvoke(DispatcherPriority.Background, new LoadNumberDelegate(LoadNumber), dispatcher, true, 1);
      }
    
      // Declare a delegate to wrap the LoadNumber method
      private delegate void LoadNumberDelegate(Dispatcher dispatcher, bool first, int id); 
      private void LoadNumber(Dispatcher dispatcher, bool first, int id)
      {
         try
         {
            using (SqlConnection conn = new SqlConnection(ConnectionString))
            {
               conn.Open();
    
               // load first 10 rows...
               String query = string.Empty;
               if (first)
               {
                  // load first 10 rows
                  query =
                     "SELECT TOP 10 [BusinessEntityID],[Name],[SalesPersonID],[Demographics],[rowguid],[ModifiedDate] FROM [AdventureWorks2008].[Sales].[Store] ORDER By [BusinessEntityID]";
                  SqlCommand cmd = conn.CreateCommand();
                  cmd.CommandType = CommandType.Text;
                  cmd.CommandText = query;
                  int lastId = -1;
                  SqlDataReader reader = cmd.ExecuteReader();
                  if (reader != null)
                  {
                     if (reader.HasRows)
                     {
                        while (reader.Read())
                        {
                           lastId = (int)reader["BusinessEntityID"];
                           AddRow(reader);
                        }
                     }
                     reader.Close();
                  }
    
                  // Load the remaining, by executing this method recursively on 
                  // the dispatcher queue, with a priority of Background.
                  dispatcher.BeginInvoke(DispatcherPriority.Background,
                     new LoadNumberDelegate(LoadNumber), dispatcher, false, lastId);
               }
               else
               {
                  // load the remaining rows...
    
                  // SIMULATE DELAY....
                  Thread.Sleep(5000);
    
                  query = string.Format(
                        "SELECT [BusinessEntityID],[Name],[SalesPersonID],[Demographics],[rowguid],[ModifiedDate] FROM [Sales].[Store] Where [BusinessEntityID] > {0} ORDER By [BusinessEntityID]",
                        id);
                  SqlCommand cmd = conn.CreateCommand();
                  cmd.CommandType = CommandType.Text;
                  cmd.CommandText = query;
                  SqlDataReader reader = cmd.ExecuteReader();
                  if (reader != null)
                  {
                     if (reader.HasRows)
                     {
                        while (reader.Read())
                        {
                           AddRow(reader);
                        }
                     }
                     reader.Close();
                  }
               }
            }
         }
         catch (SqlException ex)
         {
         }
      }
    
      private string _connectionString = string.Empty;
      public string ConnectionString
      {
         get { return _connectionString; }
         set
         {
            _connectionString = value;
            OnPropertyChanged("ConnectionString");
         }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

( .NET 2.0, System.Windows.FormsDataGridView and DataTable ) I have 1 datagrid connected to 1
I have a datagrid which shows all the headers of the rows. I have
I have a datagrid which reads all songs from a directory and generate rows.
I have a datagrid view which is editable. I'm getting the value of a
I have a DataGrid bout to the DataView property of a typed DataTable however
I have a datagrid all set up, connected to an XMLStore. When the user
I have a collection ItemDemandCollection which is connected to a data grid. If a
I have a combobox which is connected to the database so I populate the
I have a WPF Form with a such a layout: <Grid> <DataGrid> ... </DataGrid>
i have datagrid and inside which i have a checkbox . now i want

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.