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.
 
W

W.G. Ryan - MVP

What is the behavior - is it just not filtering or is it showing nothing?
I've used the following code and the behavior is what you'd expect, it
selects the new value and then shows it dropped down and highlighted. If
you use the mouse to clear the value it only fires KeyUp once, otherwise it
fires twice. However either way it's working. You may have a problem with
the UI and calling DoEvents() may refresh it - but that isn't a problem on
my machine. This is in C# (I apologize, i don't have VB.NET loaded on the
machine I'm on) but this does work and the translation is simple in that I
used your code and just converted it:

private DataTable dt;
private DataView dv;
private void Form1_Load(object sender, System.EventArgs e)
{
dt = new DataTable("MyTable");
DataColumn dc = new DataColumn("Name", typeof(System.String));
dt.Columns.Add(dc);
DataRow dro = dt.NewRow();
dro[0] = "William";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "John";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Michael";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Edward";
dt.Rows.Add(dro);
dv = dt.DefaultView;
comboBox1.DataSource = dv;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember= "Name";
}

private void comboBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
String strRowFilter = String.Concat("Name LIKE '%", comboBox1.Text, "%'");
dv.RowFilter = strRowFilter;
comboBox1.DroppedDown = true;
}
 
G

Guest

Thanks Ryan!

It just catch the first one letter.
I've figure out the problem and the following code is perfect:

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

W.G. Ryan - MVP said:
What is the behavior - is it just not filtering or is it showing nothing?
I've used the following code and the behavior is what you'd expect, it
selects the new value and then shows it dropped down and highlighted. If
you use the mouse to clear the value it only fires KeyUp once, otherwise it
fires twice. However either way it's working. You may have a problem with
the UI and calling DoEvents() may refresh it - but that isn't a problem on
my machine. This is in C# (I apologize, i don't have VB.NET loaded on the
machine I'm on) but this does work and the translation is simple in that I
used your code and just converted it:

private DataTable dt;
private DataView dv;
private void Form1_Load(object sender, System.EventArgs e)
{
dt = new DataTable("MyTable");
DataColumn dc = new DataColumn("Name", typeof(System.String));
dt.Columns.Add(dc);
DataRow dro = dt.NewRow();
dro[0] = "William";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "John";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Michael";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Edward";
dt.Rows.Add(dro);
dv = dt.DefaultView;
comboBox1.DataSource = dv;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember= "Name";
}

private void comboBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
String strRowFilter = String.Concat("Name LIKE '%", comboBox1.Text, "%'");
dv.RowFilter = strRowFilter;
comboBox1.DroppedDown = true;
}
Andreiwid said:
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.
 
P

P. Van Den Goess

I was under the understanding that resetting the Rowfilter over and over was
to be avoided in that it causes new DataView to be created each time. So if
user eroneously clicks on the combobox, then an extra dataview is created
But even when not, a new one is created each time so such methods are
potentially wasteful. Is this a case where there is better approach, or is
it a matter of perhaps being not so good approach, but being the best
available, or as good as any other approach available?
W.G. Ryan - MVP said:
What is the behavior - is it just not filtering or is it showing nothing?
I've used the following code and the behavior is what you'd expect, it
selects the new value and then shows it dropped down and highlighted. If
you use the mouse to clear the value it only fires KeyUp once, otherwise it
fires twice. However either way it's working. You may have a problem with
the UI and calling DoEvents() may refresh it - but that isn't a problem on
my machine. This is in C# (I apologize, i don't have VB.NET loaded on the
machine I'm on) but this does work and the translation is simple in that I
used your code and just converted it:

private DataTable dt;
private DataView dv;
private void Form1_Load(object sender, System.EventArgs e)
{
dt = new DataTable("MyTable");
DataColumn dc = new DataColumn("Name", typeof(System.String));
dt.Columns.Add(dc);
DataRow dro = dt.NewRow();
dro[0] = "William";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "John";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Michael";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Edward";
dt.Rows.Add(dro);
dv = dt.DefaultView;
comboBox1.DataSource = dv;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember= "Name";
}

private void comboBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
String strRowFilter = String.Concat("Name LIKE '%", comboBox1.Text, "%'");
dv.RowFilter = strRowFilter;
comboBox1.DroppedDown = true;
}
Andreiwid said:
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.
 
P

P. Van Den Goess

Anderiwid:

As an aside, I think the dataview approach may be too bulky for reasons in
my last post. To get the behavior that you want, you may want to opt for
this approach as recommended from MSDN
http://support.microsoft.com/default.aspx?scid=kb;en-us;320107 . While I
think W.G. approach works as he mentioned, I think it is not the best or
cleanest approach - the article specifies why I think that to be so.
Andreiwid said:
Thanks Ryan!

It just catch the first one letter.
I've figure out the problem and the following code is perfect:

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

W.G. Ryan - MVP said:
What is the behavior - is it just not filtering or is it showing nothing?
I've used the following code and the behavior is what you'd expect, it
selects the new value and then shows it dropped down and highlighted. If
you use the mouse to clear the value it only fires KeyUp once, otherwise it
fires twice. However either way it's working. You may have a problem with
the UI and calling DoEvents() may refresh it - but that isn't a problem on
my machine. This is in C# (I apologize, i don't have VB.NET loaded on the
machine I'm on) but this does work and the translation is simple in that I
used your code and just converted it:

private DataTable dt;
private DataView dv;
private void Form1_Load(object sender, System.EventArgs e)
{
dt = new DataTable("MyTable");
DataColumn dc = new DataColumn("Name", typeof(System.String));
dt.Columns.Add(dc);
DataRow dro = dt.NewRow();
dro[0] = "William";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "John";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Michael";
dt.Rows.Add(dro);
dro = dt.NewRow();
dro[0] = "Edward";
dt.Rows.Add(dro);
dv = dt.DefaultView;
comboBox1.DataSource = dv;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember= "Name";
}

private void comboBox1_KeyUp(object sender,
System.Windows.Forms.KeyEventArgs e)
{
String strRowFilter = String.Concat("Name LIKE '%", comboBox1.Text, "%'");
dv.RowFilter = strRowFilter;
comboBox1.DroppedDown = true;
}
Andreiwid said:
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.
 

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