各位大神,我想實作在combobox中實作模糊查詢,從網上找的代碼,在運行程序中出現從型別“DataRowView”到型別“String”的轉換無效。請問怎么解決。

Private Sub ComboBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
'獲取輸入的字串
Dim text As String = ComboBox1.Text.Trim()
'用以記錄匹配字串的個數
Dim index As Integer = 0
Dim listBox1 As New ListBox
' list_Pd是定義的全域布爾變數,用于判斷是否創建了listbox控制元件
If list_Pd Then '如果已經創建
For Each contr As Control In Me.Controls '遍歷表單中的所有控制元件,尋找創建的listbox控制元件
If contr.Name = "list" Then
listBox1 = CType(contr, ListBox)
End If
Next
Else '如果沒有創建,則呼叫Custom_ListBox()函式創建
listBox1 = Custom_ListBox(ComboBox1)
End If
'將listbox 控制元件所有項清空
listBox1.Items.Clear()
'將查詢的結果添加到listbox 的items 中
For Each Str As String In ComboBox1.Items
'將所有的字串全部轉化為小寫再判斷,這樣輸入就不用分大小寫了
If Not text = "" And Str.ToLower.Contains(text.ToLower) Then
index += 1
listBox1.Items.Add(Str)
End If
Next
'判斷符合條件的項的個數,
If index = 1 Then
ComboBox1.Text = listBox1.Items(0)
listBox1.Visible = False
ElseIf index > 1 Then
listBox1.Visible = True
Else
listBox1.Visible = False
End If
End Sub
'自動創建listbox控制元件的函式
Private Function Custom_ListBox(ByVal ComBox As ComboBox) As ListBox
Dim Listbox As New ListBox
Dim point As Point
point.X = ComBox.Location.X
point.Y = ComBox.Location.Y + ComBox.Height
With Listbox
.Name = "list" '設定控制元件名稱
.Location = point '設定控制元件的位置,放在combobox的下面
.Width = ComBox.Width '控制元件的寬度,與combobox的寬一樣
.Height = ComBox.Height * (ComBox.Items.Count + 1) '高度
.Items.Clear()
.Visible = False
End With
AddHandler Listbox.Click, AddressOf ListBox_Click '添加點擊事件 ListBox_Click()
Me.Controls.Add(Listbox) '這步重要 將控制元件添加到表單中。沒有這句將不會顯示listbox控制元件
list_Pd = True
Return Listbox
End Function
'創建的listbox的點擊事件函式
Private Sub ListBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
CType(sender, ListBox).Visible = False
ComboBox1.Text = CType(sender, ListBox).SelectedItem
End Sub
uj5u.com熱心網友回復:
Combobox是系結的DataTable吧。你可以遍歷DataTable
foreach DataRow dr in dt.Rows //大概這樣
uj5u.com熱心網友回復:
Combobox如果是純文本就不會出錯,估計你是系結了資料源,如果是系結了資料源,可以按樓上的方法解決。uj5u.com熱心網友回復:
1、用個textbox放在邊上作為模糊查詢的輸入器。2、系結的datatable轉換成dataview,然后用dv的過濾就可以了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/38131.html
標籤:VB.NET
