| title | ms.custom | ms.date | ms.prod | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Automatically Resize Cells When Content Changes in the Windows Forms DataGridView Control |
03/30/2017 |
.net-framework |
|
article |
|
|
1d68934d-a04c-4b12-9e66-c856c6828131 |
19 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Automatically Resize Cells When Content Changes in the Windows Forms DataGridView Control
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.
Example
[!code-cppSystem.Windows.Forms.DataGridView.AutoSizing#0] [!code-csharpSystem.Windows.Forms.DataGridView.AutoSizing#0] [!code-vbSystem.Windows.Forms.DataGridView.AutoSizing#0]
Compiling the Code
This example requires:
- References to the System, System.Drawing, and System.Windows.Forms assemblies.
For information about building this example from the command line for [!INCLUDEvbprvb] or [!INCLUDEcsprcs], see Building from the Command Line or Command-line Building With csc.exe. You can also build this example in [!INCLUDEvsprvs] by pasting the code into a new project. Also see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.
See Also
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