How to: Perform a Custom Action Based on Changes in a Cell of a Windows Forms DataGridView Control

The DataGridView control has a number of events you can use to detect changes in the state of DataGridView cells. Two of the most commonly used are the CellValueChanged and CellStateChanged events.

To detect changes in the values of DataGridView cells

  • 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");

To detect changes in the states of DataGridView cells

  • 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");

This example requires: