Solved

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

Posted on 2013-10-28
12
1,033 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with opening combobox using VB.NET. 3 26
Create a datatable in vb.net dynamically 1 24
Where is this file? 3 26
Regarding swagger API 1 15
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

828 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