2个日期和文本之间的日期过滤器

Posted

技术标签:

【中文标题】2个日期和文本之间的日期过滤器【英文标题】:Date Filter Between 2 dates and text 【发布时间】:2021-05-13 05:58:55 【问题描述】:

美好的一天

我正在尝试过滤来自 2 个日期并基于文本的数据。 这是我在 datagridviewer 上加载数据的代码。

Private Sub frmMCOUT_Load_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim publictable As New DataTable
    Dim cmd As New OleDb.OleDbCommand
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String = "SELECT DateEncoded as [Date Encoded], LOTNUMBER as [Lot Number], PARTSLIST as [Part Number - Part Name], prdInput as [Input], prdOutput as [Output], OPERATOR as [Operator], PROCESS as [Process], MACHINE as [Machine], REJECTS as [Reject], INTDATE as [Date], Shift as [Shift], PROCMONTH as [Year and Month], PARTNUMBER as [Part Number], PARTNAME as [Part Name], HELPERKEY as [Helper] from ProdOutput WHERE PROCESS = 'MACHINING'; "


    Try
        'open the connection
        'conn.Open()
        With cmd
            .Connection = conn
            .CommandText = sql
        End With
        'get the SQL statements used to select records in the datasource
        da.SelectCommand = cmd
        'it fills the datatable
        da.Fill(publictable)
        'populate the datagridview
        dtgEntryList.DataSource = publictable
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally

        conn.Close()
        da.Dispose()
    End Try
    dtgEntryList.Sort(dtgEntryList.Columns(1), System.ComponentModel.ListSortDirection.Descending)


End Sub

我有 2 个日期选择器,我需要过滤在这 2 个日期之间输入的日期。我还需要过滤数据,以便它显示“加工”过程。关于如何做到这一点的任何想法??

谢谢

【问题讨论】:

因此您需要编写WHERE 子句来过滤这些值并将适当的参数添加到命令中。大量现有信息和示例供您参考。如果您在执行此操作时遇到问题,请告诉我们。 你想在DB中过滤还是在VB中过滤? 在 VB 中。我在数据网格视图中插入了 【参考方案1】:

我已将数据库代码与用户界面代码分开。这使得代码更容易维护。

不要在使用它们的方法之外声明连接。

使用Using...End Using 块确保数据库对象被关闭和释放。

Private ConStr As String = "Your connection string"
Private publictable As DataTable

Private Sub frmMCOUT_Load_2(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Try
        publictable = GetPartsData()  
    Catch ex As Exception
        MsgBox(ex.Message)
        Return
    End Try
    DataGridView1.DataSource = publictable
End Sub

Private Function GetPartsData() As DataTable
    Dim dt As New DataTable
    Dim sql As String = "SELECT DateEncoded as [Date Encoded], LOTNUMBER as [Lot Number], PARTSLIST as [Part Number - Part Name], prdInput as [Input], prdOutput as [Output], OPERATOR as [Operator], PROCESS as [Process], MACHINE as [Machine], REJECTS as [Reject], INTDATE as [Date], Shift as [Shift], PROCMONTH as [Year and Month], PARTNUMBER as [Part Number], PARTNAME as [Part Name], HELPERKEY as [Helper] from ProdOutput 
    WHERE PROCESS = 'MACHINING' 
    Order by LOTNUMBER DESC; "
    Using conn As New OleDbConnection(ConStr),
            cmd As New OleDbCommand(sql, conn)
        conn.Open()
        Using reader = cmd.ExecuteReader
            dt.Load(reader)
        End Using
    End Using
    Return dt
End Function

在用户有机会选择日期后,使用Select 方法过滤DataTable,并将生成的行数组分配给网格。我不确定您要过滤哪个日期字段。只需将Date Encoded 更改为您想要的字段。使用DataTable 中存在的字段名称,而不是数据库中的字段名称。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim StartDate = DateTimePicker1.Value
    Dim EndDate = DateTimePicker2.Value
    Dim filteredData = publictable.Select($"Date Encoded >= StartDate And Date Encoded <= EndDate")
    DataGridView1.DataSource = filteredData
End Sub

【讨论】:

以上是关于2个日期和文本之间的日期过滤器的主要内容,如果未能解决你的问题,请参考以下文章

drupal 8 views日期范围过滤器 - '在'运算符之间'不包括结束日期

Angular 2:如何过滤两个日期之间的记录?

如何使用Java Streams API正确过滤开始日期和结束日期之间的日期?

c# - 如何过滤包含单词和日期的日期列之间的datagridview?

Laravel,两列之间的日期范围过滤器

如何在 iOS swift 中使用文本字段过滤日期数组?