Search code examples
c#asp.netregexdataviewrowfilter

Match a single character in a DataView RowFilter?


I'm wondering if there is any possible way of applying a RowFilter on a DataView which provides single character matching?

e.g. DataView.RowFilter = "SOME_COL like 'A_BCD%';

where the underscore represents a single-character match in Oracle SQL, and % is a wildcard of 0 or more characters.

Apparently RowFilter only supports wildcards (* or %) at the beginning or end of the matching expression. The only other idea I've had is to iterate through the rows and delete ones that don't match a regular expression, but this is not ideal.

Any help or ideas would be greatly appreciated! Thanks


Solution

  • You could use Linq to Datasets to do this

    class Program
    {
    
        static void Main(string[] args)
        {
            DataTable dt = new DataTable("test");
            dt.Columns.Add("SOME_COL", typeof(string));
    
            dt.Rows.Add("AABCDEFG");
            dt.Rows.Add("AZBCDEFG");
            dt.Rows.Add("AZZBCDEFG");
            dt.Rows.Add("ABC");
    
            Regex r = new Regex("A\\wBCDE");
    
            DataView dv 
               = (from t in dt.AsEnumerable()
                  where r.IsMatch(t.Field<string>("SOME_COL"))
                 select t).AsDataView();
    
            foreach(DataRowView row in dv)
            {
                Console.WriteLine(row[0]);
            }
    
    
        }
    
    
    }
    

    outputs

    AABCDEFG
    AZBCDEFG
    Press any key to continue . . .