Results 1 to 5 of 5

Thread: Datagridview Filter By Date

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2012
    Posts
    9

    Exclamation Datagridview Filter By Date

    Hello all,
    I am using vb.net express version. I have a datagridview which is popuated with data from an access database. I have all the data side working ok with the "Call Date" column set as datetime and sorted in date order assending within my application.

    I also have a monthviewcalender on the form and wish to filter the datagridview by the date selected in the monthviewcalender to show appointments i may have on that date.

    I have tried to populate the selected date into a textbox and use the following to filter:
    Code:
    AppointmentsBindingSource.Filter = String.Format("[Call Date] Like '" & TextBoxDate.Text) & "*'"
    This causes a error. I have tried many different variants like:
    Code:
    AppointmentsBindingSource.Filter = ("[Call Date] Like '" & MonthCalendar1.SelectionRange.Start) & "*'"
    The column is set as datetime in access and is system.datetime in the database explorers properties.

    Can anyone help with this last little bit of code.

    Thanks In Advance

    Rich

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

    Re: Datagridview Filter By Date

    This is another example of a question that could have been answered by reading the documentation. Open the MSDN Library and navigate to the topic for the BindingSource.Filter property. It will direct you to the topic for the DataColumn.Expression property for syntax and that will show you the supported SQL-like syntax for this and similar properties, including how to format dates.

    Of course, just as with any form of SQL, you can't use LIKE with dates. LIKE only makes sense for text, not numbers or dates or anything else.
    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
    New Member
    Join Date
    Feb 2012
    Posts
    9

    Re: Datagridview Filter By Date

    Did you mean ....http://msdn.microsoft.com/en-us/libr...xpression.aspx

    I cannot seem to find anything there about dates and formatting them. Kind of lost again and can't seem to paste anything together.

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

    Re: Datagridview Filter By Date

    Really? You didn't see this?
    User-Defined Values

    User-defined values may be used within expressions to be compared with column values. String values should be enclosed within single quotation marks (and each single quotation character in a string value has to be escaped by prepending it with another single quotation character). Date values should be enclosed within pound signs (#) or single quotes (') based on the data provider. Decimals and scientific notation are permissible for numeric values. For example:

    "FirstName = 'John'"

    "Price <= 50.00"

    "Birthdate < #1/31/82#"

    For columns that contain enumeration values, cast the value to an integer data type. For example:

    "EnumColumn = 5"
    That is exactly the same format as Date literals in VB.NET: #M/dd/yyyy#.
    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
    Hyperactive Member
    Join Date
    Jun 2009
    Posts
    307

    Re: Datagridview Filter By Date

    Try This:

    Code:
    AppointmentsBindingSource.Filter = String.Format("Call Date = #{0:M/dd/yyyy}#" , TextBoxDate.Text)

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