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:wave:
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.
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.
Re: Datagridview Filter By Date
Really? You didn't see this?
Quote:
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#.
Re: Datagridview Filter By Date
Try This:
Code:
AppointmentsBindingSource.Filter = String.Format("Call Date = #{0:M/dd/yyyy}#" , TextBoxDate.Text)