Skip to content

Latest commit

 

History

History
77 lines (53 loc) · 7.75 KB

customize-cells-and-columns-in-the-datagrid-by-extending-behavior.md

File metadata and controls

77 lines (53 loc) · 7.75 KB
title description ms.date dev_langs helpviewer_keywords ms.assetid
Customize Cells and Columns in DataGridView Control by Extending Their Behavior and Appearance
Learn about how to customize cells and columns in the Windows Forms DataGridView by extending their behavior and appearance.
03/30/2017
csharp
vb
DataGridView control [Windows Forms], cell customization
columns [Windows Forms], customizing in DataGridView control
cells [Windows Forms], customizing in DataGridView control
9b7dc7b6-5ce6-4566-9949-902f74f17a81

How to: Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance

The xref:System.Windows.Forms.DataGridView control provides a number of ways to customize its appearance and behavior using properties, events, and companion classes. Occasionally, you may have requirements for your cells that go beyond what these features can provide. You can create your own custom xref:System.Windows.Forms.DataGridViewCell class to provide extended functionality.

You create a custom xref:System.Windows.Forms.DataGridViewCell class by deriving from the xref:System.Windows.Forms.DataGridViewCell base class or one of its derived classes. Although you can display any type of cell in any type of column, you will typically also create a custom xref:System.Windows.Forms.DataGridViewColumn class specialized for displaying your cell type. Column classes derive from xref:System.Windows.Forms.DataGridViewColumn or one of its derived types.

In the following code example, you will create a custom cell class called DataGridViewRolloverCell that detects when the mouse enters and leaves the cell boundaries. While the mouse is within the cell's bounds, an inset rectangle is drawn. This new type derives from xref:System.Windows.Forms.DataGridViewTextBoxCell and behaves in all other respects as its base class. The companion column class is called DataGridViewRolloverColumn.

To use these classes, create a form containing a xref:System.Windows.Forms.DataGridView control, add one or more DataGridViewRolloverColumn objects to the xref:System.Windows.Forms.DataGridView.Columns%2A collection, and populate the control with rows containing values.

Note

This example will not work correctly if you add empty rows. Empty rows are created, for example, when you add rows to the control by setting the xref:System.Windows.Forms.DataGridView.RowCount%2A property. This is because the rows added in this case are automatically shared, which means that DataGridViewRolloverCell objects are not instantiated until you click on individual cells, thereby causing the associated rows to become unshared.

Because this type of cell customization requires unshared rows, it is not appropriate for use with large data sets. For more information about row sharing, see Best Practices for Scaling the Windows Forms DataGridView Control.

Note

When you derive from xref:System.Windows.Forms.DataGridViewCell or xref:System.Windows.Forms.DataGridViewColumn and add new properties to the derived class, be sure to override the Clone method to copy the new properties during cloning operations. You should also call the base class's Clone method so that the properties of the base class are copied to the new cell or column.

To customize cells and columns in the DataGridView control

  1. Derive a new cell class, called DataGridViewRolloverCell, from the xref:System.Windows.Forms.DataGridViewTextBoxCell type.

    [!code-csharpSystem.Windows.Forms.DataGridViewRolloverCell#201] [!code-vbSystem.Windows.Forms.DataGridViewRolloverCell#201]
    [!code-csharpSystem.Windows.Forms.DataGridViewRolloverCell#202] [!code-vbSystem.Windows.Forms.DataGridViewRolloverCell#202]

  2. Override the xref:System.Windows.Forms.DataGridViewTextBoxCell.Paint%2A method in the DataGridViewRolloverCell class. In the override, first call the base class implementation, which handles the hosted text box functionality. Then use the control's xref:System.Windows.Forms.Control.PointToClient%2A method to transform the cursor position (in screen coordinates) to the xref:System.Windows.Forms.DataGridView client area's coordinates. If the mouse coordinates fall within the bounds of the cell, draw the inset rectangle.

    [!code-csharpSystem.Windows.Forms.DataGridViewRolloverCell#210] [!code-vbSystem.Windows.Forms.DataGridViewRolloverCell#210]

  3. Override the xref:System.Windows.Forms.DataGridViewCell.OnMouseEnter%2A and xref:System.Windows.Forms.DataGridViewCell.OnMouseLeave%2A methods in the DataGridViewRolloverCell class to force cells to repaint themselves when the mouse pointer enters or leaves them.

    [!code-csharpSystem.Windows.Forms.DataGridViewRolloverCell#220] [!code-vbSystem.Windows.Forms.DataGridViewRolloverCell#220]

  4. Derive a new class, called DataGridViewRolloverCellColumn, from the xref:System.Windows.Forms.DataGridViewColumn type. In the constructor, assign a new DataGridViewRolloverCell object to its xref:System.Windows.Forms.DataGridViewColumn.CellTemplate%2A property.

    [!code-csharpSystem.Windows.Forms.DataGridViewRolloverCell#300] [!code-vbSystem.Windows.Forms.DataGridViewRolloverCell#300]

Example

The complete code example includes a small test form that demonstrates the behavior of the custom cell type.

[!code-csharpSystem.Windows.Forms.DataGridViewRolloverCell#000] [!code-vbSystem.Windows.Forms.DataGridViewRolloverCell#000]

Compiling the Code

This example requires:

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

See also