dcsimg
Results 1 to 5 of 5

Thread: Help about using String.Format for Filtering BindingSource

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    2

    Question Help about using String.Format for Filtering BindingSource

    Hi guys,

    English is not my first language but i'll try my best;

    So, i'm using the below code and it works like charm. I also can add many more conditions in it. But as you can see it's working only when things are equal. (Items below ExampleColumnName should equal to TextBox1.text)

    xxxBindingSource.Filter = String.Format("ExampleColumnName = '{0}' AND ExampleColumnName2 = '{1}' AND ExampleColumnName3 = '{2}'", TextBox1.Text, TextBox2.Text, TextBox3.Text)

    What i want to do is use the "like" statement in there like the below code.

    xxxBindingSource.Filter = String.Format("ExampleColumnName Like '{0}' AND ExampleColumnName2 Like '{1}' AND ExampleColumnName3 Like '{2}'", TextBox1.Text, TextBox2.Text, TextBox3.Text)

    However even if i tried it with "like" or "Like" or "LIKE" it's not working.

    I know there is many ways to do filtering multiple columns in DGV. But since the code i'm using is working perfectly could you guys suggest me a way to use the like statement without using different technique?

    Thanks for any suggestions or solutions in advance...

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,109

    Re: Help about using String.Format for Filtering BindingSource

    Welcome to VBForums

    If you don't use wildcards, Like does the same as =

    Try this:
    Code:
    xxxBindingSource.Filter = String.Format("ExampleColumnName Like '*{0}*', TextBox1.Text)

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,760

    Re: Help about using String.Format for Filtering BindingSource

    For future reference, here's what you should have done. You want to do something using the BindingSource.Filter property. Either click on that and press the F1 key or open the documentation from the Help menu and find that property in the index. In the documentation for that property is says:
    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.
    and provides a link. In the documentation for DataColumn.Expression it says:
    Wildcard Characters
    Both the * and % can be used interchangeably for wildcard characters in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be enclosed in brackets ([]). If a bracket is in the clause, each bracket character should be enclosed in brackets (for example [[] or []]). A wildcard is allowed at the start and end of a pattern, or at the end of a pattern, or at the start of a pattern. For example:

    "ItemName LIKE '*product*'"

    "ItemName LIKE '*product'"

    "ItemName LIKE 'product*'"

    Wildcard characters are not allowed in the middle of a string. For example, 'te*xt' is not allowed.
    You now have the answer to your question, using the Help menu and a few minutes reading. I strongly suggest that everyone uses the documentation first when they need help. That's why it's accessible under the Help menu.
    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

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    2

    Re: Help about using String.Format for Filtering BindingSource

    Hi,

    @si_the_geek thank you so much for quick and simple answer. "IT WORKED!" and you've made my day my friend

    @jmcilhinney i wasn't using it because when i first start to using vs2015 sometimes i couldn't find access to the internet on the same computer. But since you suggest it, i surely start using it more often from now on.

    thank you both guys, thanks so much

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    99,760

    Re: Help about using String.Format for Filtering BindingSource

    Note that you can also install most of the documentation locally if you want too, meaning that you don't need internet access to view it.
    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
  •  



Featured


Click Here to Expand Forum to Full Width