Solved

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

Posted on 2013-10-28
12
1,061 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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