The ASP.NET GridView Control Offers Developers Speed And Flexibility

by Edward Alexander

Displaying information on a web page which comes from a database is a fairly standard requirement on most websites and, in some development environments, it can take a fair amount of programming to achieve. In ASP.NET, however, it is remarkably easy, thanks to the use of built-in data-bound controls. There are two types of data-bound controls: those which are designed to display multiple records and those which will display one record only. The GridView control, the workhorse of databinding in ASP.NET, falls into the first category. It automatically generates an HTML table and fills it with information from a given data source with each record in the database rendered as a row in the table.

When using Visual Web Developer the easiest way to create a GridView control is simply to explore the appropriate data source using the database explorer, then drag the appropriate table or view onto the page. This action creates both a DataSource object and a GridView object, both of which can be customised either in Design or Source view. If the listing you wish to display on the page is to be tabular, you may not need to change the default GridView. However, it is more usual that you will want to customize the way in which data is displayed.

Naturally, you do not have to display all of the fields in the database in the HTML table generated by the Gridview. The GridView’s AutoGenerateColumns attribute should be set to False (which it is by default). Additionally, we can display data in a number of different ways. By default information is displayed in a BoundField, meaning that the data is displayed as text. By default, the heading displayed at the top of each column is simply the name of each field in the database. However, you can modify this by changing the HeaderText property of each BoundField element. Another useful property is DataFormatString which allow you to use a format string to control the display of data.

Another useful form of customization is the display of data in a choice of other HTML formats: CheckboxField, RadioButtonField, HyperlinkField, ButtonField and ImageField. The final type of field is CommandField which enables you to display links enabling the user to edit, delete or select rows of data.

For even greater flexibility, the GridView control also provides the TemplateField which allows you to place any HTML content you like in a given column and use databinding statements to embed information from the database as required. Template fields can even contain ASP.NET server controls.

About the Author:

Leave a Reply

CommentLuv Enabled