Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to delete a group of rows from a datatable?
Message
From
12/12/2007 06:43:10
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
11/12/2007 16:41:30
General information
Forum:
ASP.NET
Category:
ADO.NET
Miscellaneous
Thread ID:
01274269
Message ID:
01275071
Views:
14
Hi Bonnie,
Yes,
I forget that bug still exists.
Cetin

>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
Ç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
Reply
Map
View

Click here to load this message in the networking platform