Export (0) Print
Expand All
Expand Minimize

DataGridView.EditMode Property

Gets or sets a value indicating how to begin editing a cell.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Property EditMode As DataGridViewEditMode

Property Value

Type: System.Windows.Forms.DataGridViewEditMode
One of the DataGridViewEditMode values. The default is EditOnKeystrokeOrF2.

ExceptionCondition
InvalidEnumArgumentException

The specified value when setting this property is not a valid DataGridViewEditMode value.

Exception

The specified value when setting this property would cause the control to enter edit mode, but initialization of the editing cell value failed and either there is no handler for the DataError event or the handler has set the DataGridViewDataErrorEventArgs.ThrowException property to true. The exception object can typically be cast to type FormatException.

All DataGridViewEditMode values except for EditProgrammatically also allow a user to double-click a cell to begin editing it.

The following code example demonstrates how to initialize a simple data-bound DataGridView. It also demonstrates how to set the EditMode property.

Imports System.Data
Imports System.Data.SqlClient
Imports System.Windows.Forms
Imports System.Drawing
Imports System

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private WithEvents dataGridView1 As New DataGridView()
    Private bindingSource1 As New BindingSource()

    Public Sub New()

        Me.dataGridView1.Dock = DockStyle.Fill
        Me.Controls.Add(Me.dataGridView1)
        InitializeDataGridView()

    End Sub 

    Private Sub InitializeDataGridView()
        Try 
            ' Set up the DataGridView. 
            With Me.dataGridView1
                ' Automatically generate the DataGridView columns.
                .AutoGenerateColumns = True 

                ' Set up the data source.
                bindingSource1.DataSource = GetData("Select * From Products")
                .DataSource = bindingSource1

                ' Automatically resize the visible rows.
                .AutoSizeRowsMode = _
                    DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders

                ' Set the DataGridView control's border.
                .BorderStyle = BorderStyle.Fixed3D

                ' Put the cells in edit mode when user enters them.
                .EditMode = DataGridViewEditMode.EditOnEnter
            End With 
        Catch ex As SqlException
            MessageBox.Show("To run this sample replace " _
                & "connection.ConnectionString with a valid connection string" _
                & "  to a Northwind database accessible to your system.", _
                "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            System.Threading.Thread.CurrentThread.Abort()
        End Try 
    End Sub 

    Private Shared Function GetData(ByVal sqlCommand As String) _
        As DataTable

        Dim connectionString As String = _
            "Integrated Security=SSPI;Persist Security Info=False;" _
            & "Initial Catalog=Northwind;Data Source=localhost" 

        Dim northwindConnection As SqlConnection = _
            New SqlConnection(connectionString)

        Dim command As New SqlCommand(sqlCommand, northwindConnection)
        Dim adapter As SqlDataAdapter = New SqlDataAdapter()
        adapter.SelectCommand = command

        Dim table As New DataTable
        table.Locale = System.Globalization.CultureInfo.InvariantCulture
        adapter.Fill(table)

        Return table

    End Function

    <STAThreadAttribute()> _
    Public Shared Sub Main()
        Application.Run(New Form1)
    End Sub 

End Class

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft