Click here to Skip to main content

Achieve performance while updating a datatable bound to a bindingsource or datagridview

Datatable in ADO.NET is highly useful to do in memory data related operations. But it is very painful to update data when it is bound to a datagridview or bindingsource.While I was developing an application in .NET with C#, I came across this problem in which I had to modify a data table of...
Sign Up to vote bad good
Add a reason or comment to your vote: x
Votes of 3 or less require a comment
See more: C#.NETADO.NET
Datatable in ADO.NET is highly useful to do in memory data related operations. But it is very painful to update data when it is bound to a datagridview or bindingsource.
 
While I was developing an application in .NET with C#, I came across this problem in which I had to modify a data table of 100000 records. It took lots of time to update the entire table. I Googled how best I could to come out of this problem. Everywhere I got suggestions to set the below mentioned properties before and after updation:
 
 BindingSource bs = new BindingSource();
DataTable dt = new DataTable();
 
bs.DataSource = dt;
bs.SuspendBinding();
bs.RaiseListChangedEvents = false;
dt.BeginLoadData();
            
//== some modification on data table

dt.EndLoadData();
bs.RaiseListChangedEvents = true;
bs.ResumeBinding();
bs.ResetBindings(true);

The above code snippet performed OK compared to simple updation of datatable without setting properties. Still I wanted to achieve more performance while modifying datatable.
Upon investigation, I could achieve it by setting the filter property of binding source.

  
BindingSource bs = new BindingSource();
DataTable dt = new DataTable();
 
bs.DataSource = dt;
bs.SuspendBinding();
bs.RaiseListChangedEvents = false; 
bs.Filter = "1=0"; 
dt.BeginLoadData(); 
        
//== some modification on data table

dt.EndLoadData();
bs.RaiseListChangedEvents = true;
bs.Filter = "";
Posted 31 Jan '10
Edited 15 Feb '10


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Your Filters
Interested
Ignored
     
  1. SAKryukov (1,140)
  2. OriginalGriff (1,065)
  3. Abhinav S (460)
  4. thatraja (455)
  1. SAKryukov (9,034)
  2. Christian Graus (5,771)
  3. OriginalGriff (4,388)
  4. Abhinav S (4,280)
  5. thatraja (4,230)

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
-- There are no messages in this forum --

Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 16 Feb 2010
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid