Search code examples
c#datagridviewrowdeleting

Delete selected row from DataGridView


I need to delete the selected row from my DataGridView. Currently I managed to execute the select command but i do not know with what code to continue to remove/delete the selected row.

The code that i use is :

(refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
foreach (DataGridViewRow item in refTofrPlanMain.dGVPlan.Rows)
{
    if (item.Visible)
    {
        item.Selected = true;
        break;
    }
    //...
    //Other code
    //...
}

Where: - refTofrPlanMain represents a reference to Form1 (I am working in Form2) - dGVPlan is the DataGridView.

Thank you for your support.


Solution

  • (refTofrPlanMain.dGVPlan.DataSource as DataTable).DefaultView.RowFilter = string.Format("Vodic = '{0}'", searchTBoxW.Text);
    for(int i=refTofrPlanMain.dGVPlan.Rows.Count-1; i >=0; i--)
    {
        DataGridViewRow item = refTofrPlanMain.dGVPlan.Rows[i];
        if (item.Visible && !item.IsCurrentRowDirty())
        {
            refTofrPlanMain.dGVPlan.Rows.RemoveAt(i);
            break;
        }
        //...
        //Other code
        //...
    }