简体   繁体   English

如何在 Visual Basic 2012 中为日期使用 BindingSource.Filter?

[英]How to use BindingSource.Filter for a Date in visual basic 2012?

I am trying to filter my database to show all bookings for a date thats selected from a calender I have on my form.我正在尝试过滤我的数据库以显示从我的表单上的日历中选择的日期的所有预订。 This is the code I have written...这是我写的代码...

    Public selDate As DateTime
Dim response As Integer

Public Sub FilterBooking(selDate)
    '// Here I will create a filter to for boookings on selected date from calender

    Dim dateFrom As DateTime
    Dim dateTo As DateTime

    dateFrom = selDate & " 00:00:01"
    dateTo = selDate & " 23:59:59"
    MsgBox(dateFrom)
    MsgBox(dateTo)

    Me.QueryBookingInfoBindingSource.Filter = "BookingDate >= #" & dateFrom & "# AND BookingDate <= #" & dateTo & "#"
End Sub

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'GarageDataSet.queryBookingInfo' table. You can move, or remove it, as needed.
    Me.QueryBookingInfoTableAdapter.Fill(Me.GarageDataSet.queryBookingInfo)
    'set currently selected date in the main calender to selDate variable
    selDate = mainCalender.SelectionStart.Date
    'run the following sub
    FilterBooking(selDate)
End Sub

The filter i have created when debugged gives this error message...我在调试时创建的过滤器给出了这个错误消息......

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll 中发生类型为“System.FormatException”的未处理异常

Additional information: String was not recognized as a valid DateTime.附加信息:字符串未被识别为有效的 DateTime。

Can someone show me where im making a mistake.有人可以告诉我我哪里出错了。

PS I have also tried this filter = PS我也试过这个过滤器=

Me.QueryBookingInfoBindingSource.Filter = "BookingDate >= #" & dateFrom.ToString("dd/MM/yyyy hh:mm:ss") & "# AND BookingDate <= #" & dateTo.ToString("dd/MM/yyyy hh:mm:ss") & "#"

Have you tried formatting your dates?您是否尝试过格式化日期?

Me.QueryBookingInfoBindingSource.Filter = "BookingDate >= " & String.Format("#{0:yyyy/MM/dd HH:mm:ss}#", dateFrom) & " AND BookingDate <= " & String.Format("#{0:yyyy/MM/dd HH:mm:ss}#", dateTo)

EDIT:编辑:

This IMHO is cleaner and easier to read:这个恕我直言更干净,更容易阅读:

Public Sub FilterBooking(selDate)
    Dim dateFrom As DateTime = selDate.Date
    Dim dateTo As DateTime = dateFrom.AddDays(1).Subtract(New TimeSpan(1))

    Dim filterBuilder As New StringBuilder()
    Dim filterFormat As String = "BookingDate {0} #{1:yyyy/MM/dd HH:mm:ss}#"

    With filterBuilder
        .AppendFormat(filterFormat, ">=", dateFrom)
        .Append(" AND ")
        .AppendFormat(filterFormat, "<=", dateTo)
    End With

    Me.QueryBookingInfoBindingSource.Filter = filterBuilder.ToString()
End Sub

This will also be able to receive dates with time values without crashing, which your former code wouldn't.这也将能够接收带有时间值的日期而不会崩溃,而您以前的代码则不会。 ;) That being said, since selDate is declared outside the method, you probably don't want a parameterized method to begin with. ;) 话虽如此,由于selDate是在方法之外声明的,您可能不希望以参数化方法开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM