Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to delete a group of rows from a datatable?
Message
De
10/12/2007 11:32:30
John Baird
Coatesville, Pennsylvanie, États-Unis
 
 
À
10/12/2007 10:43:06
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Divers
Thread ID:
01274269
Message ID:
01274459
Vues:
15
>>Hello,
>>
>>I need to delete a group of rows from a DataTable based on a condition. Something like:
>>
>>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
>
>Joaquim,
>You could do that this way:
>
> string CRefeicao = "01", CFamiliaPrato = "02";
> string strFilter =
>    String.Format("CRefeicao = '{0}' and CFamiliaPrato = '{1}'",
>                  CRefeicao,
>                  CFamiliaPrato);
> foreach( DataRow row in Detalhe.Select( strFilter ) )
> {
>   row.Delete();
> }
>
>Cetin
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform