Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to delete a group of rows from a datatable?
Message
De
11/12/2007 16:41:30
 
 
À
11/12/2007 05:44:15
Cetin Basoz
Engineerica Inc.
Izmir, Turquie
Information générale
Forum:
ASP.NET
Catégorie:
ADO.NET
Divers
Thread ID:
01274269
Message ID:
01274957
Vues:
12
Actually, Cetin, there are indexer problems if the DataTable in question has added rows (row.RowState == DataRowState.Added). Looping through the DataTable with either a for (going forward) or a foreach will cause it to crash (it works fine if all the Rows are in an Unchanged state).

So, the two methodologies that work are
1)to use a for (and iterate backwards) or
2) your solution (to use a .Select()).
Both methodologies will not crash ... even if a RowState of any row is Added.

~~Bonnie

>>>>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
>>
>>Don't you still have the problem with the indexer in this example also?
>
>Which indexer problem? There weren't any indexer problem in the first place, I simply provided another way of filtering the rows to delete. Joaquim probably guided you wrong with the error message. "Delete" doesn't remove the row from the collection, thus no indexer problem there. Probably instead he tried to access to those deleted rows through DataRow which would error something like "...inaccessible...", not collection modified. ie:
>
>
>using System;
>using System.Data;
>using System.Data.OleDb;
>
>class test
>{
> static void Main()
> {
>   OleDbConnection con = new OleDbConnection(@"Provider=VFPOLEDB;
>Data Source=C:\PROGRAM FILES\MICROSOFT VISUAL FOXPRO 9\SAMPLES\data\testdata.dbc");
>   con.Open();
>   OleDbCommand cmd =
>      new OleDbCommand("select cust_id,company,contact,country from customer",con);
>   OleDbDataReader rdr = cmd.ExecuteReader();
>   DataTable tbl = new DataTable();
>   tbl.Load(rdr);
>   con.Close();
>
>   foreach(DataRow row in tbl.Rows)
>   {
>     if (((string)row["country"]).Trim() == "USA" || ((string)row["country"]).Trim() == "UK")
>         row.Delete();
>   }
>
>   for(int i=0;i<tbl.Rows.Count;i++)
>   {
>       if (tbl.Rows[i].RowState == DataRowState.Deleted )
>        {
>         Console.WriteLine("Row:{0},{1}",i,
>           tbl.Rows[i].RowState);
>        }
>       else
>       {
>         Console.WriteLine("Row:{0},{1},{2}",i,
>           tbl.Rows[i]["cust_id"],
>           tbl.Rows[i]["country"]);
>       }
>   }
> }
>}
>
>
>Or:
>
>   foreach(DataRow row in tbl.Rows)
>   {
>       if (row.RowState == DataRowState.Deleted )
>        {
>         Console.WriteLine("Row:{0}", row.RowState);
>        }
>       else
>       {
>         Console.WriteLine("Row:{0},{1}",
>           row["cust_id"],
>           row["country"]);
>       }
>   }
>
>I think what he did was to query row["field"] for deleted ones too.
>Cetin
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform