DataGrid.BeginEdit 方法

试图将网格置于允许编辑的状态。

**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)

语法

声明
Public Function BeginEdit ( _
    gridColumn As DataGridColumnStyle, _
    rowNumber As Integer _
) As Boolean
用法
Dim instance As DataGrid
Dim gridColumn As DataGridColumnStyle
Dim rowNumber As Integer
Dim returnValue As Boolean

returnValue = instance.BeginEdit(gridColumn, rowNumber)
public bool BeginEdit (
    DataGridColumnStyle gridColumn,
    int rowNumber
)
public:
virtual bool BeginEdit (
    DataGridColumnStyle^ gridColumn, 
    int rowNumber
) sealed
public final boolean BeginEdit (
    DataGridColumnStyle gridColumn, 
    int rowNumber
)
public final function BeginEdit (
    gridColumn : DataGridColumnStyle, 
    rowNumber : int
) : boolean

参数

  • rowNumber
    要编辑行的行号。

返回值

如果此方法成功,则为 true;否则为 false

备注

如果用户已经开始向单元格中键入信息,则网格将拒绝编辑请求。在这种情况下,BeginEdit 方式将返回 false

示例

下面的代码示例在更改指定列和行之前,先使用 BeginEdit 方法来测试是否可以进行编辑。

Private Sub EditGrid(dataGrid1 As DataGrid)
    ' Get the selected row and column through the CurrentCell.
    Dim colNum As Integer
    Dim rowNum As Integer
    colNum = dataGrid1.CurrentCell.ColumnNumber
    rowNum = dataGrid1.CurrentCell.RowNumber
    ' Get the selected DataGridColumnStyle.
    Dim dgCol As DataGridColumnStyle
    dgCol = dataGrid1.TableStyles(0).GridColumnStyles(colNum)
    ' Invoke the BeginEdit method to see if editing can begin.
    If dataGrid1.BeginEdit(dgCol, rowNum) Then
        ' Edit row value. Get the DataTable and selected row.
        Dim myTable As DataTable
        Dim myRow As DataRow
        ' Assuming the DataGrid is bound to a DataTable.
        myTable = CType(dataGrid1.DataSource, DataTable)
        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")
    End If
End Sub 'EditGrid
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");
    }
 }
 
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" );
      }
   }
private void EditGrid(DataGrid myGrid)
{
    // Get the selected row and column through the CurrentCell.
    int colNum;
    int rowNum;

    colNum = dataGrid1.get_CurrentCell().get_ColumnNumber();
    rowNum = dataGrid1.get_CurrentCell().get_RowNumber();
    // Get the selected DataGridColumnStyle.
    DataGridColumnStyle dgCol;
    dgCol = dataGrid1.get_TableStyles().get_Item(0).get_GridColumnStyles().
        get_Item(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.get_DataSource());
        myRow = myTable.get_Rows().get_Item(rowNum);
        // Invoke the Row object's BeginEdit method.
        myRow.BeginEdit();
        myRow.set_Item(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");
    }
} //EditGrid

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

DataGrid 类
DataGrid 成员
System.Windows.Forms 命名空间
EndEdit