Share via


Postupy: Provedení vlastní akce na základě změn v buňce ovládacího prvku Windows Forms DataGridView

Ovládací DataGridView prvek obsahuje řadu událostí, které můžete použít k detekci změn ve stavu DataGridView buněk. Dvě z nejčastěji používaných událostí jsou CellValueChanged a CellStateChanged události.

Detekce změn v hodnotách buněk DataGridView

  • Napište obslužnou rutinu CellValueChanged události.

    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
    

Detekce změn ve stavech buněk DataGridView

  • Napište obslužnou rutinu CellStateChanged události.

    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
    

Probíhá kompilace kódu

Tento příklad vyžaduje:

  • Ovládací DataGridView prvek s názvem dataGridView1. V jazyce C# musí být obslužné rutiny událostí připojené k odpovídajícím událostem.

  • Odkazy na sestavení System a System.Windows.Forms sestavení.

Viz také