DataGridView.CellValueChanged DataGridView.CellValueChanged DataGridView.CellValueChanged DataGridView.CellValueChanged Event

定義

儲存格的值變更時發生。Occurs when the value of a cell changes.

public:
 event System::Windows::Forms::DataGridViewCellEventHandler ^ CellValueChanged;
public event System.Windows.Forms.DataGridViewCellEventHandler CellValueChanged;
member this.CellValueChanged : System.Windows.Forms.DataGridViewCellEventHandler 
Public Custom Event CellValueChanged As DataGridViewCellEventHandler 

範例

下列程式碼範例示範如何使用CellValueChanged事件來更新餘額資料行DataGridView中的值。The following code example demonstrates how to use the CellValueChanged event to update the values in a balance column of a DataGridView. 這個範例是SelectionChanged事件中所提供的較大範例的一部分。This example is part of a larger example available in the SelectionChanged event.

private void DataGridView1_CellValueChanged(
    object sender, DataGridViewCellEventArgs e)
{
    // Update the balance column whenever the value of any cell changes.
    UpdateBalance();
}

private void DataGridView1_RowsRemoved(
    object sender, DataGridViewRowsRemovedEventArgs e)
{
    // Update the balance column whenever rows are deleted.
    UpdateBalance();
}

private void UpdateBalance()
{
    int counter;
    int balance;
    int deposit;
    int withdrawal;

    // Iterate through the rows, skipping the Starting Balance row.
    for (counter = 1; counter < (DataGridView1.Rows.Count - 1);
        counter++)
    {
        deposit = 0;
        withdrawal = 0;
        balance = int.Parse(DataGridView1.Rows[counter - 1]
            .Cells["Balance"].Value.ToString());

        if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null)
        {
            // Verify that the cell value is not an empty string.
            if (DataGridView1.Rows[counter]
                .Cells["Deposits"].Value.ToString().Length != 0)
            {
                deposit = int.Parse(DataGridView1.Rows[counter]
                    .Cells["Deposits"].Value.ToString());
            }
        }

        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value != null)
        {
            if (DataGridView1.Rows[counter]
                .Cells["Withdrawals"].Value.ToString().Length != 0)
            {
                withdrawal = int.Parse(DataGridView1.Rows[counter]
                    .Cells["Withdrawals"].Value.ToString());
            }
        }
        DataGridView1.Rows[counter].Cells["Balance"].Value =
            (balance + deposit + withdrawal).ToString();
    }
}
Private Sub CellValueChanged(ByVal sender As Object, _
    ByVal e As DataGridViewCellEventArgs) _
    Handles DataGridView1.CellValueChanged

    ' Update the balance column whenever the values of any cell changes.
    UpdateBalance()
End Sub

Private Sub RowsRemoved(ByVal sender As Object, _
    ByVal e As DataGridViewRowsRemovedEventArgs) _
    Handles DataGridView1.RowsRemoved

    ' Update the balance column whenever rows are deleted.
    UpdateBalance()
End Sub

Private Sub UpdateBalance()
    Dim counter As Integer
    Dim balance As Integer
    Dim deposit As Integer
    Dim withdrawal As Integer

    ' Iterate through the rows, skipping the Starting Balance Row.
    For counter = 1 To (DataGridView1.Rows.Count - 2)
        deposit = 0
        withdrawal = 0
        balance = Integer.Parse(DataGridView1.Rows(counter - 1) _
            .Cells("Balance").Value.ToString())

        If Not DataGridView1.Rows(counter) _
            .Cells("Deposits").Value Is Nothing Then

            ' Verify that the cell value is not an empty string.
            If Not DataGridView1.Rows(counter) _
                .Cells("Deposits").Value.ToString().Length = 0 Then
                deposit = Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Deposits").Value.ToString())
            End If
        End If

        If Not DataGridView1.Rows(counter) _
            .Cells("Withdrawals").Value Is Nothing Then
            If Not DataGridView1.Rows(counter) _
                .Cells("Withdrawals").Value.ToString().Length = 0 Then
                withdrawal = Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Withdrawals").Value.ToString())
            End If
        End If

        DataGridView1.Rows(counter).Cells("Balance").Value = _
            (balance + deposit + withdrawal).ToString()
    Next
End Sub

備註

認可DataGridView.CellValueChanged使用者指定的值時,就會發生此事件,這通常會在焦點離開儲存格時發生。The DataGridView.CellValueChanged event occurs when the user-specified value is committed, which typically occurs when focus leaves the cell.

不過,在核取方塊儲存格的情況下,您通常會想要立即處理變更。In the case of check box cells, however, you will typically want to handle the change immediately. 若要在按一下資料格時認可變更,您必須處理DataGridView.CurrentCellDirtyStateChanged事件。To commit the change when the cell is clicked, you must handle the DataGridView.CurrentCellDirtyStateChanged event. 在處理常式中,如果當前儲存格是核取方塊資料格,請DataGridView.CommitEdit呼叫方法並傳入Commit值。In the handler, if the current cell is a check box cell, call the DataGridView.CommitEdit method and pass in the Commit value.

變更資料格值時,不會自動排序控制項中的資料列。Rows in the control are not automatically sorted when a cell value is changed. 若要在使用者修改資料格時排序控制項,請Sort CellValueChanged在事件處理常式中呼叫方法。To sort the control when the user modifies a cell, call the Sort method in a CellValueChanged event handler.

如需如何處理事件的詳細資訊,請參閱處理和引發事件For more information about how to handle events, see Handling and Raising Events.

適用於

另請參閱