Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

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!

This entry was posted in .NET. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

7 Responses to DataBinding in .NET 2.0

  1. Jay Kimble says:

    (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…

  2. Gabe says:

    The DataConnector is deprecated in 2.0.

  3. Greg Robinson says:

    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.

  4. Bil Simser says:

    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.

  5. Darrell says:

    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!

  6. Mike Shaffer says:

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

  7. Bil Simser says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>