Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 2.77 KB

customize-the-appearance-of-cells-in-the-datagrid.md

File metadata and controls

37 lines (27 loc) · 2.77 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
Customize the Appearance of Cells in DataGridView Control
Learn about how to customize the appearance of cells in the Windows Forms DataGridView control.
03/30/2017
csharp
vb
data grids [Windows Forms], customizing cells
DataGridView control [Windows Forms], customizing cells
cells [Windows Forms], customizing in DataGridView control
478b20c9-625c-4116-9c5c-5a16e6f4ec67

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