Results 1 to 10 of 10

Thread: bindingsource.filter syntax

  1. #1

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Resolved bindingsource.filter syntax

    hi all

    I want to run a few different filters on different feilds i have in a bindingsource

    i understand how to do filter one COL to match a string but all the examples i have found only have this


    i want to know how to filter by date or between dates and use OR's or ANDs to add another filter

    right now i have

    gridinfo.Filter = "rootcause = '" + comboBox2.Text + "'"; but what if i wanted to filter rootcause to something and User to something else

    or rootcuase to something and opendate to between 12/12/07 and 12/12/06
    Last edited by Crash893; Jul 25th, 2007 at 04:59 PM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: bindingsource.filter syntax

    You basically answered your own question: put ANDs or ORs between each expression.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: bindingsource.filter syntax

    what about a between?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: bindingsource.filter syntax

    When you read the documentation for the BindingSource.Filter property you'd have seen this:
    The accepted syntax depends on the underlying data source. If the underlying data source is a DataSet, DataTable, or DataView, you can specify Boolean expressions using the syntax documented for the DataColumn.Expression property.
    When you followed the link to the topic for the DataColumn.Expression property you'd have seen what's valid and what's not. You'd have seen that the BETWEEN operator is not supported, so you'd have realised that you'd have to use two conditions: one with > and one with <.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: [RESOLVED] bindingsource.filter syntax

    is this not a valid search?

    c# Code:
    1. gridinfo.Filter = "rootcause LIKE '5'";
    2. or
    3. gridinfo.Filter = "rootcause LIKE '5*'";


    // "Author LIKE 'Va%'"
    // "Year_Born > 1940 AND Year_Born < 1948" (no, the BETWEEN operator is not supported)
    // "Author LIKE 'O''M%'". Note that the apostrophe is doubled to deal with the "O'Malley" issue.
    // "NOT Author IS NULL". This filters (out) rows where the Author column is NULL.
    // "Au_ID > 35 AND Author LIKE 'B%' OR Year_Born = 1947

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: bindingsource.filter syntax

    What data type is the rootcause column? If it's numeric then no, that's not a valid expression. The LIKE operator is for text columns only. Although I've never tried it myself, I'd guess that the syntax provides a way to convert a numeric column to a string and then use the LIKE operator on the result.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: bindingsource.filter syntax

    yes that one happens to be numeric but the number

    one other thing we are trying is to search on 3/06/* so any thing that happened on 3/06 through through out the years stuff like that

    I will continue to search if i find anything ill be sure to post it.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: bindingsource.filter syntax

    If you haven't already, take a look at the documentation for the DataColumn.Expression property. It describes the valid syntax for all properties and arguments that accept SQL-style strings. Your date search would be just like the number search: convert the column value to a string first, then use LIKE.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member Crash893's Avatar
    Join Date
    Dec 2005
    Posts
    930

    Re: bindingsource.filter syntax

    dates need a #date#

    gridinfo.Filter = "category_name LIKE '*" + comboBox1.Text + "*' AND open_date < #" + dateTimePicker1.Value.ToString("MM-dd-yyyy") + " 12:01 AM #" ;


    still working on the int portion i will let you all know

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: bindingsource.filter syntax

    Use the String.Format method. It makes the code much clearer and mistakes easier to detect:
    C# Code:
    1. gridinfo.Filter = string.Format("category_name LIKE '*{0}*' AND open_date < #{1:MM/dd/yyyy hh:mm TT}#",
    2.                                 comboBox1.Text,
    3.                                 dateTimePicker.Value.Date.AddMinutes(1))
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width