| 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: Host Controls in Windows Forms DataGridView Cells |
03/30/2017 |
.net-framework |
|
article |
|
|
e79a9d4e-64ec-41f5-93ec-f5492633cbb2 |
10 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Host Controls in Windows Forms DataGridView Cells
The xref:System.Windows.Forms.DataGridView control provides several column types, enabling your users to enter and edit values in a variety of ways. If these column types do not meet your data-entry needs, however, you can create your own column types with cells that host controls of your choosing. To do this, you must define classes that derive from xref:System.Windows.Forms.DataGridViewColumn and xref:System.Windows.Forms.DataGridViewCell. You must also define a class that derives from xref:System.Windows.Forms.Control and implements the xref:System.Windows.Forms.IDataGridViewEditingControl interface.
The following code example shows how to create a calendar column. The cells of this column display dates in ordinary text box cells, but when the user edits a cell, a xref:System.Windows.Forms.DateTimePicker control appears. In order to avoid having to implement text box display functionality again, the CalendarCell class derives from the xref:System.Windows.Forms.DataGridViewTextBoxCell class rather than inheriting the xref:System.Windows.Forms.DataGridViewCell class directly.
[!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
Clonemethod to copy the new properties during cloning operations. You should also call the base class'sClonemethod so that the properties of the base class are copied to the new cell or column.
Example
[!code-csharpSystem.Windows.Forms.DataGridViewCalendarColumn#000] [!code-vbSystem.Windows.Forms.DataGridViewCalendarColumn#000]
Compiling the Code
The following example requires:
- References to the System 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.DataGridViewColumn
xref:System.Windows.Forms.DataGridViewCell
xref:System.Windows.Forms.DataGridViewTextBoxCell
xref:System.Windows.Forms.IDataGridViewEditingControl
xref:System.Windows.Forms.DateTimePicker
Customizing the Windows Forms DataGridView Control
DataGridView Control Architecture
Column Types in the Windows Forms DataGridView Control