共用方式為


DataGridView.CurrentCellDirtyStateChanged 事件

定義

當儲存格狀態變更是與儲存格內容變更有關時發生。

public:
 event EventHandler ^ CurrentCellDirtyStateChanged;
public event EventHandler CurrentCellDirtyStateChanged;
public event EventHandler? CurrentCellDirtyStateChanged;
member this.CurrentCellDirtyStateChanged : EventHandler 
Public Custom Event CurrentCellDirtyStateChanged As EventHandler 

事件類型

範例

下列程式碼範例示範如何處理 CurrentCellDirtyStateChanged 事件。 在此範例中,事件處理常式會呼叫 CommitEdit 方法來引發 CellValueChanged 事件,並判斷 的目前值 DataGridViewCheckBoxCell 。 此程式碼範例是How to: Disable Buttons in a Button Column in the Windows Forms DataGridView Control中所提供的較大範例的一部分。

// This event handler manually raises the CellValueChanged event
// by calling the CommitEdit method.
void dataGridView1_CurrentCellDirtyStateChanged(object sender,
    EventArgs e)
{
    if (dataGridView1.IsCurrentCellDirty)
    {
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
    }
}

// If a check box cell is clicked, this event handler disables  
// or enables the button in the same row as the clicked cell.
public void dataGridView1_CellValueChanged(object sender,
    DataGridViewCellEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "CheckBoxes")
    {
        DataGridViewDisableButtonCell buttonCell =
            (DataGridViewDisableButtonCell)dataGridView1.
            Rows[e.RowIndex].Cells["Buttons"];

        DataGridViewCheckBoxCell checkCell =
            (DataGridViewCheckBoxCell)dataGridView1.
            Rows[e.RowIndex].Cells["CheckBoxes"];
        buttonCell.Enabled = !(Boolean)checkCell.Value;

        dataGridView1.Invalidate();
    }
}
' This event handler manually raises the CellValueChanged event
' by calling the CommitEdit method.
Sub dataGridView1_CurrentCellDirtyStateChanged( _
    ByVal sender As Object, ByVal e As EventArgs) _
    Handles dataGridView1.CurrentCellDirtyStateChanged

    If dataGridView1.IsCurrentCellDirty Then
        dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit)
    End If
End Sub

' If a check box cell is clicked, this event handler disables  
' or enables the button in the same row as the clicked cell.
Public Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles dataGridView1.CellValueChanged

    If dataGridView1.Columns(e.ColumnIndex).Name = "CheckBoxes" Then
        Dim buttonCell As DataGridViewDisableButtonCell = _
            CType(dataGridView1.Rows(e.RowIndex).Cells("Buttons"), _
            DataGridViewDisableButtonCell)

        Dim checkCell As DataGridViewCheckBoxCell = _
            CType(dataGridView1.Rows(e.RowIndex).Cells("CheckBoxes"), _
            DataGridViewCheckBoxCell)
        buttonCell.Enabled = Not CType(checkCell.Value, [Boolean])

        dataGridView1.Invalidate()
    End If
End Sub

備註

如果儲存格的內容已變更,但尚未儲存變更,則儲存格會標示為已修改。

此事件通常會發生在資料格已編輯,但變更尚未認可至資料快取,或取消編輯作業時發生。

如需如何處理事件的詳細資訊,請參閱 處理和引發事件

適用於

另請參閱