Problem solve Get help with specific problems with your technologies, process and projects.

Using DataGrid and GridView in ASP.NET 2.0

Ed Tittel reviews ASP.NET 2.0's news DataGrid and GridView controls, which lets users manage master/detail views or manipulate single-record views to create flexible, customized views of data.

Microsoft's Visual Studio Web pages are chock full of good information and examples about how to make effective use of that environment. As such, my job is to report on especially useful or compelling examples of this kind of thing, and to point out where related code for such things resides. This time around, I draw on an article by Dino Esposito that deals with using the DetailsView and FormView controls in ASP.NET 2.0 that work with DataGrid and GridView to enable easy construction of attractive, readable, data-driven record displays.

As it happens, though ASP 1.x included some powerful and usable data-bound controls, none of them were designed to manage views of individual records. Building master/detail views typically means allowing users to select individual records from a comprehensive records list (the master part) and then to display and manipulate the contents of a single selected record (the detail part).

The problem with ASP.NET 1.x was that, while it could show individual record fields using the DataGrid, GridView and other controls, it couldn't really manage a master/detail view or provide record-view control without buying third-party tools or building equivalent functionality on your own.

That's changed in ASP.NET 2.0. Microsoft now offers new controls that support single-record views. In particular, FormView and DetailsView controls now work with DataGrid and GridView to make it easy to create master/detail views.

With DataGrid and GridView, you see, for example, a list of company records on the left-hand side (the master view) and information about one particular record on the right hand side (the details view). You can edit the information in the details pane, but it won't show up in the master view until you click the Update button.

More from Ed Tittel

My.Settings in Visual Basic 2005

'My' extensions turbocharge new VB tool set

VS 2005 release date set; now what?

That's because the GridView (used for the master view) isn't updated until the DetailsView (used for the details view, naturally) posts changes to the actual record. The way to keep things synchronized, therefore, is to force GridView to refresh its data source any time a detail record is updated -- easy to arrange, because the DetailsView control posts an ItemUpdated event each time an update is processed.

FormView and DetailsView controls are quite similar, but differ in the way they lay out their outputs. FormView permits complete control over the layout and enables easy creation of custom forms with controls to display and edit fields. DetailsView is limited to a strictly 2D table layout with exactly two columns: header and value. You can, however, use templates to customize the value cell, by using the TemplateField class much as you would for a column within a grid control. Thus, for example, you might use a Calendar control to render a date field, and thereby inherit all the mechanics needed to properly format and display date/time information.

DetailsView also provides new ways for ASP.NET 2.0 to handle validation controls. These may apply to required fields, regular expressions, or specific value ranges. Here again, you can use templates for each field you display, adding validator controls within the template. This makes it easy to use whichever combination of validators is needed to screen and filter user input.

Overall, the enhancements that FormView and DetailsView make available are pretty compelling. If you download Esposito's sample code, you'll find numerous illustrations of all these principles put nicely to work, with value- and event-based validation techniques to boot. Definitely worth a look-see!

Ed Tittel is a full-time writer and trainer whose interests include XML and development topics, along with IT Certification and information security topics. E-mail Ed at etittel@techtarget.com with comments, questions, or suggested topics or tools to review.



Dig Deeper on ASP.NET development best practices

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

TheServerSide.com

Close