Search code examples
c#regexstringdataviewrowfilter

How to create a DataView RowFilter that can filter if the input string is a substring of the string in the desired column


I would like to create a DataView RowFilter that can filter if the input string is a substring of the string in the column. For example, if the datatable dt has the column "Name", with the string values "Mary" in one row and "John" in the other. I want to a user to be able to type "ary" in a searchbox and the returned row is the one containing "Mary". Needless to say

dv.RowFilter = "Name = 'Mary'"

Will only return an exact match. I also want it such that if the input string is empty it returns all rows.


Solution

  • Per this link, you can use the LIKE comparison operator and the * wildcard. The following statement would return all rows where the Name column includes the value "Mary":

    dv.RowFilter = "Name LIKE '*Mary*'"
    

    Note, if the value were blank, the satement would be "Name LIKE '**'", which would return all rows so this satisfies your other condition.