views:

55

answers:

1

Am I missing the point here, I don't see how this is any better than just assigning the Text property of a Label directly?

 //class variables
 Binding stateBinding;
 MyRowType rowForDataBinding;

 ChangeBoundData()
 {
  rowForDataBinding = GetNewRow();
  lblStatus.DataBindings.Remove(temp);
  temp = lblState.DataBindings.Add("Text", rowForDataBinding , "State");
 }
+2  A: 

If you are binding on a row-by-row basis, the amount of benefit is negligible. However, let's say you want to bind a database table to a data grid. Suddenly, the one line of binding code minimizes a ton of effort.

I've gone back and forth on the appropriateness of data-binding, but from what I can tell, the primary benefit is code reduction. The less code you have to write, the less code you have to fix.

j0rd4n
I started doing it by binding to a DataGridView; and even more than the code reduction with large tables it's ridiculously faster. With a single row though it seems that I'm actually ending up with more code since I need a class variable to store the binding and 2 lines of code each time it changes (3 lines) vs just doing lblState.Text = rowForDataBinding.State; (1 line).
Dan Neely
@Dan - Yes, you'll see a major performance increase. These days, I would try to databind first and only if there was a need for very specific "override" behavior would I manually load the grid.
j0rd4n
@j0rd4n I'm fully sold on doing binding with grids. I'm trying to find out if/when binding to anything else is a reasonable decision.
Dan Neely
@Dan It depends on how far you are going to go with model-view based patterns. If you want to minimize the amount of UI-logic your code-behind contains, data binding can really help. In practice, however, I tend to still write a lot of UI code in my presentation tier and handle all application logic in the controller/model tiers - mainly because that is what I'm used to. Grids and lists seem very natural for databinding. Other UI elements feel overkill (in my personal opinion).
j0rd4n