Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to delete a group of rows from a datatable?
Message
From
11/12/2007 05:44:15
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
10/12/2007 11:33:07
John Baird
Coatesville, Pennsylvania, United States
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
01274269
Message ID:
01274718
Views:
14
>>>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
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform