Data Display Modes in the Windows Forms DataGridView Control

The DataGridView control can display data in three distinct modes: bound, unbound, and virtual. Choose the most suitable mode based on your requirements.

Unbound

Unbound mode is suitable for displaying relatively small amounts of data that you manage programmatically. You do not attach the DataGridView control directly to a data source as in bound mode. Instead, you must populate the control yourself, typically by using the DataGridViewRowCollection.Add method.

Unbound mode can be particularly useful for static, read-only data, or when you want to provide your own code that interacts with an external data store. When you want your users to interact with an external data source, however, you will typically use bound mode.

For an example that uses a read-only unbound DataGridView, see How to: Create an Unbound Windows Forms DataGridView Control.

Bound

Bound mode is suitable for managing data using automatic interaction with the data store. You can attach the DataGridView control directly to its data source by setting the DataSource property. When the control is data bound, data rows are pushed and pulled without the need of explicit management on your part. When the AutoGenerateColumns property is true, each column in your data source will cause a corresponding column to be created in the control. If you prefer to create your own columns, you can set this property to false and use the DataPropertyName property to bind each column when you configure it. This is useful when you want to use a column type other than the types that are generated by default. For more information, see Column Types in the Windows Forms DataGridView Control.

For an example that uses a bound DataGridView control, see Walkthrough: Validating Data in the Windows Forms DataGridView Control.

You can also add unbound columns to a DataGridView control in bound mode. This is useful when you want to display a column of buttons or links that enable users to perform actions on specific rows. It is also useful to display columns with values calculated from bound columns. You can populate the cell values for calculated columns in a handler for the CellFormatting event. If you are using a DataSet or DataTable as the data source, however, you might want to use the DataColumn.Expression property to create a calculated column instead. In this case, the DataGridView control will treat calculated column just like any other column in the data source.

Sorting by unbound columns in bound mode is not supported. If you create an unbound column in bound mode that contains user-editable values, you must implement virtual mode to maintain these values when the control is sorted by a bound column.

Virtual

With virtual mode, you can implement your own data management operations. This is necessary to maintain the values of unbound columns in bound mode when the control is sorted by bound columns. The primary use of virtual mode, however, is to optimize performance when interacting with large amounts of data.

You attach the DataGridView control to a cache that you manage, and your code controls when data rows are pushed and pulled. To keep the memory footprint small, the cache should be similar in size to the number of rows currently displayed. When the user scrolls new rows into view, your code requests new data from the cache and optionally flushes old data from memory.

When you are implementing virtual mode, you will need to track when a new row is needed in the data model and when to rollback the addition of the new row. The exact implementation of this functionality will depend on the implementation of the data model and the transaction semantics of the data model; whether commit scope is at the cell or row level.

For more information about virtual mode, see Virtual Mode in the Windows Forms DataGridView Control. For an example that shows how to use virtual mode events, see Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.

See also