在搜索结果中返回日期 [英] Return a date on search results

查看:81
本文介绍了在搜索结果中返回日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有各种搜索框(每个表单一个),编码后通过各种方法返回结果,只要返回的结果是整数格式,它就可以很好地工作。我目前的问题是我正在使用一个新的表单并需要搜索框通过REPAIR_DATE返回结果,这是一个DateTime字段。



我当前的代码如下:



I have various search boxes in my program (one per form) that is coded to bring results back by various methods and it works beautifully, so long as the returned results are in Integer format. My current problem is I am working a new form and need the search box to bring back results by REPAIR_DATE, which is a DateTime field.

my current code as follows:

<pre>    Private Sub btnSearch_Click(sender As System.Object, e As System.EventArgs) Handles btnSearch.Click
        Cursor = Cursors.WaitCursor
        Dirty()

        If cbColName.Text = "SEARCH BY:" Then
            MeMsgBoxSearchCriteria.ShowDialog()
        Else : lbSearchResults.Items.Clear()
            Select Case MaintenanceDataSet.Maintenance_Table.Columns(cbColName.Text).DataType
                Case GetType(Integer)
                    DV.RowFilter = cbColName.Text & " = " & tbSearchInput.Text.Trim
                Case GetType(Date)
                    DV.RowFilter = cbColName.Text & " = #" & tbSearchInput.Text.Trim & "#"
                Case Else
                    DV.RowFilter = cbColName.Text & " LIKE '*" & tbSearchInput.Text.Trim & "*'"
            End Select

            If DV.Count > 0 Then
                For IX As Integer = 0 To DV.Count - 1
                    lbSearchResults.Items.Add(DV.Item(IX)("ID"))
                Next
                If DV.Count = 1 Then
                    lbSearchResults.SelectedIndex = 0
                    Dim ix As Integer = MaintenanceDataSetBindingSource.Find("ID", CInt(lbSearchResults.SelectedItem.ToString))
                    MaintenanceDataSetBindingSource.Position = ix
                Else
                    lbSearchResults.Visible = True
                    lbSearchResults.BringToFront()
                End If
            Else
                ' Display a message box noting the search criteria is not found.   
                MeMsgBoxNoSearch.ShowDialog()
            End If
        End If
        Cursor = Cursors.Default
    End Sub

    Private Sub lbSearchResults_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lbSearchResults.SelectedIndexChanged
        Dim ix As Integer = MaintenanceDataSetBindingSource.Find("ID", CInt(lbSearchResults.SelectedItem.ToString))
        MaintenanceDataSetBindingSource.Position = ix
        lbSearchResults.Visible = False
    End Sub





我希望根据REPAIR_DATE列恢复结果。



我尝试过:



我尝试将ix as Integer重新定义为ix as Date并将CInt(lbSearchResults.SelectedItem.ToString)更改为CDate(lbSearchResults.SelectedItem.ToString)并且它的错误类型'整数'的值不能转换为'日期'我仍然是VB的新手,不知道如何正确地将REPAIR_DATE结果带回来而不是整数。我确实在我的列表框中按日期得到了结果(当我使用REPAIR_DATE作为排序字段时,但是上面有代码设置,但是当点击其中一个返回的结果来加载该记录时,它会导致程序用上面列出的错误中止主要是因为它想要返回一个数字而不是一个日期。我明白为什么它失败了,我只是不知道如何解决。



I am looking to bring back results based on column REPAIR_DATE.

What I have tried:

I tried redefining "ix as Integer" to "ix as Date" and changing "CInt(lbSearchResults.SelectedItem.ToString)" to "CDate(lbSearchResults.SelectedItem.ToString)" and it ERRORS WITH "value of type 'integer' cannot be converted to 'date'" I am still fairly new to VB and not sure how to properly bring REPAIR_DATE results back as opposed to an integer. I do get the results by date in my listbox (when I use REPAIR_DATE as the sort-by field, but with code set as I have it above, but when clicking on one of the returned results to load that record it causes the program to abort with the above listed error mainly because it is looking to return a number not a date. I understand why it fails, I just do not know how to resolve.

推荐答案

Quote:

我尝试将ix as Integer重新定义为ix as Date并更改 CInt(lbSearchResults。 SelectedItem.ToString)to CDate(lbSearchResults.SelectedItem.ToString)和它的错误 类型的值'integer'无法转换为'date'

I tried redefining "ix as Integer" to "ix as Date" and changing "CInt(lbSearchResults.SelectedItem.ToString)" to "CDate(lbSearchResults.SelectedItem.ToString)" and it ERRORS WITH "value of type 'integer' cannot be converted to 'date'"



错误信息非常清楚。假设 ID 字段是整数的类型,你不能把它转换为日期/日期时间数据类型。

你必须参考 REPAIR_DATE fiel d按照程序。



请注意,您的代码中还有其他一些问题:


The error message is quite clear. Assuming that ID field is type of integer, you cannot implicity convert it to date/datetime data type.
You have to refer to REPAIR_DATE field in according procedure.

Note that there's few other issues in your code:



  1. ToString()方法是多余的:


  1. ToString() method is redundant:
Dim ix As Integer = MaintenanceDataSetBindingSource.Find("ID", CInt(lbSearchResults.SelectedItem.ToString))

  • 按日期过滤数据,我建议更改你的代码:

  • to filter data by date, i'd suggest to change your code:

    DV.RowFilter = cbColName.Text & " = #" & tbSearchInput.Text.Trim & "#"



    to:


    to:

    Dim ci As CultureInfo = CultureInfo.InvariantCulture
    Dim stringDate As String = tbSearchInput.Text.Trim
    DV.RowFilter = String.Format("{0}=#{1}#", cbColName.Text, stringDate.Format("yyyy/MM/dd", ci))



    使用ISO日期时间格式是一种很好的编程习惯。根据具体情况,您可能需要添加时间部分。

    看看这里: vb.net - 如何在Visual Basic 2012中使用BindingSource.Filter作为日期? - 堆栈溢出 [ ^ ]





  • < br $> b $ b

    祝你好运!





    Good luck!


    这篇关于在搜索结果中返回日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆