2017-02-12 73 views
-1

我需要按日期搜索DataGridView。如果我在文本框中輸入2/11/2017,我希望網格向我顯示包含此日期的行。 DataGridView中的日期列名爲AppointmentDate,並在SQL Server中創建。此欄的類型僅爲日期。使用文本框搜索VB

我看到了這個,但我使用的是實體框架模型,我不知道如何實現它。

dgvPacientet.CurrentCell = Nothing 
    Dim found As Boolean = False 
    For Each row As DataGridViewRow In dgvPacientet.Rows 
     row.Visible = row.Cells("Dt_terminit").Value = txtData.Text 
     If row.Visible Then found = True 
    Next 
    If Not found Then MsgBox("Item not found")` 

回答

-1

只顯示或隱藏包含所需文本的行。

DataGridView1.CurrentCell = Nothing 
Dim found as Boolean = false 
For Each row As DataGridViewRow In DataGridView1.Rows 
    Row.Visible = Not IsDBNull(row.Cells("DT_Terminit").Value) andalso row.Cells("DT_Terminit").Value = TextBox5.Text 
    If Row.Visible then found = true 
Next 
If Not found Then MsgBox("Item not found") 

但是你真的需要解析文本,以確保它是一個日期第一,如果單元格中包含一個真正的日期,你需要測試轉換成日期。

Dim DT As Date 
If Not Date.TryParse(TextBox5.Text, DT) Then 
    MsgBox("Please enter a date") '<-- remove this line if searching on the fly... (as you enter text) 
    Exit Sub 
End If 

DataGridView1.CurrentCell = Nothing 

Dim found As Boolean = False 
For Each row As DataGridViewRow In DataGridView1.Rows 
    Row.Visible = Not IsDBNull(row.Cells("DT_Terminit").Value) andalso row.Cells("DT_Terminit").Value =DT 
    If row.Visible Then found = True 
Next 
If Not found Then MsgBox("Item not found") 
+0

它說與貨幣經理的位置關聯的行不能被隱藏。 – Rinor

+0

YOu可能需要先清除當前單元格...查看更新後的代碼。 –

+0

它不讓我寫日期。我開始輸入日期,只要我輸入2,它就對我說,請輸入日期。 – Rinor