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.

適用対象

こちらもご覧ください