Bagikan melalui


How to: Specify the Edit Mode for the Windows Forms DataGridView Control

By default, users can edit the contents of the current 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 DataGridView control is enabled.

  • The EditMode property value is not EditProgrammatically.

  • The ReadOnly properties of the cell, row, column, and control are all set to false.

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 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 BeginEdit method.

To change the edit mode of a DataGridView control

  • Set the DataGridView.EditMode property to the appropriate DataGridViewEditMode enumeration.

    Me.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter
    
    this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter;
    

Compiling the Code

This example requires:

See Also

Reference

DataGridView

DataGridView.EditMode

Other Resources

Data Entry in the Windows Forms DataGridView Control