0

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.

Draken
  • 3,134
  • 13
  • 34
  • 54
Stefan Siman
  • 49
  • 2
  • 10
  • Biggest problem is you're doing a foreach loop and should never delete during a foreach loop. Better to reverse iterate and delete like here: http://stackoverflow.com/questions/1582285/how-to-remove-elements-from-a-generic-list-while-iterating-over-it – Draken Apr 12 '16 at 08:39
  • Thing is that i wil constantly search new values (the exact value will be filtered without doubles) and i want to delete it each time. Would that pose a problem ? – Stefan Siman Apr 12 '16 at 08:45
  • Not that I'm aware of, though I'm not sure I understand your question. The only issue you may have is the larger your list gets, the longer the time it takes to iterate. But you were iterating it anyway, so I don't see an issue – Draken Apr 12 '16 at 08:47
  • My question is what is the code to remove the selected row, that would fit my code above. – Stefan Siman Apr 12 '16 at 08:56
  • Check the posted answer, not 100% `refTofrPlanMain.dGVPlan.Rows.Count` is how you access the row count, but should be something similar – Draken Apr 12 '16 at 09:02

1 Answers1

1
(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
    //...
}
Draken
  • 3,134
  • 13
  • 34
  • 54
  • It might be Items.Count, as referred to here: http://stackoverflow.com/questions/838679/row-and-column-count-of-data-grid-in-c-sharp – Draken Apr 12 '16 at 09:04
  • It seems i recive a error : Additional information: Uncommitted new row cannot be deleted. Located at the and of refTofrPlanMain.dGVPlan.Rows.RemoveAt(i); – Stefan Siman Apr 12 '16 at 09:09
  • Should users be able to add new rows to your `datagridview` as it's complaining there is an incomplete row added? – Draken Apr 12 '16 at 09:12
  • Check the edit, added a IsCurrentRowDirty check: https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.iscurrentrowdirty(v=vs.110).aspx – Draken Apr 12 '16 at 09:13
  • Well, they do not add new row, it loads an .xls with OleDB. And i aim to remove from datagridview the rows. – Stefan Siman Apr 12 '16 at 09:30
  • You might want to set this to false then: `refTofrPlanMain.dGVPlan.AllowUserToAddRows = false` – Draken Apr 12 '16 at 09:31
  • Solved the issue with the error, had to modify the GridViewProperty to AllowUserToAddRow to False. Thank you. – Stefan Siman Apr 12 '16 at 09:37