Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-10-28
12
Medium Priority
?
1,161 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 2000 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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