dotnetmonitor.com

 
Index
Previous
Next

 

DataGridView rows, columns, and headers can change size as a result of many different occurrences. The following table shows these occurrences.

Occurrence

Description

User resize

Users can make size adjustments by dragging or double-clicking row, column, or header dividers.

Control resize

In column fill mode, column widths change when the control width changes; for example, when the control is docked to its parent form and the user resizes the form.

Cell value change

In content-based automatic sizing modes, sizes change to fit new display values.

Method call

Programmatic content-based resizing lets you make opportunistic size adjustments based on cell values at the time of the method call.

Property setting

You can also set specific height and width values.

 

By default, user resizing is enabled, automatic sizing is disabled, and cell values that are wider than their columns are clipped.

 

The following table shows scenarios that you can use to adjust the default behavior or to use specific sizing options to achieve particular effects.

Scenario

Implementation

Use column fill mode for displaying similarly sized data in a relatively small number of columns that occupy the entire width of the control without displaying the horizontal scroll bar.

Set the AutoSizeColumnsMode property to Fill.

Use column fill mode with display values of varying sizes.

Set the AutoSizeColumnsMode property to Fill. Initialize relative column widths by setting the column FillWeight properties or by calling the control AutoResizeColumns method after filling the control with data.

Use column fill mode with values of varying importance.

Set the AutoSizeColumnsMode property to Fill. Set large MinimumWidth values for columns that must always display some of their data or use a sizing option other than fill mode for specific columns.

Use column fill mode to avoid displaying the control background.

Set the AutoSizeMode property of the last column to Fill and use other sizing options for the other columns.

Display a fixed-width column, such as an icon or ID column.

Set AutoSizeMode to None and Resizable to False for the column. Initialize its width by setting the Width property or by calling the control AutoResizeColumn method after filling the control with data.

Adjust sizes automatically whenever cell contents change to avoid clipping and to optimize the use of space.

Set an automatic sizing property to a value that represents a content-based sizing mode. To avoid a performance penalty when working with large amounts of data, use a sizing mode that calculates displayed rows only.

Adjust sizes to fit values in displayed rows to avoid performance penalties when working with many rows.

Use the appropriate sizing-mode enumeration values with automatic or programmatic resizing. To adjust sizes to fit values in newly displayed rows while scrolling, call a resizing method in a Scroll event handler. To customize user double-click resizing so that only values in displayed rows determine the new sizes, call a resizing method in a RowDividerDoubleClick or ColumnDividerDoubleClick event handler.

Adjust sizes to fit cell contents only at specific times to avoid performance penalties or to enable user resizing.

Call a content-based resizing method in an event handler. For example, use the DataBindingComplete event to initialize sizes after binding, and handle the CellValidated or CellValueChanged event to adjust sizes to compensate for user edits or changes in a bound data source.

Adjust row heights for multiline cell contents.

Ensure that column widths are appropriate for displaying paragraphs of text and use automatic or programmatic content-based row sizing to adjust the heights. Also ensure that cells with multiline content are displayed using a WrapMode cell style value of True.

Typically, you will use an automatic column sizing mode to maintain column widths or set them to specific widths before row heights are adjusted.

What is the DataGridView
Differences between the DataGridView and DataGrid controls
Highlight of features
Structure of DGV
Architecture Elements
Cells and Bands