DataGridView.CancelRowEdit Evento

Definizione

Si verifica quando la proprietà VirtualMode di un controllo DataGridView è true ed è necessario annullare le modifiche apportate in una riga.

public:
 event System::Windows::Forms::QuestionEventHandler ^ CancelRowEdit;
public event System.Windows.Forms.QuestionEventHandler CancelRowEdit;
public event System.Windows.Forms.QuestionEventHandler? CancelRowEdit;
member this.CancelRowEdit : System.Windows.Forms.QuestionEventHandler 
Public Custom Event CancelRowEdit As QuestionEventHandler 

Tipo evento

Esempio

Nell'esempio di codice seguente viene illustrato come gestire questo evento per un DataGridView controllo in modalità virtuale. Quando il controllo è in modalità di modifica, la rowInEdit variabile contiene l'indice della riga da modificare e la customerInEdit variabile contiene un riferimento a un oggetto Customer corrispondente a tale riga. Quando l'utente annulla la modalità di modifica, questo oggetto può essere rimosso. Se la riga che l'utente stava modificando è la riga per i nuovi record, tuttavia, l'oggetto Customer precedente viene rimosso e sostituito con uno nuovo in modo che l'utente possa iniziare a modificare nuovamente. Questo esempio fa parte di un esempio più ampio disponibile in Procedura dettagliata: Implementazione della modalità virtuale nel controllo DataGridView Windows Forms.

void dataGridView1_CancelRowEdit( Object^ /*sender*/,
    System::Windows::Forms::QuestionEventArgs^ /*e*/ )
{
   if ( this->rowInEdit == this->dataGridView1->Rows->Count - 2 &&
        this->rowInEdit == this->customers->Count )
   {
      
      // If the user has canceled the edit of a newly created row, 
      // replace the corresponding Customer object with a new, empty one.
      this->customerInEdit = gcnew Customer;
   }
   else
   {
      
      // If the user has canceled the edit of an existing row, 
      // release the corresponding Customer object.
      this->customerInEdit = nullptr;
      this->rowInEdit = -1;
   }
}
private void dataGridView1_CancelRowEdit(object sender,
    System.Windows.Forms.QuestionEventArgs e)
{
    if (this.rowInEdit == this.dataGridView1.Rows.Count - 2 &&
        this.rowInEdit == this.customers.Count)
    {
        // If the user has canceled the edit of a newly created row, 
        // replace the corresponding Customer object with a new, empty one.
        this.customerInEdit = new Customer();
    }
    else
    {
        // If the user has canceled the edit of an existing row, 
        // release the corresponding Customer object.
        this.customerInEdit = null;
        this.rowInEdit = -1;
    }
}
Private Sub dataGridView1_CancelRowEdit(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.QuestionEventArgs) _
    Handles dataGridView1.CancelRowEdit

    If Me.rowInEdit = Me.dataGridView1.Rows.Count - 2 AndAlso _
        Me.rowInEdit = Me.customers.Count Then

        ' If the user has canceled the edit of a newly created row, 
        ' replace the corresponding Customer object with a new, empty one.
        Me.customerInEdit = New Customer()

    Else

        ' If the user has canceled the edit of an existing row, 
        ' release the corresponding Customer object.
        Me.customerInEdit = Nothing
        Me.rowInEdit = -1

    End If

End Sub

Commenti

Quando è DataGridView in modalità virtuale, per impostazione predefinita viene eseguito il commit delle modifiche alla cache dei dati a livello di cella. L'evento CancelRowEdit può essere usato quando si implementano transazioni a livello di riga.

Per altre informazioni su come gestire gli eventi, vedere la gestione e generazione di eventi.

Si applica a

Vedi anche