Permalink
Fetching contributors…
Cannot retrieve contributors at this time
47 lines (39 sloc) 3.96 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: Disable Buttons in a Button Column in the Windows Forms DataGridView Control
03/30/2017
.net-framework
dotnet-winforms
article
jsharp
data grids, disabling buttons
buttons, disabling in button columns
DataGridView control [Windows Forms], disabling button cells
5c344d01-013a-4a6b-8f8d-62ec9321d81e
15
dotnet-bot
dotnetcontent
wpickett

How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control

The xref:System.Windows.Forms.DataGridView control includes the xref:System.Windows.Forms.DataGridViewButtonCell class for displaying cells with a user interface (UI) like a button. However, xref:System.Windows.Forms.DataGridViewButtonCell does not provide a way to disable the appearance of the button displayed by the cell.

The following code example demonstrates how to customize the xref:System.Windows.Forms.DataGridViewButtonCell class to display buttons that can appear disabled. The example defines a new cell type, DataGridViewDisableButtonCell, that derives from xref:System.Windows.Forms.DataGridViewButtonCell. This cell type provides a new Enabled property that can be set to false to draw a disabled button in the cell. The example also defines a new column type, DataGridViewDisableButtonColumn, that displays DataGridViewDisableButtonCell objects. To demonstrate this new cell and column type, the current value of each xref:System.Windows.Forms.DataGridViewCheckBoxCell in the parent xref:System.Windows.Forms.DataGridView determines whether the Enabled property of the DataGridViewDisableButtonCell in the same row is true or false.

[!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.

Example

[!code-csharpSystem.Windows.Forms.DataGridView.DisabledButtons#0] [!code-vbSystem.Windows.Forms.DataGridView.DisabledButtons#0]

Compiling the Code

This example requires:

  • References to the System, System.Drawing, System.Windows.Forms and System.Windows.Forms.VisualStyles 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

Customizing the Windows Forms DataGridView Control
DataGridView Control Architecture
Column Types in the Windows Forms DataGridView Control