在搜索结果中返回日期

编程入门 行业动态 更新时间:2024-10-25 12:28:13
本文介绍了在搜索结果中返回日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

  • ToString()方法是多余的:
  • 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 - 如何在Visual Basic 2012中使用BindingSource.Filter作为日期? - 堆栈溢出 [ ^ ]

  • b $ b 祝你好运!

    Good luck!

    更多推荐

    在搜索结果中返回日期

    本文发布于:2023-10-18 13:22:46,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1504344.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:搜索结果   日期

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!