Skip to Content

Quick And Easy Data Presentation With The ASP.NET GridView Control (Malcolm Bryant)

May 11, 2010

Database-driven website are a fairly standard requirement 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.

The first step in customization is often to turn off the automatic display of all the fields in the database as columns in the HTML table. This is done by setting the GridView's AutoGenerateColumns attribute to False. Secondly, 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.

The GridView also offers you the option of displaying 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.

TemplateFields offer even greater flexibility, allowing 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 AuthorIf you would like to learn more about ASP.NET training courses, visit Macresource Computer Training, a UK IT training company offering ASP.NET training courses in London and throughout the UK..

Similar entries