如何填写AutoCompleteCustomSource

时间:2011-04-30 15:10:20

标签: vb.net winforms sqlite autocomplete

如何使用vb.net中的数据阅读器使用查询结果填充AutoCompleteCustomSource列表?

需要示例代码。

编辑1:

这是我尝试过的,它不起作用

Private Sub cbEmployeeNo_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbEmployeeNo.Click

cbEmployeeNo.AutoCompleteCustomSource.Clear()

Dim mySelectQuery As String = "SELECT * FROM EmployeeTable WHERE " & cbSearch.Text & " LIKE '" & cbEmployeeNo.Text & "' AND Status LIKE '" & cbStatus.Text & "'"
Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
Dim sqConnection As New SQLiteConnection(myConnString)
Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
sqConnection.Open()

Try
    Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()
    Dim m As Integer

    Do While sqReader.Read

        For m = 1 To sqReader.FieldCount() - 1
            If (Not sqReader.IsDBNull(m)) Then
                If cbSearch.Text = "EmployeeID" Then
                    cbEmployeeNo.AutoCompleteCustomSource.Add(sqReader.GetInt32(m))
                Else
                    cbEmployeeNo.AutoCompleteCustomSource.Add(sqReader.GetString(m))
                End If
            End If
        Next m
    Loop

    sqReader.Close()

Catch ex As Exception
    MsgBox("Error: " & ex.ToString, vbExclamation)
Finally
    sqConnection.Close()
End Try
End Sub

编辑2:这是我尝试的第二个代码(遵循DevExpress的答案中的链接)。它无法正常工作

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim sStringColl As New AutoCompleteStringCollection
    Dim mySelectQuery As String = "SELECT * FROM EmployeeTable WHERE " & cbSearch.Text & " LIKE '" & cbEmployeeNo.Text & "' AND Status LIKE '" & cbStatus.Text & "'"
    Dim myConnString As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3"
    Dim sqConnection As New SQLiteConnection(myConnString)
    Dim sqCommand As New SQLiteCommand(mySelectQuery, sqConnection)
    sqConnection.Open()
    Dim sqReader As SQLiteDataReader = sqCommand.ExecuteReader()

While sqReader.Read()
    sStringColl.AddRange(New String() {sqReader(0)})
End While
cbEmployeeNo.AutoCompleteCustomSource = sStringColl
End Sub

1 个答案:

答案 0 :(得分:1)

我找到了您正在寻找的代码:

AutoCompleteCustomSource