Saturday, April 3, 2010

remove rows from dataset

In order to remove rows from a given dataset and If you really want to use a foreach, copy the rows to an array first:


private void Test()
{
   DataTable dt = new DataTable();
   foreach (DataRow row in ToArray(dt.Rows))
   {
     row.Delete();
   }
}
DataRow[] ToArray(DataRowCollection collection)
{
   DataRow[] result = new DataRow[collection.Count];
   collection.CopyTo(result, 0);
   return result;
}


You can also use a for-next loop...

Charles

http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/cd5d6063-9226-4f98-9220-7d6b1b53e710

No comments:

Post a Comment