DataGridView.CancelRowEdit 事件
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
發生於 DataGridView 控件的 VirtualMode 屬性 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 事件。
如需如何處理事件的詳細資訊,請參閱 處理和引發事件。