views:

1302

answers:

3

I am pretty new to C# and .NET and I'm strugling a little with the whole concept of databinding. What I am asking for is a quick rundown of the concept, or even better, point me towards sources on the net (preferred) or in print that can help me get my head around the idea.

EDIT:

I do my development in vs2008 and we are using winforms

+11  A: 

Well, what architecture are you using? winforms? asp.net? wpf?

The high level is that if you have objects such as:

public class Person {
    public string Name {get;set;}
    public DateTime DateOfBirth {get;set;}
}

Then you can get the binding framework to do all the hard work, and you just say what you want bound - for example (winforms):

txtName.DataBindings.Add("Text", person, "Name");

This sets the textbox's Text property based on the person's Name, and can update the person's Name when the user changes the text.

Multi-record binding is more complex, and is based on IList in winforms/wpf, and IEunmerable in ASP.NET; this allows you to bind multiple records (for example into a grid). If the list offers extra features (sorting, filtering etc, via IBindingList, IBindingListView, etc), then more functionality might be available.

Binding also allows "observer" usage - i.e. change notification: if you indirectly change the person's Name, then the textbox gets automatically updated. This relies on events - either of the form public event EventHandler NameChanged;, or (more commonly now) via the INotifyPropertyChanged event (allowing one event to notify for multiple properties).

Some lists (such as BindingList<T>, DataView) have similar notification loops.

Marc Gravell
I edited original post to answer your question :)
Sakkle
+2  A: 

The concept of databinding is quite simple; It allows you to 'bind' the data that is contained in an object to a visual control. That control 'displays' your data. When the user changes the value that is displayed by the control, the changes are automatically persisted to the underlying object. Vice versa, when someone changes the data in the object, the control can display the newest value.

http://msdn.microsoft.com/en-us/library/ms752347.aspx http://www.akadia.com/services/dotnet_databinding.html

Frederik Gheysels
+1  A: 

If you have the flexibility to do so, I would suggest using WPF if you can (there may be legacy requirements, or programmer familiarity which prevent you from doing so).

But the WPF databinding support in default controls is pervasive and consistent, the WinForms data binding support, not so much (TreeView/ListView, I'm looking at you).