DataGridView Control Scenarios (Windows Forms)

With the DataGridView control, you can display tabular data from a variety of data sources. For simple uses, you can manually populate a DataGridView and manipulate the data directly through the control. Typically, however, you will store your data in an external data source and bind the control to it through a BindingSource component.

This topic describes some of the common scenarios that involve the DataGridView control.

Scenario 1: Displaying Small Amounts of Data

You do not have to store your data in an external data source to display it in the DataGridView control. If you are working with a small amount of data, you can populate the control yourself and manipulate the data through the control. This is called unbound mode. For more information, see How to: Create an Unbound Windows Forms DataGridView Control.

Scenario Key Points

  • In unbound mode, you populate the control manually.

  • Unbound mode is particularly suited for small amounts of read-only data.

  • Unbound mode is also suited for spreadsheet-like or sparsely populated tables.

Scenario 2: Viewing and Updating Data Stored in an External Data Source

You can use the DataGridView control as a user interface (UI) through which users can access data kept in a data source such as a database table or a collection of business objects. For more information, see How to: Bind Data to the Windows Forms DataGridView Control.

Scenario Key Points

  • Bound mode lets you connect to a data source, automatically generate columns based on the data source properties or database columns, and automatically populate the control.

  • Bound mode is suited for heavy user interaction with data. Data can be formatted for display, and user-specified data can be parsed into the format expected by the data source. Data entry formatting errors and database constraint errors can be detected so that users can be warned and erroneous cells can be corrected.

  • Additional functionality such as column sorting, freezing, and reordering enable users to view data in the way most convenient for their workflow.

  • Clipboard support enables users to copy data from your application into other applications.

Scenario 3: Advanced Data

If you have special needs that the standard data binding model does not address, you can manage the interaction between the control and your data by implementing virtual mode. Implementing virtual mode means implementing one or more event handlers that let the control request information about cells as the information is needed.

For example, if you work with large amounts of data, you may want to implement virtual mode to ensure optimal efficiency. Virtual mode is also useful for maintaining the values of unbound columns that you display along with columns retrieved from another data source.

For more information about virtual mode, see Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.

Scenario Key Points

  • Virtual mode is suited for displaying very large amounts of data when you need to fine-tune performance.

Scenario 4: Automatically Resizing Rows and Columns

When you display data that is regularly updated, you can automatically resize rows and columns to ensure that all content is visible. The DataGridView control provides several options that let you enable or disable manual resizing, resize programmatically at specific times, or resize automatically whenever content changes. For more information, see Sizing Options in the Windows Forms DataGridView Control.

Scenario Key Points

  • Manual resizing enables users to adjust cell heights and widths.

  • Automatic resizing enables you to maintain cell sizes so that cell content is never clipped.

  • Programmatic resizing enables you to resize cells at specific times to avoid the performance penalty of continuous automatic resizing.

Scenario 5: Simple Customization

The DataGridView control provides many ways for you to alter its basic appearance and behavior. For more information, see Cell Styles in the Windows Forms DataGridView Control.

Scenario Key Points

  • DataGridViewCellStyle objects let you provide color, font, formatting, and positioning information at multiple levels and for individual elements of the control.

  • Cell styles can be layered and shared by multiple elements, letting you reuse code.

Scenario 6: Advanced Customization

The DataGridView control provides many ways for you to customize its appearance and behavior.

Scenario Key Points

See Also

Reference

DataGridView Control Overview (Windows Forms)

DataGridView