Few days ago I worked on a project where database table has more than 80 columns. My client asked to create a search so that it can search through all the columns. So I have decided to create an extension method which will search through all the columns in DataTable. Creating generic method or extension for searching through all the columns in DataTable in C#.

It is difficult to write a SQL query (when table has many columns) which will search through all the columns.

Here I have tried to write a method which will filter through all the columns.

Input              : Source DataTable, Search String
Output           : DataTable with filtered rows.
Processing    : Actually our method used DataView to perform filter and it generates query like following

ColumnName0 like ‘%Your search string%’
Or ColumnName1 like ‘%Your search string%’
Or ColumnNameN like ‘%Your search string%’


Difficulties  : If any column in our table is integer or date format or any other format except string then like command will not work. That’s why I make a datatable which will compy all columns from source table by making all the columns in string.

Consider following code for creating filter method

public static DataTable exFilter(this DataTable dtSource, string strSearchString)
            DataTable newDataTable = new DataTable();

            //Creating Filter Query
            string FilterQuery = "";
            for (int v = 0; v

Download sample project