Geeks With Blogs
faizan ahmad Usually the things which were not a straight Google

Here is a extension method which can be used to perform the  Delete operation on a DataTable just like the select i.e. using a filterExpression:

using

System;

using

System.Collections.Generic;

using

System.Data;

using

System.Data.SqlClient;

public static class MyExtensions

{

   /// <summary>

   /// Delete row based on filterExpression

   /// </summary>

   /// <param name="dt"></param>

   /// <param name="filterExpression"> just like select </param>

   /// <returns> number of deleted rows </returns>

   public static int Delete( this DataTable dt, string filterExpression)

   {

      int nor=0; // number of rows deleted

      DataRow [] toBeDeleted;

      toBeDeleted = dt.Select(filterExpression);

      if (toBeDeleted.Length > 0)

      {

         foreach ( DataRow dr in toBeDeleted)

            {

               dt.Rows.Remove(dr);

               nor++;

            }

      }

      return nor;

   }

 }

 

Posted on Monday, December 12, 2011 5:34 PM .NET | Back to top


Comments on this post: DataTable Delete using condition / where clause

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © faizanahmad | Powered by: GeeksWithBlogs.net