shashi sampige
shashi sampige

Reputation: 124

c# datatable filter using like operator

dtjr.DefaultView.RowFilter = " Journal_Name Like '" + cbo_jrnl.Text.Trim() + "*'";

is working fine,

But i want to filter all jounral_name starts with number from 0-9

How could i achieve this ?

Upvotes: 2

Views: 11872

Answers (2)

Mong Zhu
Mong Zhu

Reputation: 23732

what you need is the % operator:

dtjr.DefaultView.RowFilter = " Journal_Name Like '" + cbo_jrnl.Text.Trim() + "%'";

Here is a lesso with examples to the usage of LIKE

And here us the MSDN explanation for the syntax and usage:

Wildcard character    Description           Example

      %               Any string of zero    WHERE title LIKE '%computer%' 
                      or more characters.   finds all book titles with 
                                            the word 'computer' anywhere in 
                                            the book title.

EDIT:

Sorry for the lack of attention. if as you say:

But i want to filter all jounral_name starts with number from 0-9

In this case unfortunately using the [ ] operator ( which is also described in the MSDN link I posted) to state a range of numbers will not work as it seems to be the wrong format. What you can do is to check the range by comparison < and > and using the wildcard:

dtjr.DefaultView.RowFilter = @"Journal_Name > '0%' AND Journal_Name < '9%'";

Upvotes: 2

Azar Shaikh
Azar Shaikh

Reputation: 449

Not using like keyword here but You can use Regex and linq as below to achieve your goal

    dtjr= dtjr.AsEnumerable()
     .Where(r => Regex.IsMatch(r["jounral_name"].ToString(), "^[0-9]+"))
     .CopyToDataTable();

Upvotes: 0

Related Questions