Solved

Help with creating filter bar for Data GridView using VB.NET

Posted on 2013-10-28
12
1,024 Views
Last Modified: 2013-10-29
Hi,


Using a Texbox control, how do I filter data in column(1) of my DataGridView.  For example
if my Grid contains:

House
Cat
Horse

When I type H in my textbox, I should only see:

House
Horse

Thanks,

Victor
0
Comment
Question by:vcharles
  • 6
  • 6
12 Comments
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 500 total points
ID: 39608261
Hi Victor...One way is to use BindingSource.Filter method in TextBox TextChange Event
Here is just a working example.
    Dim bs As New BindingSource
    Dim filepath As String = "C:\MyXMLfile.xml"
    Dim ds As New DataSet

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Try
            ds.ReadXml(filepath)
            bs.DataSource = ds.Tables(0)
            DataGridView1.DataSource = bs
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        If Me.TextBox1.Text.Length > 0 Then
            Me.bs.Filter = "Your first ColumnName Like '" & TextBox1.Text & "%'"
        Else
            Me.bs.RemoveFilter()
        End If
    End Sub

Open in new window


Hope this is a guide

Yiannis
0
 

Author Comment

by:vcharles
ID: 39608564
HiHu,

Hi,

I receive the following error when I try to type "10" in my Textbox.

Error:   Syntax error: Missing operand after ''1%'' operator.


Code:

 Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
        If Me.TextBox1.Text.Length > 0 Then
            Me.bs.Filter = "NSC'" & TextBox1.Text & "%'"
        Else
            Me.bs.RemoveFilter()
        End If
    End Sub
   


Any ideas why it is not working?

Thanks,

Victor
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39608634
Try
Me.bs.Filter = "NSC Like '" & TextBox1.Text & "%'"

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:vcharles
ID: 39608857
Still not working, running more test. Thanks.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39608873
Victor .Please provide your code so I can help further..
0
 

Author Comment

by:vcharles
ID: 39608996
Hi,

Below is my codemodified to use your code, old code is commented out.

Button Click event to load Grid:

 Dim dtset1 As New DataSet
        Dim fs1 As System.IO.FileStream
        fs1 = New System.IO.FileStream(Application.StartupPath + "\NSN.xml", IO.FileMode.Open)
        dtset1.ReadXml(fs1)
        bs.DataSource = dtset1.Tables(0)
        fs1.Close()
        '  Me.C1TrueDBGrid3.DataSource = dtset1.Tables(0)
               '*******************************
        ' C1TrueDBGrid3.DataSource = dt.DefaultView.ToTable(True, New String() {"NSN"})

        Me.C1TrueDBGrid3.DataSource = bs

Textbox Textchange event:

  If Me.TextBox11.Text.Length > 0 Then
            MsgBox(C1TrueDBGrid3.Columns("NSN").HeaderText)
            'Me.bs.Filter = "NSN" & TextBox11.Text & "%'"
            Me.bs.Filter = C1TrueDBGrid3.Columns("NSN").HeaderText & " Like '" & TextBox11.Text & "*'"
        Else
            Me.bs.RemoveFilter()
        End If
    End Sub

Thanks.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39609341
Change
 C1TrueDBGrid3.Columns("NSN").HeaderText

Open in new window


With just
 Me.bs.Filter = "NSN Like '" & TextBox11.Text & "%'"

Open in new window

Look at your XML File The NSN must be a node..


Yiannis
0
 

Author Comment

by:vcharles
ID: 39610284
Hi,

It works but, is there a way to achieve the same using C1TrueDBGrid3.Columns(0).HeaderText, because my column name will change and I would like to avoid hard coding the column name to avoid multiple If statements.

Thanks,

Victor
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39610312
No i think it can not be done...There is no need to use if statements if you know the srtucture of your XML files..Your dategtidview columns are autogenerated by setting the datasource,and column header text are automatically display the child elements of your XML file.Sure you can change this but in that case if you want to filter you must use the column names..


Yiannis
0
 

Author Comment

by:vcharles
ID: 39610323
Hi,

It's also working with Me.bs.Filter = C1TrueDBGrid3.Columns(1).HeaderText & " Like '" & TextBox11.Text & "%'"

Thank You.
0
 

Author Closing Comment

by:vcharles
ID: 39610325
Thank You.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 39610329
I did not try it with  Header text that is why i wrote "i think it can not be done"

So ..nice!!

Yiannis
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Reading the Web.Config using IIS 7.5? 4 37
C# bracket error 3 32
Modal Popup Extender control 1 22
vb.net class 3 17
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question