Expand Minimize

DataGridView.RowsRemoved Event

Occurs when a row or rows are deleted from the DataGridView.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event DataGridViewRowsRemovedEventHandler RowsRemoved

When rows are deleted from a DataGridView control, the index numbers of subsequent rows are lowered to compensate.

For more information about handling events, see Consuming Events.

The following code example demonstrates how to use this event to update the values in a balance column of a DataGridView. This example is part of a larger example available in the SelectionChanged event.

private void DataGridView1_CellValueChanged(
    object sender, DataGridViewCellEventArgs e)
    // Update the balance column whenever the value of any cell changes.

private void DataGridView1_RowsRemoved(
    object sender, DataGridViewRowsRemovedEventArgs e)
    // Update the balance column whenever rows are deleted.

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);
        deposit = 0;
        withdrawal = 0;
        balance = int.Parse(DataGridView1.Rows[counter - 1]

        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]

        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value != null)
            if (DataGridView1.Rows[counter]
                .Cells["Withdrawals"].Value.ToString().Length != 0)
                withdrawal = int.Parse(DataGridView1.Rows[counter]
        DataGridView1.Rows[counter].Cells["Balance"].Value =
            (balance + deposit + withdrawal).ToString();

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

© 2015 Microsoft