March 5, 2021
Hot Topics:

Using Nested DataGrids in ASP.NET

  • By Paul Kimmel
  • Send Email »
  • More Articles »

Part I: How to Use DataLists and DataGrids in ASP.NET

Common relationships in object-oriented programming are the aggregation, association, and dependency. When object A is composed of and responsible for the lifetime of a contained object B, we call this relationship an aggregation. When object A uses the facilities of another object B but is not responsible for the object's lifetime, we call this relationship an association. When an object A is dependent for part of its implementation on a second object B, we call this relationship a dependency. If the multiplicity of aggregate, associative, or dependent objects is greater than one then we often employ an array, collection, DataSet, or DataTable to represent the multiplicity of objects.

Many classes exist in the .NET Framework to express relationships between one object A and many objects B, referred to generically as one-to-many relationships. These classes will just as easily permit more complex relationships. For example, each instance of object A may contain one-to-many instances of object B and B may contain one-to-many instances of object C. These relationships are well-understood and can be easily represented with arrays, list, collections, or other data structures. What may be more challenging is representing these nested, complex relationships in the presentation layer.

One could clearly try to write reams of code that dynamically creates controls on the fly, assembling a user interface piecemeal, but such code may be difficult and time consuming to write. A better alternative is to use existing controls that model nested relationships. In this first article I will demonstrate how you can use ASP.NET controls like the DataList and a DataGrid to represent A-B-C relations where B and C represent complex objects and A contains one or more instances of B and B contains one or more instances of C. In part 2 of this article I will demonstrate how to use HTML tables to manage complex layouts and nest DataGrids within DataLists, and part 3 will demonstrate how to support editing a multiplicity of objects presented to the user with ASP.NET controls.

Binding Data to a DataList or DataGrid

The DataList and DataGrid controls can be conveniently used to present repeating objects. The source of the data can be bound directly to the DataList or DataGrid (a Repeater is a good control for read-only data, too), or you can define a template and the DataList and DataGrid will repeat those controls for every object in the source data.

Most of the ways that exist to describe a collection of data can be used as data sources for the DataList and DataGrid. By digging around in the shared source code library (sscli, or Rotor) it looks like the only requirement is that an object implement IEnumerable. This means that any array, collection, list, or ADO.NET data source can be bound directly to a DataList or DataGrid. (I have bound all of these to the DataList and DataGrid, and they work nicely.)

The basic steps for displaying data in a DataList or DataGrid is to assign your object to the control's DataSource property and called DataBind method. For example, if you place a DataGrid onto an ASP.NET Web Page, assign an array of integers to the DataGrid's DataSource property and call DataBind the integers will be displayed in the DataGrid. Listing 1 shows the code to display the array of integers.

Listing 1: Display an array of simple data.

Private Sub Page_Load(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MyBase.Load

  DataGrid1.DataSource = New Integer() {1, 2, 3, 4, 5}

End Sub

The two lines of code in listing 1 represent the bulk of work necessary to move data into a DataGrid (or DataList). While the example in listing 1 is very simple, you can use this technique to display a one of a kind object in a DataGrid. For example, if designing a GUI to display a single instance of a Customer object, you can create an array initialized with one Customer object and bind that to the DataGrid or DataList. The DataSource statement could be written as follows:

DataGrid1.DataSource = New Customer(){MyCustomer}

Page 1 of 2

This article was originally published on May 30, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

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