Permalink
Fetching contributors…
Cannot retrieve contributors at this time
46 lines (39 sloc) 3.62 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 Rows in the Windows Forms DataGridView Control
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
data grids, customizing rows
rows, customizing in DataGridView control
DataGridView control [Windows Forms], customizing rows
d40b53d2-7e7c-48c5-8570-6e79d15c3bbb
12
dotnet-bot
dotnetcontent
wpickett

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

You can control the appearance of xref:System.Windows.Forms.DataGridView rows by handling one or both of the xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType and xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType events. These events are designed so that you can paint only what you want to while letting the xref:System.Windows.Forms.DataGridView control paint the rest. For example, if you want to paint a custom background, you can handle the xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType event and let the individual cells paint their own foreground content. Alternately, you can let the cells paint themselves and add custom foreground content in a handler for the xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType event. You can also disable cell painting and paint everything yourself in a xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType event handler.

The following code example implements handlers for both events in order to provide a gradient selection background and some custom foreground content that spans multiple columns.

Example

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

Compiling the Code

This example requires:

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

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
xref:System.Windows.Forms.DataGridView.RowPrePaint?displayProperty=nameWithType
xref:System.Windows.Forms.DataGridView.RowPostPaint?displayProperty=nameWithType
Customizing the Windows Forms DataGridView Control
DataGridView Control Architecture