developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Sun Releases NetBeans 6.7 IDE for Java, PHP    June 29, 2009
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Free Tech Newsletter -

Using Nested DataGrids in ASP.NET
By Paul Kimmel

Go to page: 1  2  Next  

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}
  DataGrid1.DataBind()

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}

Go to page: 1  2  Next  


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Visual Basic Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs