ExtensionMethod.NET Home of 819 C#, Visual Basic, F# and Javascript extension methods

RemoveSelectedRows

Removes all selected rows from datagridview and returns the response on success

Source

public static bool RemoveSelectedRows(this DataGridView dgv)
{
    try
    {
        if (dgv.RowCount > 0)
        {
            if (dgv.SelectedRows.Count == dgv.Rows.Count)
            {
                dgv.DataSource = null;
            }

            foreach (DataGridViewRow row in dgv.SelectedRows)
            {
                dgv.Rows.Remove(row);
            }
        }

        return true;
    }
    catch (Exception exc)
    {
        return false;
    }
}

Example

DataGridView dgvCaseList = new DataGridView();
dgvCase.RemoveSelectedRows();

Author: Pursanov Dmitry

Submitted on: 20 mei 2009

Language: C#

Type: System.Windows.Forms.DataGridView

Views: 3067