title | description | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Automatically Resize Cells When Content Changes in DataGridView Control |
Learn more about how to automatically resize cells when content changes in the Windows Forms DataGridView Control. |
03/30/2017 |
|
|
1d68934d-a04c-4b12-9e66-c856c6828131 |
You can configure the xref:System.Windows.Forms.DataGridView control to resize its rows, columns, and headers automatically whenever content changes, so that cells are always large enough to display their values without clipping.
You have many options to restrict which cells are used to determine the new sizes. For example, you can configure the control to automatically resize the width of its columns based only on the values in rows that are currently displayed. With this, you can avoid inefficiency when working with large numbers of rows, although in this case, you might want to use sizing methods such as xref:System.Windows.Forms.DataGridView.AutoResizeColumns%2A to adjust sizes at times of your choosing.
For more information about automatic resizing, see Sizing Options in the Windows Forms DataGridView Control.
The following code example demonstrates the options available for automatic resizing.
[!code-cppSystem.Windows.Forms.DataGridView.AutoSizing#0] [!code-csharpSystem.Windows.Forms.DataGridView.AutoSizing#0] [!code-vbSystem.Windows.Forms.DataGridView.AutoSizing#0]
This example requires:
- References to the System, System.Drawing, and System.Windows.Forms assemblies.
- xref:System.Windows.Forms.DataGridView
- xref:System.Windows.Forms.DataGridView.ColumnHeadersHeightSizeMode%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridView.RowHeadersWidthSizeMode%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridView.AutoSizeColumnsMode%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridView.AutoSizeRowsMode%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridViewColumn.AutoSizeMode%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridViewColumn.InheritedAutoSizeMode%2A?displayProperty=nameWithType
- xref:System.Windows.Forms.DataGridViewAutoSizeRowsMode
- xref:System.Windows.Forms.DataGridViewAutoSizeColumnMode
- xref:System.Windows.Forms.DataGridViewAutoSizeColumnsMode
- xref:System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode
- xref:System.Windows.Forms.DataGridViewRowHeadersWidthSizeMode
- Resizing Columns and Rows in the Windows Forms DataGridView Control
- Sizing Options in the Windows Forms DataGridView Control
- How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control