Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to delete a group of rows from a DataTable?
 

How to delete a group of rows from a DataTable?

Hello,

I need to delete a group of rows from a DataTable based on a condition. Something like:

Code Block

string CRefeicao = "01", CFamiliaPrato = "02";

foreach (DataRow LinhaDetalhe in Detalhe.Rows)
{
string Crefeicao2 = LinhaDetalhe["CRefeicao"].ToString();
string CFAmiliaPrato2 = LinhaDetalhe["CFamiliaPrato"].ToString();

if (CRefeicao == Crefeicao2 & CFamiliaPrato == CFAmiliaPrato2)
{
LinhaDetalhe.Delete();
}
}

The problem is: if i have a DataTable with 2 Rows, and the first row fills the condition it will be deleted, then it will remain 1 row in the DataTable. In the 2nd passage of the ForEach loop i'll get an exception: "Colection has been modified. INumerate operation can not be executed.".

It makes sense: now there's no row nÂș2 to loop through, but how can i delete a group of DataTable.DataRows?
There's other way to do this?

Thank You,

Joaquim

JoaquimC  Sunday, December 09, 2007 12:13 PM

It's solved, and it was obvious: a loop from end to beginning.

Joaquim

JoaquimC  Sunday, December 09, 2007 1:19 PM

It's solved, and it was obvious: a loop from end to beginning.

Joaquim

JoaquimC  Sunday, December 09, 2007 1:19 PM

You can use google to search for other answers

Custom Search

More Threads

• Databound Combox Can not change Focus
• Tablestyles in VS2005 Winform Datagrids do not work
• HELP - data grid creating duplicate controls?
• Pass Selected DataGrid Cell or Record to Another Form
• checkbox problem
• Hummph! Table Data Reverts to Previous...
• Multiple Rows/Lines in a single Gridview row?
• .Net Chart Control does not appear in run-time
• ADO.NET 2.0
• DataGridViewTextBoxColumn & DateTime !