Click here to Skip to main content
Licence CPOL
First Posted 21 Jun 2005
Views 38,501
Bookmarked 29 times

CurrencyManager Collection - Dealing with SuspendBinding and ResumeBinding

By | 21 Jun 2005 | Article
Manage SuspendBindings and ResumeBindings for all CurrencyManagers in your app.
 
Part of The SQL Zone sponsored by
See Also

Introduction

Due to the large number of records I deal with in my applications, the ability to suspend DataBindings is key to performance. I have often spent too much time making sure I am suspending DataBindings for all areas of my app. It is common to have a dozen or more active screens with a Binding Context.

One common reason for suspending binding is to be able to make changes to multiple fields before validation occurs. Those cases can be handled on a 'one on one' basis as business logic dictates.

There are a myriad of events that can fire off on bound controls when you're populating a dataset, both on grids and individual controls. I have found a significant performance increase when I suspend Bindings as I'm loading data, or when I'm clearing data.

CollectionWithEvents

This is a class that inherits from CollectionBase and provides events for the collection. The CurrencyManagerCollection class inherits from CollectionWithEvents.

using System;
using System.Collections;
namespace Assemblies.UserInterfaceArchitecture
{
  // summary
  // Base class for managing strongly typed collections
  // summary
  public class CollectionWithEvents : CollectionBase
  {
    // Declare the event signatures
    public delegate void CollectionClear();
    public delegate void CollectionChange(int index, object value);
    // Collection change events
    public event CollectionClear Clearing;
    public event CollectionClear Cleared;
    public event CollectionChange Inserting;
    public event CollectionChange Inserted;
    public event CollectionChange Removing;
    public event CollectionChange Removed;
    // Overrides for generating events
    protected override void OnClear()
    {
      if (Clearing != null) Clearing();
    }
    protected override void OnClearComplete()
    {
      if (Cleared != null) Cleared();
    }
    protected override void OnInsert(int index, object value)
    {
      if (Inserting != null) Inserting(index, value);
    }
    protected override void OnInsertComplete(int index, object value)
    {
      if (Inserted != null) Inserted(index, value);
    }
    protected override void OnRemove(int index, object value)
    {
      if (Removing != null) Removing(index, value);
    }
    protected override void OnRemoveComplete(int index, object value)
    {
      if (Removed != null) Removed(index, value);
    }
  }
}

CurrencyManagerCollection

using System;
using System.Data ;
using System.Windows.Forms ;

Assemblies.UserInterfaceArchitecture
{
    /// <SUMMARY>
    /// Utility for handling a collection of currencymanagers.
    /// If we add all of the currency managers to this collection, 
    /// we can easily 'SuspendBinding' or 'ResumeBinding' on all 
    ///currency managers at the same time, 
    ///when we're changing the underlying lists, 
    ///through searching or clearing, or whatever
    /// </SUMMARY>
    public class CurrencyManagerCollection : CollectionWithEvents
    {
        /// <SUMMARY>
        /// suspend bindings for all currency managers in collection
        /// </SUMMARY>
        public void SuspendBindings()
        {
            foreach (CurrencyManager cm in this)
            {
                cm.SuspendBinding() ;
            }
        }

        /// <SUMMARY>
        /// resume bindings for all currency managers in collection
        /// </SUMMARY>
        public void ResumeBindings()
        {
            foreach (CurrencyManager cm in this)
            {
                cm.ResumeBinding() ;
            }
        }

        public int Add(CurrencyManager value)
        {
            //make sure we're not trying to add the same object
            if (Contains(value) == false)
            {
                return base.List.Add(value as object);
            }
            return 0 ;
        }

        public void Remove(CurrencyManager value)
        {
            base.List.Remove(value as object);
        }

        public void Insert(int index, CurrencyManager value)
        {
            base.List.Insert(index, value as object);
        }

        public bool Contains(CurrencyManager value)
        {
            return base.List.Contains(value as object);
        }

        public CurrencyManager this[int index]
        {
            get { return (base.List[index] as CurrencyManager); }
        }
    }
}

Usage

As the events are firing off in your controls/forms that set up DataBindings, all you need to do is add the CurrencyManger to the collection. Some examples of this are shown below:

// Suppose we have a 'mainClass' that 
// has a CurrencyManagerCollection class called CurrencyManagers
// this will add the currency manager for the current form/control 
// for the binding context of the 'TheDataView'
mainClass.CurrencyManagers.Add(((CurrencyManager)
          this.BindingContext[TheDataView]));
// this will add the currency manager for the current form/control 
// for the binding context of the 'TheDataTable'
mainClass.CurrencyManagers.Add(((CurrencyManager)
          this.BindingContext[TheDataTable]));
// if you already have a CurrencyManager set up, you can just add it.
// if your CurrencyManager was called 'currencyManger' then:
mainClass.CurrencyManagers.Add(currencyManager);

As you're preparing to load data into your DataSet (hopefully it will be a strongly-typed DataSet!), you can to the following:

mainClass.CurrencyManagers.SuspendBindings() ;
//your code here that populates the DataSet
mainClass.CurrencyManagers.ResumeBindings() ;

In some cases I don't want to suspend bindings on everything in the application, but when I do, I know that all my databound controls will be suspended!

License

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

About the Author

Paul Brower

Other
Newsham Choice Genetics
United States United States

Member

I've been writing software for about 19 years, and I'm 38, so do the math.
 
I've spent about half my career as a contractor. I've lived all over the place, but currently reside near St. Louis, Missouri. Moved out here from Roseville, California in Feb-2005. No regrets yet.
 
Over the recent years I've written software for:
- Disposing of radioactive and toxic waste.
- Disposing of surplus inventory during the Decommission of McClellan Air Force Base.
- Facilitating genetic improvement for Swine Breeding.
- Managing children placed in State custody.
- Dealing with commercial trucking delivery schedules.
- Tracking high resolution images from archeological digs.
- Project Management for the roofing industry.
- Processing engines for credit card transactions.
 
... just to name a few! Isn't consulting great!
 
...Update January 2009: I've joined Newsham Choice Genetics (http://www.newsham.com) as the Director of Information Technology.
 
personal website: http://www.blurious.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks on the very good work PinmemberReader Man San7:40 3 Jun '08  
GeneralEvents in Collections PinmemberDeclan Brennan7:10 24 Jun '05  
GeneralFormatting question PinmemberPaul Brower3:07 21 Jun '05  
GeneralRe: Formatting question PinprotectorMarc Clifton4:05 21 Jun '05  
GeneralRe: Formatting question - italics PinmemberPaul Brower5:06 21 Jun '05  
GeneralRe: Formatting question - italics PinprotectorMarc Clifton5:36 21 Jun '05  
GeneralRe: Formatting question - italics PinprotectorMarc Clifton5:38 21 Jun '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 21 Jun 2005
Article Copyright 2005 by Paul Brower
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid