Solved

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

Posted on 2013-10-28
12
991 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
Comment Utility
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
Comment Utility
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
Comment Utility
Try
Me.bs.Filter = "NSC Like '" & TextBox1.Text & "%'"

Open in new window

0
 

Author Comment

by:vcharles
Comment Utility
Still not working, running more test. Thanks.
0
 
LVL 18

Expert Comment

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

Author Comment

by:vcharles
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

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

Thank You.
0
 

Author Closing Comment

by:vcharles
Comment Utility
Thank You.
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now