DataBinding in .NET

Note: The Information in this article is outdated. Databinding uses generic lists today. tl;dr: NDO objects can be used with any Kind of data binding in .NET.

NDO supports .NET data binding. In .NET arbitrary classes can be bound to controls, the public properties of the classes are displayed and can be manipulated. The properties created by the AddAccessor function can be used for that purpose.

Let’s use the following class to take a closer look at DataBinding:

    public class Employee

    {

        string FirstName;

        string LastName;

        public Employee (string FirstName, string LastName)

        {

            this.FirstName = FirstName;

            this.LastName = LastName;

        }

        public string LastName

        {

            get { return LastName; }

            set { LastName = value; }

        }

        public string FirstName

        {

            get { return FirstName; }

            set { FirstName = value; }

        }

    }

Now you can create a DataGrid in a Windows form and bind objects to it:

private IList EmployeeList;

public void foo()

{

    EmployeeList = new ArrayList();

    EmployeeList.Add(new Employee("Mirko", "Matytschak");

    EmployeeList.Add(new Employee("John", "Doe");

    this.dataGrid1.DataSource = EmployeeList;

}

The Windows-Forms library displays a table with the Employee elements in the DataGrid. The library uses reflection to look for the public properties of the elements and generates corresponding columns.

If you want to enforce another selection or another sort direction of the columns using TableStyles and ColumStyles, you have to set the property MappingName in the TableStyles properties to the type of the collection, in this case "ArrayList".

Disadvantages of the Binding to ArrayLists

This simple solution has several drawbacks:

1.   If an empty list is connected to a control or a list gets empty during dynamic manipulation, an exception is thrown. This is because WindowForms derives the meta data directly from the element objects and no meta data is available in an empty list.

2.   You can only use one TableStyle for a control if you use only ArrayLists as a container.

3.   The NDO Enhancer creates some public properties for all persistent classes. Hence you always have to use TableStyles if you do not want to display these properties in the grid.

.NET offers some interesting mechanisms for the adaptation of data binding that are used by NDO.

The NDO Solution

NDO uses a class NDOArrayList which is derived from ArrayList. The container functionality of the NDOArrayList remains the same but it implements the ITypedList interface. This interface allows declaring meta data for the elements in the list. Furthermore a name can be assigned that can be used as MappingName afterwards.

Now the example can be written that way:

IList kundenListe = new NDOArrayList(typeof(Employee));

This allows binding an empty customer list to a control. The additional properties the NDO enhancer generates for persistent classes are invisible. The Mapping name for use with the TableStyles is Employee. It is no problem to display different element classes in one grid.