DataGridView.CellValueChanged 이벤트

정의

셀 값이 변경될 때 발생합니다.

public:
 event System::Windows::Forms::DataGridViewCellEventHandler ^ CellValueChanged;
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에서 값을 업데이트 하는 이벤트입니다. 이 예제는에서 사용할 수 있는 보다 큰 예제의 일부는 SelectionChanged 이벤트입니다.

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 이벤트는 일반적으로 포커스가 셀을 떠날 때 발생하는 사용자 지정 값이 커밋될 때 발생합니다.

그러나 검사 상자 셀의 경우 일반적으로 변경 사항을 즉시 처리하려고 합니다. 셀을 클릭할 때 변경 내용을 커밋하려면 DataGridView.CurrentCellDirtyStateChanged 이벤트를 처리해야 합니다. 처리기에서 현재 셀이 확인란 셀인 경우 DataGridView.CommitEdit 메서드를 호출하고 Commit 값을 전달합니다.

셀 값이 변경되면 컨트롤의 행이 자동으로 정렬되지 않습니다. 사용자가 셀을 수정할 때 컨트롤을 정렬하려면 이벤트 처리기에서 메서드를 CellValueChanged 호출 Sort 합니다.

이벤트를 처리 하는 방법에 대 한 자세한 내용은 참조 하세요. 이벤트 처리 및 발생합니다.

적용 대상

추가 정보