Browse DevX
Sign up for e-mail newsletters from DevX


Making Connections: Binding Controls to Custom Data Sources in .NET : Page 5

Anyone can bind a DataGrid to a DataSet in .NET, but the fact that you can bind almost any control property to almost any suitable object property—even to custom objects or custom object collections—is far less widely known.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Flattening Data for Display
I am sure there's a better term for this concept, but the idea is that you want to display a collection of values in a single control. For example, in most cases it's convenient to store an address as separate strings such as postal code, city, street address, etc., but for display purposes, you would often want to "flatten" the values, concatenating and displaying them as a single string in a Label, TextBox, or other control.

The Address class below shows one solution. Internally it stores the various address parts in a StringCollection, but binding to the object calls the ToString() method which combines the address lines into a single string. If you have an existing model or third-party library that you can't change, you can write an adapter class to map the multiple strings in the collection to the control.

public class Address { public Address() { this.addressLines = new StringCollection(); // Example Address this.addressLines.Add("Tadcombe"); this.addressLines.Add("UK"); } public Address(StringCollection addressLines) { this.addressLines = addressLines; } StringCollection addressLines; public override string ToString() { string result = ""; foreach (string line in addressLines) { if (line.Length > 0) { if (result.Length > 0) { result += ", "; } result += line; } } return result; } }

The file CustomerOrders2.aspx in the sample code demonstrates binding the Address value to a GridCell, but the principle applies just as well to other single-valued controls.

If you wanted the address to be split into separate lines you could use a repeater control bound to the collection with your single-valued control displaying each element of the collection.

The sample code contains working examples of the techniques outlined. The strongly typed collections were generated using CodeSmith Explorer which makes light work of generating the code.

The samples are all written in C# but mapping to another .NET language should be reasonably straightforward.

Although the process to connect arbitrary objects and object hierarchies to user interface controls in the .NET framework is straightforward, you'll find there are a few stumbling blocks to catch the unwary. First, remember to call DataBind() either on the form or user control directly. Expose the attributes to which you want to bind control values as public properties rather than as fields to allow both direct referencing and indirect referencing using DataBinder.Eval().

Graham Brooks is an independent software development consultant specializing in application development using Agile Methods. Graham has been working with Microsoft Technologies since the late 1980's, beginning with Windows 2.0 and C++.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date