Click here to Skip to main content
15,879,095 members
Articles / Programming Languages / C#
Article

CurrencyManager Collection - Dealing with SuspendBinding and ResumeBinding

Rate me:
Please Sign up or sign in to vote.
3.33/5 (3 votes)
21 Jun 2005CPOL1 min read 55.6K   30   7
Manage SuspendBindings and ResumeBindings for all CurrencyManagers in your app.

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.

C#
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

C#
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:

C#
// 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]));
C#
// 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]));
C#
// 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:

C#
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)


Written By
Choice Genetics US
United States United States
Seasoned IT Professional. Currently the IT Director for Choice Genetics, and also the world-wide manager for IT Projects related to R&D for Groupe Grimaud (our parent company).

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.

Comments and Discussions

 
GeneralThanks on the very good work Pin
Reader Man San3-Jun-08 7:40
professionalReader Man San3-Jun-08 7:40 
GeneralEvents in Collections Pin
Declan Brennan24-Jun-05 7:10
Declan Brennan24-Jun-05 7:10 
GeneralFormatting question Pin
Paul Brower21-Jun-05 3:07
Paul Brower21-Jun-05 3:07 
GeneralRe: Formatting question Pin
Marc Clifton21-Jun-05 4:05
mvaMarc Clifton21-Jun-05 4:05 
GeneralRe: Formatting question - italics Pin
Paul Brower21-Jun-05 5:06
Paul Brower21-Jun-05 5:06 
GeneralRe: Formatting question - italics Pin
Marc Clifton21-Jun-05 5:36
mvaMarc Clifton21-Jun-05 5:36 
GeneralRe: Formatting question - italics Pin
Marc Clifton21-Jun-05 5:38
mvaMarc Clifton21-Jun-05 5:38 

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

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