Questo articolo è stato tradotto automaticamente. Per visualizzare l'articolo in inglese, selezionare la casella di controllo Inglese. È possibile anche visualizzare il testo inglese in una finestra popup posizionando il puntatore del mouse sopra il testo.
Traduzione
Inglese

Evento DataGridView.CellValueChanged

 

Data di pubblicazione: novembre 2016

Si verifica quando il valore di una cella cambia.

Spazio dei nomi:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event DataGridViewCellEventHandler CellValueChanged

The E:System.Windows.Forms.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. To commit the change when the cell is clicked, you must handle the E:System.Windows.Forms.DataGridView.CurrentCellDirtyStateChanged event. In the handler, if the current cell is a check box cell, call the M:System.Windows.Forms.DataGridView.CommitEdit(System.Windows.Forms.DataGridViewDataErrorContexts) method and pass in the F:System.Windows.Forms.DataGridViewDataErrorContexts.Commit value.

Rows in the control are not automatically sorted when a cell value is changed. To sort the control when the user modifies a cell, call the Overload:System.Windows.Forms.DataGridView.Sort method in a E:System.Windows.Forms.DataGridView.CellValueChanged event handler.

For more information about handling events, see NIB: Consuming Events.

The following code example demonstrates how to use the E:System.Windows.Forms.DataGridView.CellValueChanged event to update the values in a balance column of a T:System.Windows.Forms.DataGridView. This example is part of a larger example available in the E:System.Windows.Forms.DataGridView.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();
    }
}

.NET Framework
Disponibile da 2.0
Torna all'inizio
Mostra: