65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.43/5 (5 votes)

Jan 31, 2010

CPOL
viewsIcon

21502

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...

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 = "";