如何让datetimepicker工作

时间:2013-07-16 06:48:37

标签: vb.net datetimepicker

我有一张excel表,其中包含我想使用datetimepicker的日期(仅需要日期)。我只想点击datetimepicker,选择一个日期,它必须在我的数据网格视图中显示信息。这是我的按钮代码。

我有一个我试过的txt框,但是无法获取日期,我现在已经放了一个datetimepicker框,但是无法让ii工作。

对此有任何帮助。

这是我的主要代码

Imports System.Data.OleDb

Public Class Tapes_info
Private dtGlobal As New DataTable


Private Sub Tapes_info_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    dtGlobal.Clear()
    Using cn As New OleDb.OleDbConnection
        Dim Builder As New OleDbConnectionStringBuilder With {.DataSource = IO.Path.Combine(Application.StartupPath, "Backuptapes.xls"), .Provider = "Microsoft.ACE.OLEDB.12.0"}
        Builder.Add("Extended Properties", "Excel 12.0; IMEX=1;HDR=No;")
        cn.ConnectionString = Builder.ConnectionString
        cn.Open()
        Using cmd As OleDbCommand = New OleDbCommand With {.Connection = cn}

            cmd.CommandText = "SELECT TOP 5130 F1 As Tapes, F2 As Containere, F3 as ContainerRef, F4 as DateOut FROM [Tapese$]"
            Dim dr As System.Data.IDataReader = cmd.ExecuteReader
            dtGlobal.Load(dr)
            LstTape.DisplayMember = "Tapes"
            LstTape.DataSource = dtGlobal
            txtContainer.DataBindings.Add("Text", dtGlobal, "Containere")
            txtContainerRef.DataBindings.Add("Text", dtGlobal, "ContainerRef")
            txtDateOut.DataBindings.Add("Text", dtGlobal, "Dateout")

        End Using
    End Using
End Sub

这是我的按钮代码,我想显示我想要的任何日期。

Private Sub BtnSearchDateOut_Click(ByVal sender As System.Object, ByVal e As     System.EventArgs) Handles BtnSearchDateOut.Click

For i As Integer = 0 To dtGlobal.Rows.Count - 1
    If IsDBNull(dtGlobal.Rows(i)("Dateout")) Then
        dtGlobal.Rows(i)("Dateout") = ""
    End If
Next

Dim query = From item In dtGlobal.AsEnumerable() Where item.Field(Of String)("Dateout").StartsWith(txtSearchDateOut.Text) Select item
If query.Count > 0 Then
    Dim newDT As DataTable = query.CopyToDataTable()

    MsgBox(newDT.Rows.Count.ToString() & " Date out found.")

    Dim frm As New Form()
    Dim dgv As New DataGridView()
    dgv.DataSource = newDT
    dgv.Refresh()
    frm.Controls.Add(dgv)
    dgv.Dock = DockStyle.Fill
    frm.Size = New Size(1400, 700)
    frm.Show()

Else
    MsgBox("There is no Date for this found.") 'message
End If 

1 个答案:

答案 0 :(得分:0)

我不确定您是如何使用日期来过滤结果的,但是您首先要从dateTimePicker获取日期;像这样:Dim dt As Date = Me.DateTimePicker1.Value然后你可以使用dt.date作为过滤的输入。