| 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: Format Data in the Windows Forms DataGridView Control |
03/30/2017 |
.net-framework |
|
article |
|
|
8c33543c-9c08-4636-a65a-fdf714a529b7 |
16 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Format Data in the Windows Forms DataGridView Control
The following procedures demonstrate basic formatting of cell values using the xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A property of a xref:System.Windows.Forms.DataGridView control and of specific columns in a control. For information about advanced data formatting, see How to: Customize Data Formatting in the Windows Forms DataGridView Control.
To format currency and date values
-
Set the xref:System.Windows.Forms.DataGridViewCellStyle.Format%2A property of a xref:System.Windows.Forms.DataGridViewCellStyle. The following code example sets the format for specific columns using the xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A property of the columns. Values in the
UnitPricecolumn appear in the current culture-specific currency format, with negative values surrounded by parentheses. Values in theShipDatecolumn appear in the current culture-specific short date format. For more information about xref:System.Windows.Forms.DataGridViewCellStyle.Format%2A values, see Formatting Types.[!code-csharpSystem.Windows.Forms.DataGridViewMisc#071] [!code-vbSystem.Windows.Forms.DataGridViewMisc#071]
To customize the display of null database values
-
Set the xref:System.Windows.Forms.DataGridViewCellStyle.NullValue%2A property of a xref:System.Windows.Forms.DataGridViewCellStyle. The following code example uses the xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType property to display "no entry" in all cells containing values equal to xref:System.DBNull.Value?displayProperty=nameWithType.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#073] [!code-vbSystem.Windows.Forms.DataGridViewMisc#073]
To enable wordwrap in text-based cells
-
Set the xref:System.Windows.Forms.DataGridViewCellStyle.WrapMode%2A property of a xref:System.Windows.Forms.DataGridViewCellStyle to one of the xref:System.Windows.Forms.DataGridViewTriState enumeration values. The following code example uses the xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType property to set the wrap mode for the entire control.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#074] [!code-vbSystem.Windows.Forms.DataGridViewMisc#074]
To specify the text alignment of DataGridView cells
-
Set the xref:System.Windows.Forms.DataGridViewCellStyle.Alignment%2A property of a xref:System.Windows.Forms.DataGridViewCellStyle to one of the xref:System.Windows.Forms.DataGridViewContentAlignment enumeration values. The following code example sets the alignment for a specific column using the xref:System.Windows.Forms.DataGridViewColumn.DefaultCellStyle%2A property of the column.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#072] [!code-vbSystem.Windows.Forms.DataGridViewMisc#072]
Example
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#070] [!code-vbSystem.Windows.Forms.DataGridViewMisc#070]
Compiling the Code
These examples require:
-
A xref:System.Windows.Forms.DataGridView control named
dataGridView1that contains a column namedUnitPrice, a column namedShipDate, and a column namedCustomerName. -
References to the xref:System?displayProperty=nameWithType, xref:System.Drawing?displayProperty=nameWithType, and xref:System.Windows.Forms?displayProperty=nameWithType assemblies.
Robust Programming
For maximum scalability, you should share xref:System.Windows.Forms.DataGridViewCellStyle objects across multiple rows, columns, or cells that use the same styles rather than setting the style properties for each element separately. For more information, see Best Practices for Scaling the Windows Forms DataGridView Control.
See Also
xref:System.Windows.Forms.DataGridView.DefaultCellStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewBand.DefaultCellStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewCellStyle
Basic Formatting and Styling in the Windows Forms DataGridView Control
Cell Styles in the Windows Forms DataGridView Control
Data Formatting in the Windows Forms DataGridView Control
How to: Customize Data Formatting in the Windows Forms DataGridView Control
Formatting Types