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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21860206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:48:51  来源:igfitidea点击:

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

vb.netvisual-studio-2012bindingsource

提问by Funkster Phillip Watkins

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

Additional information: String was not recognized as a valid DateTime.

mscorlib.dll 中发生类型为“System.FormatException”的未处理异常

附加信息:字符串未被识别为有效的 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") & "#"

回答by Crono

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 selDateis declared outside the method, you probably don't want a parameterized method to begin with.

这也将能够接收带有时间值的日期而不会崩溃,而您以前的代码则不会。;) 话虽如此,由于selDate是在方法之外声明的,您可能不希望以参数化方法开头。