Permalink
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (52 sloc) 4.48 KB
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: Customize Data Formatting in the Windows Forms DataGridView Control
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
DataGridView control [Windows Forms], formatting data
DataGridView control [Windows Forms], cell styles
cells, changing colors in DataGridView control [Windows Forms]
colors, changing in DataGridView control [Windows Forms]
data [Windows Forms], formatting in DataGridView control
DataGridView control [Windows Forms], cell customization
DataGridView control [Windows Forms], changing cell colors
Windows Forms, changing colors of DataGridView cells
DataGridView control [Windows Forms], substituting cell values for display
data grids, formatting data
a6e72c70-ce18-425f-828d-d57be6f96ab6
21
dotnet-bot
dotnetcontent
wpickett

How to: Customize Data Formatting in the Windows Forms DataGridView Control

The following code example demonstrates how to implement a handler for the xref:System.Windows.Forms.DataGridView.CellFormatting?displayProperty=nameWithType event that changes how cells are displayed depending on their columns and values.

Cells in the Balance column that contain negative numbers are given a red background. You can also format these cells as currency to display parentheses around negative values. For more information, see How to: Format Data in the Windows Forms DataGridView Control.

Cells in the Priority column display images in place of corresponding textual cell values. The xref:System.Windows.Forms.ConvertEventArgs.Value%2A property of the xref:System.Windows.Forms.DataGridViewCellFormattingEventArgs is used both to get the textual cell value and to set the corresponding image display value.

Example

[!code-csharpSystem.Windows.Forms.DataGridViewCustomizeDataFormatting#00] [!code-vbSystem.Windows.Forms.DataGridViewCustomizeDataFormatting#00]

Compiling the Code

This example requires:

  • References to the System, System.Drawing, and System.Windows.Forms assemblies.

  • xref:System.Drawing.Bitmap images named highPri.bmp, mediumPri.bmp, and lowPri.bmp residing in the same directory as the executable file.

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.DefaultCellStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridViewBand.DefaultCellStyle%2A?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.DataGridViewCellStyle
xref:System.Drawing.Bitmap
Displaying Data in the Windows Forms DataGridView Control
How to: Format Data in the Windows Forms DataGridView Control
Cell Styles in the Windows Forms DataGridView Control
Data Formatting in the Windows Forms DataGridView Control