Certified ASP.NET Programmer Learning Resources Enhancing the GridView Control

Learning Resources

Enhancing the GridView Control

A recurring task in software development is to display tabular data. ASP.NET provides a number of tools for showing tabular data in a grid, including the GridView control. With the GridView control, you can display, edit, and delete data from many different kinds of data sources, including databases, XML files, and business objects that expose data.

You can use the GridView control to do the following:

  • Automatically bind to and display data from a data source control.

  • Select, sort, page through, edit, and delete data from a data source control.

Additionally, you can customize the appearance and behavior of the GridView control by doing the following:

  • Specifying custom columns and styles.

  • Utilizing templates to create custom user interface (UI) elements.

  • Adding your own code to the functionality of the GridView control by handling events.

Data Binding with the GridView Control

The GridView control provides you with two options for binding to data:

  • Data binding using the DataSourceID property, which allows you to bind the GridView control to a data source control. This is the recommended approach because it allows the GridView control to take advantage of the capabilities of the data source control and provide built-in functionality for sorting, paging, and updating.

  • Data binding using the DataSource property, which allows you to bind to various objects, including ADO.NET datasets and data readers. This approach requires you to write code for any additional functionality such as sorting, paging, and updating.

When you bind to a data source using the DataSourceID property, the GridView control supports two-way data binding. In addition to the control displaying returned data, you can enable the control to automatically support update and delete operations on the bound data.

Formatting Data Display in the GridView Control

You can specify the layout, color, font, and alignment of the GridView control's rows. You can specify the display of text and data contained in the rows. Additionally, you can specify whether the data rows are displayed as items, alternating items, selected items, or edit-mode items. The GridView control also allows you to specify the format of the columns. 

Editing and Deleting Data Using the GridView Control

By default, the GridView control displays data in read-only mode. However, the control also supports an edit mode in which it displays a row that contains editable controls such as TextBox or CheckBox controls. You can also configure the GridView control to display a Delete button that users can click to delete the corresponding record from the data source.

The GridView control can automatically perform editing and deleting operations with its associated data source, which allows you to enable editing behavior without writing code. Alternatively, you can control the process of editing and deleting data programmatically, such as in cases where the GridView control is bound to a read-only data source control.

You can customize the input controls that are used when a row is in edit mode using a template.


 For Support