Avatar of davecocks
davecocks
 asked on

DateTime in Filter in BindingSource

Hi
I'm using Visual studio 2008 VB.Net - windows form

column:   diveDate

I'm filtering records in a DataGridView on a Form using a dateTime picker

I need to convert the column diveDate into just date. At the moment its DateTime ie.

'03/02/2009 17:11:11'  I need to convert it into '03/02/2009'

I've tried 'diveDate.date'  and 'diveDate(date)'  or diveDate(dd:mm:yyyy) but I get the same error:

can't find column 'diveDate.date'.......


Me.DiveDetailsListingBindingSource.Filter = "diveDate = '" & _ 
datePickerSurveyList.Value.Date.ToString() & "'"

Open in new window

.NET ProgrammingVisual Basic Classic

Avatar of undefined
Last Comment
Jo Ha

8/22/2022 - Mon
Jorge Paulino

How about this way
Me.DiveDetailsListingBindingSource.Filter = "diveDate = '" & _ 
datePickerSurveyList.Value.Date.ToString("MM/dd/yyyy") & "'"

Open in new window

davecocks

ASKER
Hi jpaulino,

Thanks for your reply. I get this error:

Cannot perform '=' operation on System.DateTime and System.String.

I tried putting like instead of =, but I get this error:

Cannot perform 'Like' operation on System.DateTime and System.String.

Any ideas?
Jorge Paulino

Try this way:

Me.DiveDetailsListingBindingSource.Filter = "diveDate = #" & _ 
datePickerSurveyList.Value.Date.ToString("MM/dd/yyyy") & "#"

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
davecocks

ASKER
Hi jpaulino,

Thanks for your help,  I got the following error:

'String was not recognized as a valid DateTime

Make sure your method arguments are in the right format
When converting a string to DateTime, parse the string to take the date before putting each variable into the dateTime object'
Jorge Paulino

Can you show how you have done ? The last code I have showed you should work fine!
diveDate is a datetime field right ?
davecocks

ASKER
Hi,

Sorry you are right. I had been converting to dd/mm/yyyy rather than what you recommended mm/dd/yyyy

However, Its still doesn't filter the dates in diveDate.

I've tried some dummy data and records with:

 26/02/2009 00:00:00 get returned in the gridView fine but.....

it doesn't return any records with real dates i.e. 26/02/2009 16:45:34

Any other ideas?

    Private Sub datePickerSurveyList_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles datePickerSurveyList.ValueChanged
        
'Sets up filter for the selected dates
 Me.DiveDetailsListingBindingSource.Filter = "diveDate = #" & _
 datePickerSurveyList.Value.Date.ToString("MM/dd/yyyy") & "#"
 
 
    End Sub

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Jorge Paulino

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
davecocks

ASKER
Brilliant!!!! Thanks so much for your help!!!! This was the code in the end:

  Me.DiveDetailsListingBindingSource.Filter = "diveDate >= #" & _
        datePickerSurveyList.Value.Date.ToString("MM/dd/yyyy") & " 00:00:00# and diveDate <= #" & _
        datePickerSurveyList.Value.Date.ToString("MM/dd/yyyy") & " 23:59:59#"
Jorge Paulino

Glad I could help!
jpaulino
is-madar

this is the code for convert the date
ExDate = FormatDateTime(Mydatareader("ExpirationDate"), DateFormat.ShortDate)

Open in new window

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Jo Ha

Bindingsource.filter accepted Dateformat from american only.
"MM/dd/yyyy" & " 00:00:00# = true
"dd/MM/yyy" & " 00:00:00# = false