Combobox自动匹配问题
我在SQL中建了一个名为Station的表,其中SP:车站拼音首字母;SName:车站中文名称;
现在我在Combobox文本框中输入车站拼音首字母,可以实现自动匹配问题,但Combobox的下拉列表中会重复出现车站的中文名称;下面是我所写的代码,请各位高手指点一下,哪里有问题,该怎么改,谢谢!
VB.NET
cPub类
Imports System.Data.OleDb
Public Class cPub
Public ObjConn As OleDbConnection
Public ObjComm As OleDbCommand
Public ObjDa As New OleDbDataAdapter
Public ObjDs As New DataSet
Public dr As OleDbDataReader
Public ConnStr As String
Public Sqlstr As String
Public Sub New()
ConnStr = "Provider=SQLOLEDB;Data Source=XXX;Initial Catalog=Freights;User ID=sa;Password=sa;"
ObjConn = New OleDbConnection(ConnStr)
End Sub
Public Sub ConnOpen()
Try
If ObjConn.State.ToString = "Closed" Then
ObjConn.Open()
Else
Exit Sub
End If
Catch ex As Exception
Exit Sub
End Try
End Sub
Public Sub ReadData()
ConnOpen()
ObjComm = New OleDbCommand(Sqlstr, ObjConn)
dr = ObjComm.ExecuteReader()
End Sub
Public Function ReadStation(ByVal name As String) As OleDbDataReader
Sqlstr = "select * from Station where SP like'" & name & "%'"
ReadData()
Return dr
End Function
End Class
窗体中
Private Sub cbDZ_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDZ.TextChanged
dr = data.ReadStation(cbDZ.Text)
If dr.HasRows Then
While dr.Read
cbDZ.Items.Add(Trim(dr("SName")))
cbDZ.DroppedDown = True
End While
End If
End Sub
[解决办法]
给钱给钱
Public Sub FillDataToCombobox(ByVal objTableName As String, ByVal objColumnName As String, ByVal objCombobox As ComboBox) Dim objDataSet As New DataSet Using objData As New WDABase Try objDataSet.Clear() objData.SQL = "Select DISTINCT " & objColumnName & " from " & objTableName & " where " & objColumnName & " is not null" objData.OpenConnection() objData.FillDataSet(objDataSet, objTableName) objCombobox.DataSource = objDataSet.Tables(objTableName) objCombobox.DisplayMember = objColumnName objCombobox.AutoCompleteSource = AutoCompleteSource.ListItems objCombobox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest objCombobox.Text = "" Catch ex As Exception MsgBox("错误信息:" + ex.ToString, MsgBoxStyle.Critical, "System") Finally End Try End Using