Permalink
Fetching contributors…
Cannot retrieve contributors at this time
44 lines (37 sloc) 2.95 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 the Appearance of Cells in the Windows Forms DataGridView Control
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
data grids, customizing cells
DataGridView control [Windows Forms], customizing cells
cells, customizing in DataGridView control
478b20c9-625c-4116-9c5c-5a16e6f4ec67
11
dotnet-bot
dotnetcontent
wpickett

How to: Customize the Appearance of Cells in the Windows Forms DataGridView Control

You can customize the appearance of any cell by handling the xref:System.Windows.Forms.DataGridView control's xref:System.Windows.Forms.DataGridView.CellPainting event. You can extract the xref:System.Windows.Forms.DataGridView control's xref:System.Drawing.Graphics from the xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs.Graphics%2A property of the xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs. With this xref:System.Drawing.Graphics, you can affect the appearance of the entire xref:System.Windows.Forms.DataGridView control, but you will usually want to affect only the appearance of the cell that is currently being painted. The xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs.ClipBounds%2A property of the xref:System.Windows.Forms.DataGridViewCellPaintingEventArgs enables you to restrict your painting operations to the cell that is currently being painted.

In the following code example, you will paint all the cells in a ContactName column using the xref:System.Windows.Forms.DataGridView control's color scheme. Each cell's text content is painted in xref:System.Drawing.Color.Crimson%2A, and an inset rectangle is drawn in the same color as the xref:System.Windows.Forms.DataGridView control's xref:System.Windows.Forms.DataGridView.GridColor%2A property.

Example

[!code-csharpSystem.Windows.Forms.DataGridViewCellPainting#10] [!code-vbSystem.Windows.Forms.DataGridViewCellPainting#10]

Compiling the Code

This example requires:

  • A xref:System.Windows.Forms.DataGridView control named dataGridView1 with a ContactName column such as the one in the Customers table in the Northwind sample database.

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

See Also

xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.DataGridView.CellPainting
Customizing the Windows Forms DataGridView Control