views:

149

answers:

4

I just started working on a small team of .NET programmers about a month ago and recently got in a discussion with our team lead regarding why we don't use databinding at all in our code. Every time we work with a data grid, we iterate through a data table and populate the grid row by row; the code usually looks something like this:

Dim dt as DataTable = FuncLib.GetData("spGetTheData ...")
Dim i As Integer

For i = 0 To dt.Rows.Length - 1 '(not sure why we do not use a for each here)'
  gridRow = grid.Rows.Add()
  gridRow(constantProductID).Value = dt("ProductID").Value
  gridRow(constantProductDesc).Value = dt("ProductDescription").Value
Next

'(I am probably missing something in the code, but that is basically it)'

Our team lead was saying that he got burned using data binding when working with Sheridan Grid controls, VB6, and ADO recordsets back in the nineties. He's not sure what the exact problem was, but he remembers that binding didn't work as expected and caused him some major problems. Since then, they haven't trusted data binding and load the data for all their controls by hand.

The reason the conversation even came up was because I found data binding to be very simple and really liked separating the data presentation (in this case, the data grid) from the in-memory data source (in this case, the data table). "Loading" the data row by row into the grid seemed to break this distinction. I also observed that with the advent of XAML in WPF and Silverlight, data-binding seems like a must-have in order to be able to cleanly wire up a designer's XAML code with your data.

When should I be cautious of using data-binding in .NET?

+7  A: 

You should be cautious when using anything you don't understand fully.

I'll confess to not understanding data-binding very well, but I still think your team lead's position is a little extreme and reactionary.

My rule of thumb would be; don't rely on something till you understand it. Once you understand it, you don't need my (or anyone elses) rules of thumb.

:)

Some links related to caveats using data-binding:

http://travisgosselin.com/blog/?p=46

http://stackoverflow.com/questions/450373/databinding-in-c-and-net

John Weldon
great advice :-)
Matt Briggs
I completely agree. That's why I want to understand better what's going on with data-binding. To me, I would rather fully understand databinding in .NET and *then* choose not to use it rather than avoid it entirely.
Ben McCormack
I don't know that you need to fully understand a construct before using it (insert car/engine analogy here). But you should understand when and when not to use it.
BenV
I agree, your ability to *accurately* discern when and when not to use a construct depend on *how well* you understand the construct.
John Weldon
+1  A: 

As everything else, you can use it properly, or improperly. DataBinding can be very powerful at times, but like mentioned before, you must know how to use it correctly.

Generally, there are two types of binding in .NET (and its tools): automatic and manual. Automatic binding is when VS creates everything for you, and you drag-and-drop. Don't do this. EVER. Only exception for this would be these new additions to Silverlight 4 in VS2010. It is the only automatic binding done properly.

On the other hand, manual databinding using DataSets as Unit of Works, CurrencyManagers and other stuff, can be extremely useful (talking about .NET 2.0 WinForms here, right? ).

Sapphire
Yeah, we are using .NET 2.0 WinForms. I'm wondering if you might expand on what you mean by "manual databinding...and other stuff, can be extremely useful." Can you offer an example? That would be really helpful.
Ben McCormack
Well, by manual databinding I mean binding directly to controls in code, using DataSource and other properties. Check this out: http://www.akadia.com/services/dotnet_databinding.html
Sapphire
Just to add, it doesn't matter do you bind to DataTables or custom POCO objects, if you prefer them. Binding works either way. I used it many times to build pretty cool data-manipulation forms.
Sapphire
+2  A: 

http://www.knowdotnet.com/articles/differences.html

things have changed dramatically FOR THE BETTER in .Net

Data Binding vs Manual Population

VB6 programmers have stayed away from the ADO data control and bound controls because of some performance reasons. I am making the move towards VB.NET and have the feeling that bound controls are not so evil after all.

BenV
+3  A: 

If all you are doing is displaying data, in my opinion, there is no reason NOT to use databinding.

I too come from the VB6 world, where databinding bit us again and again, so we had standards and workarounds in order not to use it.

Enter .NET, where databinding is as flexible as you need it to be. I have come to really appreciate the power that binding has now.

You are writing much unnecessary code by rejecting the built in capabilities.

You can even bind on-screen objects such as textboxes to properties in classes. Check out this link for an overview of winforms databinding. http://msdn.microsoft.com/en-us/library/ef2xyb33(v=VS.100).aspx

Jeremy
@Jeremy thanks for your input and for linking to MSDN. I suppose that should have been an obvious go-to point, but I didn't even think about it. It turns out there's a wealth of information there!
Ben McCormack