| 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: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control |
03/30/2017 |
.net-framework |
|
article |
|
|
63d770dc-b3f5-462b-901a-3125b2753792 |
16 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control
You can use the xref:System.Windows.Forms.DataGridView control methods to resize rows, columns, and headers so that they display their entire values without truncation. You can use these methods to resize xref:System.Windows.Forms.DataGridView elements at times of your choosing. Alternately, you can configure the control to resize these elements automatically whenever content changes. This can be inefficient, however, when you are working with large data sets or when your data changes frequently. For more information, see Sizing Options in the Windows Forms DataGridView Control.
Typically, you will programmatically adjust xref:System.Windows.Forms.DataGridView elements to fit their content only when you load new data from a data source or when the user has edited a value. This is useful to optimize performance, but it is also useful when you want to enable your users to manually resize rows and columns with the mouse.
The following code example demonstrates the options available to you for programmatic resizing.
Example
[!code-cppSystem.Windows.Forms.DataGridView.ProgrammaticResizing#0] [!code-csharpSystem.Windows.Forms.DataGridView.ProgrammaticResizing#0] [!code-vbSystem.Windows.Forms.DataGridView.ProgrammaticResizing#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.AutoResizeColumn%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.AutoResizeColumns%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.AutoResizeColumnHeadersHeight%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.AutoResizeRow%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.AutoResizeRows%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.AutoResizeRowHeadersWidth%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewAutoSizeRowMode
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: Automatically Resize Cells When Content Changes in the Windows Forms DataGridView Control