Move Over DataGrid, There's a New Grid in Town!

Move Over DataGrid, There's a New Grid in Town!
Dino Esposito

This article is based on the May 2004 Technology Preview of ASP.NET 2.0. All information herein is subject to change.
This article discusses:
  • The ASP.NET 2.0 GridView, FormView, and DetailsView
  • Differences between the DataGrid and the GridView
  • The programming interfaces of these controls
  • How to achieve master/detail views
This article uses the following technologies:
ASP.NET, ASP.NET 2.0, C#
Code download available at: GridView.exe (124 KB)
Browse the Code Online
Despite the richness and versatility of its programming interface, the ASP.NET 1.x DataGrid control requires you to write a lot of custom code to handle common operations such as paging, sorting, editing, and deleting data. For example, while the DataGrid control can raise events when the user clicks to save or cancel changes, it doesn't offer much more than that. If you want to store changes to a persistent medium, such as a database, you have to handle the UpdateCommand event yourself, retrieve changed values, prepare a SQL command, and then proceed from there to commit the update.
The reason the DataGrid control limits the raising of events for common data operations is that it's a data source-agnostic control that can be bound to any data object that is enumerable. Implementing data operations such as update or delete would require a direct link with one particular data source. In ASP.NET 1.x, you work around this limitation by writing ADO.NET code that is specific to your application.
ASP.NET 2.0 enhances the data-binding architecture, introducing a new family of components—the data source objects—which act as a bridge between data-bound controls and ADO.NET objects. These source objects promote a slightly different programming model and provide for new features and members. For data reporting purposes, your ASP.NET 2.0 applications should use the newest grid control—the GridView. The familiar DataGrid control is still supported, but it doesn't take full advantage of the specific capabilities of data source components.
The GridView control is the successor to the DataGrid and extends it in a number of ways. First, it fully supports data source components and can automatically handle data operations, such as paging, sorting, and editing, provided its bound data source object supports these capabilities. In addition, the GridView control offers some functional improvements over the DataGrid. In particular, it supports multiple primary key fields and exposes some user interface enhancements and a new model for handling and canceling events.
The GridView comes with a pair of complementary view controls: DetailsView and FormView. By combining these controls, you can easily set up master/detail views using very little code and sometimes no code at all.

GridView Versus DataGrid
In ASP.NET 2.0, the class hierarchy of data-bound controls is more consistent than in ASP.NET 1.x. In version 2.0, all controls derive from the same base class (the BaseDataBoundControl class), regardless of the actual implementation or user interface characteristics. Figure 1 shows the new class diagram. The DataGrid and other version 1.x controls like Repeater and DataList are not included in the diagram. The inheritance tree for these existing controls is the same as ASP.NET 1.x. In particular, the Repeater inherits WebControl, whereas DataList and DataGrid inherit BaseDataList. As you can see in Figure 1, GridView is a composite data-bound control and shares a common set of methods and properties with all other data-bound controls, including DropDownList, DetailsView, and ListBox.
Figure 1 ASP.NET Class Diagram 
Although similar in their high-level functionality, the GridView and the DataGrid controls have a different foundation. To enable easy migration from existing pages, the GridView preserves the DataGrid object model as much as possible. However, you shouldn't expect 100 percent compatibility between DataGrid-based code and new GridView-based code.
Another key difference between the DataGrid and GridView controls lies in the adaptive user interface. Unlike the version 1.x DataGrid, the GridView can render on mobile devices, too. In other words, to build reports on mobile devices, you can use the same grid control you would use for desktop pages. The DataGrid in version 2.0 can also render adaptively, but its UI capabilities are not quite as rich as those of the GridView.
In ASP.NET 2.0, the DataGrid control has been enhanced to support common control features such as themes and personalization. In addition, the new DataGrid control can be populated by a data source control. Remember, though, that a DataGrid can only bind to a data source object for the purpose of reading data. To actually modify the underlying data source, some user-defined code is still required. In contrast, the GridView control takes advantage of the capabilities of the underlying data source and automatically deletes or updates records. Note that the GridView control also supports the classic binding mechanism based on the DataSource property and the DataBind method. Although fully supported, this programming practice is discouraged.

GridView and Data Source Controls
So, what's a data source control? I covered this hot new feature of ASP.NET 2.0 in great detail in the June 2004 issue of MSDN®Magazine. In summary, a data source control is a set of Microsoft® .NET Framework classes that facilitate two-way binding between data stores and data-bound controls. Existing controls like the DataGrid and new data-bound controls like the GridView can be bound to a data source, albeit with different capabilities.
A data source control represents the key functions of a data source: select, insert, update, and delete. Data source controls can represent any data source from relational databases to XML files, from streams to business objects. If this brief description makes you think of .NET managed providers, take a look at Figure 2.
Figure 2 Data Source Controls, GridView, and Data Sources 
Data source controls may sit on top of some .NET data providers and form an intermediate layer between data-bound controls and data sources. Data source controls also expose a common interface for a few basic operations. Some of the data-bound controls—specifically, the GridView control—bind to these commands to automate in-place editing along with other data-related operations.
A data source control uses its properties and methods to expose the bound content as a set of named views. The IDataSource interface that all data source controls implement provides the basic set of capabilities to retrieve views of data from a data source. ASP.NET 2.0 supplies a handful of built-in data source controls, as listed in Figure 3. The data source controls in Figure 3 belong to two categories: tabular and hierarchical components. The SiteMapDataSource and XmlDataSource components are hierarchical data source controls and can be used by hierarchical components like the TreeView and Menu control. The various other components manage tabular data.



See details: http://msdn.microsoft.com/en-us/magazine/cc163933.aspx

Comments

Popular posts from this blog

Asynchronous Socket Programming in C#

Url Routing MVC TUTORIAL

WCF Chat Sample