DataRow.BeginEdit Método

Definição

Inicia uma operação de edição num DataRow objeto.

public:
 void BeginEdit();
public void BeginEdit();
member this.BeginEdit : unit -> unit
Public Sub BeginEdit ()

Exceções

O método foi chamado dentro do RowChanging evento.

O método era chamado para uma linha eliminada.

Exemplos

O exemplo cria um simples DataTable com um DataColumn e cinco DataRow objetos, e um UniqueConstraint. Também é adicionado um RowChanged gestor de eventos para monitorizar quando o valor da linha está a mudar. Após invocação BeginEdit nas linhas existentes, a restrição e o evento são temporariamente desativados e os valores originais e propostos são impressos. O BeginEdit é novamente invocado para definir duas linhas com o mesmo valor. Quando EndEdit é chamado, o UniqueConstraint é aplicado nos valores idênticos.

private void DemonstrateRowBeginEdit()
{
    DataTable table = new DataTable("table1");
    DataColumn column = new
        DataColumn("col1",Type.GetType("System.Int32"));
    table.RowChanged+=new
        DataRowChangeEventHandler(Row_Changed);
    table.Columns.Add(column);

    // Add a UniqueConstraint to the table.
    table.Constraints.Add(new UniqueConstraint(column));

    // Add five rows.
    DataRow newRow;

    for(int i = 0;i<5; i++)
    {
        // RowChanged event will occur for every addition.
        newRow= table.NewRow();
        newRow[0]= i;
        table.Rows.Add(newRow);
    }
    // AcceptChanges.
    table.AcceptChanges();

    // Invoke BeginEdit on each.
    Console.WriteLine(
        "\n Begin Edit and print original and proposed values \n");
    foreach(DataRow row in table.Rows)
    {

        row.BeginEdit();
        row[0]=(int) row[0]+10;
        Console.Write("\table Original \table" +
            row[0, DataRowVersion.Original]);
        Console.Write("\table Proposed \table" +
            row[0,DataRowVersion.Proposed] + "\n");
    }
    Console.WriteLine("\n");
    // Accept changes
    table.AcceptChanges();
    // Change two rows to identical values after invoking BeginEdit.
    table.Rows[0].BeginEdit();
    table.Rows[1].BeginEdit();
    table.Rows[0][0]= 100;
    table.Rows[1][0]=100;
    try
    {
        /* Now invoke EndEdit. This will cause the UniqueConstraint
           to be enforced.*/
        table.Rows[0].EndEdit();
        table.Rows[1].EndEdit();
    }
    catch(Exception e)
    {
        // Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.",
            e.GetType());
    }
}

private void Row_Changed(object sender,
    System.Data.DataRowChangeEventArgs e)
{
    DataTable table = (DataTable)  sender;
    Console.WriteLine("RowChanged " + e.Action.ToString()
        + "\table" + e.Row.ItemArray[0]);
}
Private Sub DemonstrateRowBeginEdit()
    Dim table As New DataTable("table1")
    Dim column As New DataColumn("col1", Type.GetType("System.Int32"))
    AddHandler table.RowChanged, AddressOf Row_Changed
    table.Columns.Add(column)

    ' Add a UniqueConstraint to the table.
    table.Constraints.Add(New UniqueConstraint(column))

    ' Add five rows.
    Dim newRow As DataRow
       
    Dim i As Integer
    For i = 0 To 4
        ' RowChanged event will occur for every addition.
        newRow = table.NewRow()
        newRow(0) = i
        table.Rows.Add(newRow)
    Next i

    ' AcceptChanges.
    table.AcceptChanges()

    ' Invoke BeginEdit on each.
    Console.WriteLine(ControlChars.Cr _
       & " Begin Edit and print original and proposed values " _
       & ControlChars.Cr)
    Dim row As DataRow
    For Each row In  table.Rows           
        row.BeginEdit()
        row(0) = CInt(row(0)) & 10
        Console.Write(ControlChars.Tab & " Original " & ControlChars.Tab _
           & row(0, DataRowVersion.Original).ToString())
        Console.Write(ControlChars.Tab & " Proposed " & ControlChars.Tab _
           & row(0, DataRowVersion.Proposed).ToString() & ControlChars.Cr)
    Next row
    Console.WriteLine(ControlChars.Cr)

    ' Accept changes
    table.AcceptChanges()

    ' Change two rows to identical values after invoking BeginEdit.
    table.Rows(0).BeginEdit()
    table.Rows(1).BeginEdit()
    table.Rows(0)(0) = 100
    table.Rows(1)(0) = 100
    Try
        ' Now invoke EndEdit. This will cause the UniqueConstraint
        ' to be enforced.
        table.Rows(0).EndEdit()
        table.Rows(1).EndEdit()
    Catch e As Exception
    ' Process exception and return.
        Console.WriteLine("Exception of type {0} occurred.", _
           e.GetType().ToString())
    End Try
End Sub

Private Sub Row_Changed _
(sender As Object, e As System.Data.DataRowChangeEventArgs)
    Dim table As DataTable = CType(sender, DataTable)
    Console.WriteLine("RowChanged " & e.Action.ToString() _
       & ControlChars.Tab & e.Row.ItemArray(0).ToString())
End Sub

Observações

Use o BeginEdit método para colocar um DataRow modo de edição. Neste modo, os eventos são temporariamente suspensos, permitindo ao utilizador fazer alterações em mais do que uma linha sem ativar regras de validação. Por exemplo, se tiver de garantir que o valor da coluna para um montante total é igual aos valores das colunas de débito e crédito numa linha, pode colocar cada linha em modo de edição para suspender a validação dos valores das linhas até que o utilizador tente confirmar os valores.

O BeginEdit método é chamado implicitamente quando o utilizador altera o valor de um controlo data-bound; o EndEdit método é chamado implicitamente quando se invoca o AcceptChanges método para o DataTable objeto. Enquanto está neste modo de edição, armazena DataRow representações dos valores originais e novos propostos. Portanto, desde que o EndEdit método não tenha sido chamado, pode recuperar a versão original ou proposta passando um ou DataRowVersion.OriginalDataRowVersion.Proposed para o version parâmetro da Item[] propriedade. Também pode cancelar quaisquer edições neste momento invocando o CancelEdit método.

Para verificar se a linha contém um valor original ou proposto, chame o HasVersion método.

Note

O BeginEdit método suspende RowChanging temporariamente os eventos, mas a delete operação não.

Aplica-se a

Ver também