使用文本框VB搜索

时间:2017-02-12 13:24:24

标签: vb.net textbox

我需要按日期搜索DataGridView。如果我在文本框中输入2/11/2017,我希望网格显示包含此日期的行。 DataGridView中的Date列名为AppointmentDate,在SQL Server中创建。此列的类型仅为日期。

我看到了这个,但我使用的是实体框架模型,我不知道如何实现它。

    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 个答案:

答案 0 :(得分:-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")