Procedura: eseguire un'azione personalizzata in base alle modifiche apportate a una cella di un controllo DataGridView di Windows FormHow to: Perform a Custom Action Based on Changes in a Cell of a Windows Forms DataGridView Control

Il DataGridView controllo ha un numero di eventi è possibile utilizzare per rilevare le modifiche apportate allo stato di DataGridView celle.The DataGridView control has a number of events you can use to detect changes in the state of DataGridView cells. Due delle più comunemente utilizzate sono il CellValueChanged e CellStateChanged eventi.Two of the most commonly used are the CellValueChanged and CellStateChanged events.

Per rilevare le modifiche nei valori delle celle di DataGridViewTo detect changes in the values of DataGridView cells

  • Scrivere un gestore per il CellValueChanged evento.Write a handler for the CellValueChanged event.

    private void dataGridView1_CellValueChanged(object sender,
        DataGridViewCellEventArgs e)
    {
        string msg = String.Format(
            "Cell at row {0}, column {1} value changed",
            e.RowIndex, e.ColumnIndex);
        MessageBox.Show(msg, "Cell Value Changed");
    }
    
    Private Sub dataGridView1_CellValueChanged(ByVal sender As Object, _
        ByVal e As DataGridViewCellEventArgs) _
        Handles dataGridView1.CellValueChanged
    
        Dim msg As String = String.Format( _
            "Cell at row {0}, column {1} value changed", _
            e.RowIndex, e.ColumnIndex)
        MessageBox.Show(msg, "Cell Value Changed")
    
    End Sub
    

Per rilevare le modifiche degli stati delle celle DataGridViewTo detect changes in the states of DataGridView cells

  • Scrivere un gestore per il CellStateChanged evento.Write a handler for the CellStateChanged event.

    private void dataGridView1_CellStateChanged(object sender,
        DataGridViewCellStateChangedEventArgs e)
    {
        DataGridViewElementStates state = e.StateChanged;
        string msg = String.Format("Row {0}, Column {1}, {2}",
            e.Cell.RowIndex, e.Cell.ColumnIndex, e.StateChanged);
        MessageBox.Show(msg, "Cell State Changed");
    }
    
    Private Sub dataGridView1_CellStateChanged(ByVal sender As Object, _
        ByVal e As DataGridViewCellStateChangedEventArgs) _
        Handles dataGridView1.CellStateChanged
    
        Dim state As DataGridViewElementStates = e.StateChanged
        Dim msg As String = String.Format( _
            "Row {0}, Column {1}, {2}", _
            e.Cell.RowIndex, e.Cell.ColumnIndex, e.StateChanged)
        MessageBox.Show(msg, "Cell State Changed")
    
    End Sub
    

Compilazione del codiceCompiling the Code

L'esempio presenta i requisiti seguenti:This example requires:

Vedere ancheSee Also

DataGridView
DataGridView.CellValueChanged
DataGridView.CellStateChanged
Programmazione con celle, righe e colonne nel controllo DataGridView Windows FormsProgramming with Cells, Rows, and Columns in the Windows Forms DataGridView Control
Procedura dettagliata: convalida di dati nel controllo DataGridView di Windows FormWalkthrough: Validating Data in the Windows Forms DataGridView Control