none
DataView.RowFilter CAN'T match exactly? RRS feed

  • Question

  • Does "=" in RowFilter returns exactly rows?

    If I set the RowFilter as "col1='abc'", It seems that both "abc" and "abc     " will be returned as result.

    WHY?

    What follows is my test code.

        class Program

        {

            static void Main(string[] args)

            {

                DataTable table = new DataTable();

                table.Columns.Add("col1");

     

                table.Rows.Add("abc");

                table.Rows.Add("abc        ");

                table.Rows.Add("        abc        ");

     

                table.DefaultView.RowFilter = "col1 = 'abc'";

     

                Console.WriteLine(table.DefaultView.Count);

            }

        }


    I Believe I Can Fly.
    Wednesday, June 9, 2010 10:01 AM

Answers

  • Hi kooboobird,

    And please try this:

    DataTable table = new DataTable();
    table.Columns.Add("col1");
    
    table.Rows.Add("abc");
    table.Rows.Add("abc    ");
    table.Rows.Add("    abc    ");
    
    DataView dv = new DataView(table);
    dv.RowFilter = "col1 = 'abc' And Len(col1) = Len('abc')";
    
    this.dataGridView1.DataSource = dv;
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 14, 2010 5:03 AM
    Moderator

All replies

  • Hi Kooboobird,

    "When you perform string comparisons by using LIKE, all characters in the pattern string are significant. This includes leading or trailing spaces. If a comparison in a query is to return all rows with a string LIKE 'abc ' (abc followed by a single space), a row in which the value of that column is abc (abc without a space) is not returned. However, trailing blanks, in the expression to which the pattern is matched, are ignored. If a comparison in a query is to return all rows with the string LIKE 'abc' (abc without a space), all rows that start with abc and have zero or more trailing blanks are returned." And I think the the same rules apply for RowFilter.
    http://msdn.microsoft.com/en-us/library/ms179859.aspx

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, June 10, 2010 10:30 AM
    Moderator
  • Thanks Alex. But how do I get exact results by RowFilter?


    I Believe I Can Fly.
    Friday, June 11, 2010 1:27 AM
  • Hi kooboobird,

    You can filter the rows using linq. For example:

    var results = from p in table.AsEnumerable()
           where p.Field<string>("col1") == "abc"
           select p;
    
    DataTable dt1 = results .CopyToDataTable<DataRow>();
    
    this.dataGridView2.DataSource = dt1;
    
    Best regards,
    Alex Liang
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, June 11, 2010 8:13 AM
    Moderator
  • Thanks again!

    But I have to use .NET 2.0 for some reason. So is there any solution using RowFilter?


    I Believe I Can Fly.
    Friday, June 11, 2010 10:02 AM
  • Hi kooboobird,

    And please try this:

    DataTable table = new DataTable();
    table.Columns.Add("col1");
    
    table.Rows.Add("abc");
    table.Rows.Add("abc    ");
    table.Rows.Add("    abc    ");
    
    DataView dv = new DataView(table);
    dv.RowFilter = "col1 = 'abc' And Len(col1) = Len('abc')";
    
    this.dataGridView1.DataSource = dv;
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, June 14, 2010 5:03 AM
    Moderator
  • Thank you very much!
    I Believe I Can Fly.
    Thursday, June 17, 2010 4:47 AM