ComboBox autofiltering items - using RowFilter

G

Guest

Hi guys!

I need a help from you.
I would like to do an autofiltering ComboBox. There is a ComboBox as
DropDown style and its items come from a DataView. Well, when user types on
that control (Text property) it should filter the rows inside using the
DataView.RowFilter property and, in the same time, shows to user its items
(DroppedDown = True) already filtered. I've tried the following:

Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp
Dim strRowFilter As String
strRowFilter = String.Concat("Name LIKE '%", ComboBox1.Text, "%'")
DataView1.RowFilter = strRowFilter
ComboBox1.DroppedDown = True
End Sub

As you can see, when the users types on ComboBox's Text property the event
KeyUp is raised. Then, its Text content becames a Filter string, eg. "John"
goes "Name LIKE '%John%'" and that string to RowFilter.

My solution for this issue isn't working. When I type the Text I can se the
items but for just the first letter.

Thanks in advance.
 
H

Henrry Pires

Hi
Try this code:

'somewhere alter fill you dataview
ComboBox1.SelectedItem = Nothing

Private Sub ComboBox1_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyUp

'save the original text

Dim str As String = ComboBox1.Text

'make the filter

dv.RowFilter = "Name LIKE '%" & ComboBox1.Text & "%'"

'celar the select value

ComboBox1.SelectedItem = Nothing

'set the original text

ComboBox1.Text = str

'put the focus at the end of the string

ComboBox1.SelectionStart = str.Length

'open the combo box

ComboBox1.DroppedDown = True

End Sub
 
G

Guest

Thanks a lot Henrry!

the code refined:

Private Sub ComboBox2_KeyUp(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles ComboBox2.KeyUp
If e.KeyCode >= Keys.A And e.KeyCode <= Keys.Z Or e.KeyCode =
Keys.Back Or e.KeyCode = Keys.Delete Then
Dim strTemp As String = ComboBox2.Text

If strTemp.Trim.CompareTo(String.Empty) = 0 Then
dtvCBOCliFor.RowFilter = String.Empty
Else
Dim strRowFilter As String = String.Concat("NomeRazSocial
LIKE '%", ComboBox2.Text, "%'")
dtvCBOCliFor.RowFilter = strRowFilter
End If

ComboBox2.Text = strTemp
ComboBox2.SelectionStart = strTemp.Length
ComboBox2.DroppedDown = True
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top