Browse DevX
Sign up for e-mail newsletters from DevX


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

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

User Controls and Other Bindings
Figure 1. Binding User Controls: The figure shows a user control containing a single grid with a template Orders column. Note that it displays both orders and order details.
Nested grids are very useful, but sometimes you need more control over the layout and presentation of the data—and some support from the IDE. User controls can provide this flexibility. An added bonus is that you can use the IDE to develop the control, defining the grid columns, etc. The following example uses a custom control to display the Order object in a grid that also displays the order details (see Figure 1).

The CustomerOrders.aspx file contains a single grid with a template Orders column containing a reference to the OrderViewControl.ascx control for both the item and edit template. I find it easiest to drag-and-drop the control onto the page and then copy the control to the template column built with the property builder. This ensures that the page references the control properly.

Author's Note: In the control's Page_Load method you must make an explicit call to the DataBind() method of your grid control. Calling the control's DataBind method doesn't seem to work!

private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { OrderGrid.DataBind(); } }

For comparison, the sample code includes a nested grid version implemented in the single TreeGrid.aspx file.

So far, you've seen how to bind a class property value to a control. The .NET framework provides a much richer set of bindings that this. In fact, you can bind any property of a control to any property in an object collection. This is particularly useful if you want to emphasize object state visually. To maintain a clean separation between presentation details and object implementation you can use stylesheets to add the visual emphasis. The following example binds the value and style name from an object to the control. How the value is displayed is then a function of the stylesheet rather than the control and object model.

<asp:Label id="Label1" CssClass = '<%# MyProperty1.Style %>' Text = '<%# MyProperty1.Value %>' runat="server">Label</asp:Label>

Here's a slightly more complex example that uses an integer property value to determine which CSS class to apply. Note that this has the advantage of placing all knowledge of how the value is to be presented to the user into the interface—but it also moves the decision on what should be highlighted out of the object, which may not be desirable.

<asp:Label id="Label3" CssClass='<%# MyIntProperty1 > 0 ? "highlight" : "normal" %>' Text='<%# MyIntProperty1 %>' runat="server">Label</asp:Label>

Collection Interfaces
Many controls work with object collections as data sources. For data binding purposes, you can define a collection by the interfaces it supports. Two interfaces are particularly important: IEnumerable and IList.

IEnumerable is really a gateway interface supporting a single method called GetEnumerator that returns an object that implements the IEnumerator interface. IEnumerator contains one property, Current, and two methods, MoveNext and Reset. The initial call to MoveNext moves the pointer to the first object, and subsequent calls move the pointer to the next object in the collection. The method returns false when there are no more items in the collection. As long as MoveNext returns true the control uses the Current property to access the object.

Some controls require that the collection support the IList interface, (such as ListControl and ComboBox). IList provides the control with more flexibility in how it retrieves the required data. IList is a descendent of ICollection, IEnumerable. The control typically makes use of the ICollection.Count method to get the number of items in the collection and then uses the Item method (indexer in C#) to obtain the contained objects.

Given that data sources are characterised by interfaces rather than classes, you can map data sources onto almost any kind of data or object. The objects returned by the interface do not need to be of the same type—they just need to support the property or properties you specified the control should read for its values.

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