DataGrid.BeginEdit Method (DataGridColumnStyle, Int32)

 

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
)

Parameters

gridColumn
Type: System.Windows.Forms.DataGridColumnStyle

A DataGridColumnStyle to edit.

rowNumber
Type: System.Int32

The number of the row to edit.

Return Value

Type: System.Boolean

true if the method is successful; otherwise, false.

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.BeginEdit();
       myRow[colNum] = "New Value";
       // You must accept changes on both DataRow and DataTable.
       myRow.AcceptChanges();
       myTable.AcceptChanges();
       dataGrid1.EndEdit(dgCol, rowNum, false);
    }
    else{
      Console.WriteLine("BeginEdit failed");
    }
 }

.NET Framework
Available since 1.1
Return to top
Show: