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.

To add a GridView control to a page

  • Drag the GridView control from the Toolbox task pane to your page.

After you add a GridView control, you can specify a data source for the control.

To bind the GridView control to a data source

  1. In Design view, right-click the GridView control, and then click Show Common Control Tasks.

  2. On the Common DropDownList Tasks menu, click an existing data source or <New Data Source...> in the Choose Data Source dropdown.

  3. If you choose <New Data Source...>, configure a new data source in the Data Source Configuration Wizard. For more information, see Configure a data source by using a data bound control.

Formatting the 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.

To specify GridView control display options

  1. Click the GridView control in Design view to select it, right-click the control, and choose Properties from the shortcut menu to open the Tag Properties task pane.

  2. Specify the styles you want for the various GridView elements in the Styles category of the Properties task pane. For example, under the Font property group in the RowStyle property group, set the Name property to the font you want for the items in the rows in the GridView.

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 TextBoxor CheckBoxcontrols. 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.

To specify GridView control data editing options

  1. In Design view, right-click the GridView control, and then click Show Common Control Tasks.

  2. Check the functionality you want:

    • Enable paging — Displays only a subset of the records on a page and allows a user to move from page to page to display more records. For more information, see Paging in a GridView Web Server Control in the MSDN library.

    • Enable sorting — Allows a user to sort the records from the database. The GridView control supports sorting by a single column without requiring any programming. You can further customize the sort functionality of the GridView control by using the sort event and providing a sort expression. For more information, see Sorting Data in a GridView Web Server Control in the MSDN library.

    • Enable editing — Allows a user to make changes to the records.

    • Enable deleting — Allows a user to delete rows from the database.

    • Enable selection — Allows a user to select rows. You specify the look of selected rows by setting the styles in the SelectedRowStyle group in the Properties task pane.

For more information about the GridView control, see GridView Web Server Control Overview in the MSDN library.

See also


ASP.NET overview

ASP.NET data controls overview

DataList control

DetailsView control

FormView control

Repeater control