Saturday, March 26, 2016

DataBinding DataSets: Directly vs BindingSource

I’ve noticed some confusion over a blog post that I wrote many years ago. It can be found here: Fun With DataSets The post is about the various versions of DataRows (Proposed vs Current) and how a DataTable databound to a Control can sometimes not correctly report that DataSet.HasChanges() is true. The post was about how to get around that problem.

I was originally just going to add an Update to that post, for clarification, but decided that a new post would be better. (I will Update that old post, but only to add a link to this new one).

The clarification I want to make is that this appears to be an issue only if you DataBind directly to your DataTable, like this:

myDataGridView.DataSource = ds.Tables[0];  

However, I have not yet seen this happen when using a BindingSource, like this:

BindingSource bs = new BindingSource();
bs.DataSource = ds.Tables[0];  
myDataGridView.DataSource = bs;

To still play it safe, just in case, you could do the following (instead of the more “expensive” solution I suggested in my previous Fun With DataSets post):

// assuming the BindingSource is called "bs", and the DataSet is "ds"
bs.EndEdit();
if (ds.HasChanges())
{
    // process your DataSet changes
}

Why did I write that old post (back in 2009), when BindingSource had been introduced more than 3 years earlier? It had been new to .NET 2.0 and Visual Studio 2005, released in January 2006. One would assume that by 2009, developers would be using BindingSource all the time and would never have a reason to have to worry about this.  A little history of my background will explain this …

I started working with .NET 1.0 in April 2002 (it had been released only a month earlier). We had just begun a new startup company with plans to develop a new state-of-the-art .NET application. Along the way, we started developing our own “Framework” that would be used throughout our application. I was heavily involved with the “Framework” development. Unfortunately, 1.0 was a little buggy, so we were all glad when 1.1 came out a year later (with Visual Studio 2003). But, even 1.1 had its issues, one being the subject of these two posts. The CommitProposedChanges() method in the first post was born out of these issues.

Now, cut to 3 years later and the release of .NET 2.0 … well, by then I had written a full-blown “Framework” that had DataBinding “built-in” to a lot of the base control classes, not to mention a lot of new developers working on various applications who were used to using the “Framework” the way it was. I probably could have refactored the “Framework” classes to incorporate the use of BindingSource, but I’d have to be damn sure that it was backward compatible so as not to break existing code.  Nope … if it ain’t broke, don’t fix it! 

Now, judging from a lot of recent questions I’ve seen on various Forums, there are still plenty of people doing DataBinding the “old” way. So, even my old posts are still relevant! Gotta keep ‘em all happy!!  =0)