DataGridView.CancelRowEdit Событие

Определение

Происходит, когда свойство VirtualMode элемента управления DataGridView имеет значение true, и отменяет изменения в строке.

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 

Тип события

Примеры

В следующем примере кода показано, как обрабатывать это событие для элемента управления в виртуальном DataGridView режиме. Если элемент управления находится в режиме редактирования, rowInEdit переменная содержит индекс редактируемой строки, а customerInEdit переменная содержит ссылку на объект Customer, соответствующий этой строке. Когда пользователь отменяет выход из режима редактирования, этот объект может быть удален. Если строка, редактируемая пользователем, является строкой для новых записей, старый объект Customer удаляется и заменяется новым, чтобы пользователь снова начал вносить изменения. Этот пример является частью более крупного примера, доступного в разделе Пошаговое руководство. Реализация виртуального режима в элементе управления Windows Forms DataGridView.

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

Комментарии

Когда находится в виртуальном DataGridView режиме, изменения по умолчанию фиксируются в кэше данных на уровне ячейки. Событие CancelRowEdit можно использовать при реализации транзакций на уровне строк.

Дополнительные сведения об обработке событий см. в разделе Обработка и вызов событий.

Применяется к

См. также раздел