1

我正在尝试bound source使用数据行在 VB 2010 中的(SQL 表)中的两个日期之间进行搜索。由于数据行函数不支持“之间”,因此我使用了<and >
因此,我运行此代码,当实际条目数为 8 时,输出为 900+ 个条目。在第二次点击我的按钮而不进行任何更改后,出现了正确的条目数。

Private Sub cmdSearch_Click(sender As System.Object, e As System.EventArgs) _ 
                                                    Handles cmdSearch.Click

    Dim Expression As String
    Dim OrderStr As String = "Area"
    Dim DateStr As String
    Dim StartDate As String
    Dim EndDate As String
    Dim Shift As String = ""
    Dim Area As String = ""
    Dim Product As String

    If (DtpStartDate.Value = Nothing Or DtpEndDate.Value = Nothing) Then
        MsgBox("Please input a start and end date.")
        Exit Sub
    End If

    If (radShiftAllSearch.Checked <> True _ 
            And radShiftOneSearch.Checked <> True 
            And radShiftTwoSearch.Checked <> True _ 
            And radShiftThreeSearch.Checked <> True) Then
        MsgBox("Please select a shift to search for.")
        Exit Sub
    End If

    Select Case True
        Case radShiftOneSearch.Checked
            Shift = " AND [Shift] = '1'"
        Case radShiftTwoSearch.Checked
            Shift = " AND [Shift] = '2'"
        Case radShiftThreeSearch.Checked
            Shift = " AND [Shift] = '3'"
        Case radShiftAllSearch.Checked
            Shift = " AND ([Shift] = '1' OR [Shift] = '2' OR [Shift] = '3')"
    End Select


    **StartDate = DtpStartDate.Value.Subtract(oneday)
    EndDate = DtpEndDate.Value.Add(oneday)
    'StartDate = Format(DtpStartDate.Value.Subtract(oneday), "M/dd/yyyy")
    'EndDate = Format(DtpEndDate.Value.Add(oneday), "M/dd/yyyy")
    DateStr = "[Dates] > '" & StartDate & "' AND [Dates] < '" & EndDate & "'"**

    If (txtProductSearch.Text = "") Then
        Product = ""
    Else
        Product = "AND [Product] LIKE '" & txtProductSearch.Text & "'"
    End If

    For h As Integer = 0 To CheckedListBox1.CheckedItems.Count - 1
        Dim XDRV As DataRowView = CType(CheckedListBox1.CheckedItems(h), DataRowView)
        Dim XDR As DataRow = XDRV.Row
        Dim XDisplayMember As String = XDR(CheckedListBox1.DisplayMember).ToString()
        If (Area = "") Then
            Area = Area & " AND ([Area] LIKE '" & XDisplayMember & "'"
        Else
            Area = Area & " OR [Area] LIKE '" & XDisplayMember & "'"
        End If
    Next

    If (Area <> "") Then
        Area = Area & ")"
    End If

    Expression = DateStr & Product & Shift & Area
    TextBox4.Text = Expression

    Dim SearchRows() As DataRow = _ 
    ProductionDataSet.Tables("Production_Daily").Select(Expression, OrderStr)
    'foundcount = SearchRows.Count - 1

    DataGridView1.DataSource = SearchRows
    DataGridView1.Show()


End Sub

谢谢

4

0 回答 0