Darrell Norton's Blog [MVP]

Sponsors

The Lounge

News

  • Darrell Norton pic

    MVP logo

    View Darrell Norton's profile on LinkedIn

    Currently Reading:

    weewar.com

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
DataBinding in .NET 2.0

The databinding support for the .NET Framework version 1.x was the primary reason for using a DataSet. Using it made life much easier, and trying to develop non-DataSet components was correspondingly harder.

But that’s all changed in .NET 2.0. You can bind to a business object very simply. First, you need to add a DataConnector for your business object to your form (or user control). Then, the following three lines of code (this is with Windows Forms, it may be slightly different for ASP.NET 2.0):

// Business Object DataConnector
this.BusinessObjectDataConnector = 
  new System.Windows.Forms.DataConnector(this.components);

this.BusinessObjectDataConnector.DataSource = 
  Services.BusinessObject.BusinessObjectDataClass.Load();

// DataBindings
this.textbox1.DataBindings.Add(
  new Binding("Text", this.BusinessObjectDataConnector, 
  "SomeProperty", true) );

That’s it! Now if you change the value of textbox1, it updates the BusinessObject’s SomeProperty value for you!


Posted 04-04-2005 9:08 AM by Darrell Norton
Filed under:

[Advertisement]

Comments

andrew connell wrote DataBinding in .NET 2.0
on 04-04-2005 10:15 AM
Bil Simser wrote re: DataBinding in .NET 2.0
on 04-04-2005 11:42 AM
Very nice. I tried this out with a small spike and it works great. I spent more than a second and created a DTO to hold my information as I wouldn't bind a business object directly to a UI but this does provide a nice UI mapping.

With a bit of reflection, you could even do it automatically without having to know about what properties the BusinessObject had (or dynamically create the form controls based on the public properties in the object).
Mike Shaffer wrote re: DataBinding in .NET 2.0
on 04-04-2005 12:44 PM
This is what makes .Net 2.0 so great (IMHO). Our organization was on track for a great WinForms conversion and binding was one of the great advantages that .Net has over everything else (Click Once being the other...)

But the sad story here is that a new head of Development came, the type that feels all Microsoft is evil and all Java is miraculous. So this WinForms fanatic is having to write SWT code...no binding, no easy deploy strategy, no nothing..... And we're a Windows only outfit!!! Go figure. A sane decision altered by zealotry....
Darrell Norton wrote re: DataBinding in .NET 2.0
on 04-04-2005 7:04 PM
Bil - yes I was thinking about using Reflection to make it work automatically. On the other part, can you post your DTO sample code?? I'm really interested to see how you created that!
Bil Simser wrote re: DataBinding in .NET 2.0
on 04-05-2005 4:55 PM
Darrell,

I'll post a blog tommorow with a more extensive sample that includes the DTO and some stuff I've been playing with since you posted your sample.

Thanks.
Greg Robinson wrote re: DataBinding in .NET 2.0
on 04-06-2005 12:47 PM
You can bind a custom type's Properties to controls with ADO.NET now. I understand the post, just go confused when you stated "You can bind to a business object very simply". And yes, it is a challenge to bind without DataSets, though in some ways I wish we never used DataSets in our application.
Gabe wrote re: DataBinding in .NET 2.0
on 05-04-2005 1:16 PM
The DataConnector is deprecated in 2.0.
Jay Kimble wrote re: DataBinding in .NET 2.0
on 06-24-2005 11:25 AM
(Catching up on my blog reading believe it or not... wow, am I behind!)

Anyway, in Asp.Net 1.1, I regularly bind stuff like DataGrids to business objects... I don't normally use binding for other types of controls, but I think the problem of binding directly to a business object may be a WinForms limitation.

Actually if memory serves me right this is the one thing that Asp.Net has over WinForms...