This documentation is archived and is not being maintained.

DataGrid.BeginEdit Method

Attempts to put the grid into a state where editing is allowed.

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

public bool BeginEdit(
	DataGridColumnStyle gridColumn,
	int rowNumber


Type: System.Windows.Forms.DataGridColumnStyle
A DataGridColumnStyle to edit.
Type: System.Int32
The number of the row to edit.

Return Value

Type: System.Boolean
true if the method is successful; otherwise, false.


IDataGridEditingService.BeginEdit(DataGridColumnStyle, Int32)

The grid will deny edit requests if the user already started typing into a cell. In that case, the BeginEdit method will return false.

The following code example uses the BeginEdit method to test if editing is possible before changing a specified column and row.

private void EditGrid(DataGrid dataGrid1){
    // Get the selected row and column through the CurrentCell.
    int colNum;
    int rowNum;
    colNum = dataGrid1.CurrentCell.ColumnNumber;
    rowNum = dataGrid1.CurrentCell.RowNumber;
    // Get the selected DataGridColumnStyle.
    DataGridColumnStyle dgCol;
    dgCol = dataGrid1.TableStyles[0].GridColumnStyles[colNum];
    // Invoke the BeginEdit method to see if editing can begin.
    if (dataGrid1.BeginEdit(dgCol, rowNum)){
       // Edit row value. Get the DataTable and selected row.
       DataTable myTable;
       DataRow myRow;
       // Assuming the DataGrid is bound to a DataTable.
       myTable = (DataTable) dataGrid1.DataSource;
       myRow = myTable.Rows[rowNum];
       // Invoke the Row object's BeginEdit method.
       myRow[colNum] = "New Value";
       // You must accept changes on both DataRow and DataTable.
       dataGrid1.EndEdit(dgCol, rowNum, false);
      Console.WriteLine("BeginEdit failed");

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.