| 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: Specify the Edit Mode for the Windows Forms DataGridView Control |
03/30/2017 |
.net-framework |
|
article |
|
|
93e117e8-94c4-411b-ba31-645e475ed85c |
17 |
dotnet-bot |
dotnetcontent |
wpickett |
How to: Specify the Edit Mode for the Windows Forms DataGridView Control
By default, users can edit the contents of the current xref:System.Windows.Forms.DataGridView text box cell by typing in it or pressing F2. This puts the cell in edit mode if all of the following conditions are met:
-
The underlying data source supports editing.
-
The xref:System.Windows.Forms.DataGridView control is enabled.
-
The xref:System.Windows.Forms.DataGridView.EditMode%2A property value is not xref:System.Windows.Forms.DataGridViewEditMode.EditProgrammatically.
-
The
ReadOnlyproperties of the cell, row, column, and control are all set tofalse.
In edit mode, the user can change the cell value and press ENTER to commit the change or ESC to revert the cell to its original value.
You can configure a xref:System.Windows.Forms.DataGridView control so that a cell enters edit mode as soon as it becomes the current cell. The behavior of the ENTER and ESC keys is unchanged in this case, but the cell remains in edit mode after the value is committed or reverted. You can also configure the control so that cells enter edit mode only when users type in the cell or only when users press F2. Finally, you can prevent cells from entering edit mode except when you call the xref:System.Windows.Forms.DataGridView.BeginEdit%2A method.
To change the edit mode of a DataGridView control
-
Set the xref:System.Windows.Forms.DataGridView.EditMode%2A?displayProperty=nameWithType property to the appropriate xref:System.Windows.Forms.DataGridViewEditMode enumeration.
[!code-csharpSystem.Windows.Forms.DataGridViewMisc#067] [!code-vbSystem.Windows.Forms.DataGridViewMisc#067]
Compiling the Code
This example requires:
-
A xref:System.Windows.Forms.DataGridView control named
dataGridView1. -
References to the xref:System and xref:System.Windows.Forms assemblies.
See Also
xref:System.Windows.Forms.DataGridView
xref:System.Windows.Forms.DataGridView.EditMode%2A?displayProperty=nameWithType
Data Entry in the Windows Forms DataGridView Control