如何通过搜索日期范围在datagridview中显示数据?

时间:2016-08-17 10:14:00

标签: vb.net datagridview

我想知道我的代码有什么问题 我想使用两个DateTimePicker.Value.Date来搜索日期范围,然后在DataGridView中显示整行。

DataGridView中没有显示我的代码,只有空白行。请帮忙!!谢谢!

这是我更新的代码:

Private Sub btnSearchTest_Click(sender As Object, e As EventArgs) Handles btnSearchTest.Click
    con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=|DataDirectory|\TESTDB.accdb")
    con.Open()

    Dim ds As New DataSet
    Dim dt As New DataTable
    Dim SelectedSearch As String
    SelectedSearch = cbxSearchList.SelectedItem
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter
    If cbxSearchList.SelectedItem = "Sign Date" Then
        SelectedSearch = "SignDate"
        Dim dtp1 As Date = DateTimePicker1.Value.Date
        Dim dtp2 As Date = DateTimePicker2.Value.Date
        da = New OleDbDataAdapter("SELECT * FROM User where " + SelectedSearch + " BETWEEN #" + dtp1 + "# and #" + dtp2 + "#", con)
    End If

    da.Fill(dt)
    SearchDataGridView.DataSource = dt.DefaultView

    con.Close()
    SearchDataGridView.Show()

End Sub

1 个答案:

答案 0 :(得分:1)

我会在数据表而不是数据库上使用过滤器,就像这样......

Private Sub btnSearchTest_Click(sender As Object, e As EventArgs) Handles btnSearchTest.Click
    con = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=|DataDirectory|\TESTDB.accdb")
    con.Open()

Dim ds As New DataSet
Dim dt As New DataTable
Dim SelectedSearch As String
SelectedSearch = cbxSearchList.SelectedItem
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
Dim dtp1 As Date
Dim dtp2 As Date
If cbxSearchList.SelectedItem = "Sign Date" Then
    SelectedSearch = "SignDate"
    dtp1 = DateTimePicker1.Value.Date
    dtp2 = DateTimePicker2.Value.Date
    da = New OleDbDataAdapter("SELECT * FROM User", con)
End If

da.Fill(dt)
SearchDataGridView.DataSource = dt.Select("#" & dtp1 & "# >= SignDate AND #" & dtp2 & "# <= SignDate")

con.Close()
SearchDataGridView.Show()

End Sub