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 |
|
|
478b20c9-625c-4116-9c5c-5a16e6f4ec67 |
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.
[!code-csharpSystem.Windows.Forms.DataGridViewCellPainting#10] [!code-vbSystem.Windows.Forms.DataGridViewCellPainting#10]
This example requires:
-
A xref:System.Windows.Forms.DataGridView control named
dataGridView1
with aContactName
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.
- xref:System.Windows.Forms.DataGridView
- xref:System.Windows.Forms.DataGridView.CellPainting
- Customizing the Windows Forms DataGridView Control