Partage via


DataGridView.CancelRowEdit Événement

Définition

Se produit lorsque la propriété VirtualMode d’un contrôle DataGridView est true et que l’utilisateur annule les modifications dans une ligne.

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 

Type d'événement

Exemples

L’exemple de code suivant montre comment gérer cet événement pour un contrôle DataGridView en mode virtuel. Lorsque le contrôle est en mode édition, la variable rowInEdit contient l’index de la ligne en cours de modification et la variable customerInEdit contient une référence à un objet Customer correspondant à cette ligne. Lorsque l’utilisateur annule le mode d’édition, cet objet peut être ignoré. Si la ligne que l’utilisateur a modifiée est la ligne des nouveaux enregistrements, toutefois, l’ancien objet Customer est ignoré et remplacé par un nouvel objet afin que l’utilisateur puisse recommencer à apporter des modifications. Cet exemple fait partie d’un exemple plus large disponible dans procédure pas à pas : implémentation du mode virtuel dans le contrôle 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

Remarques

Lorsque le DataGridView est en mode virtuel, les modifications sont validées dans le cache de données au niveau de la cellule par défaut. L’événement CancelRowEdit peut être utilisé lors de l’implémentation de transactions au niveau des lignes.

Pour plus d’informations sur la gestion des événements, consultez Gestion et déclenchement d’événements.

S’applique à

Voir aussi