Click here to Skip to main content
15,891,136 members
Articles / Desktop Programming / Windows Forms

DataGridView Filter Popup

Rate me:
Please Sign up or sign in to vote.
3.88/5 (6 votes)
5 Jun 2009CPOL1 min read 49.4K   35   11
A cool way to filter data in a DataGridView.

Introduction

This article is about a neat way I found to filter data in a DataGridView. This works with all grid view I've ever made with very little code change.

Background

This stems from a similar solution I found. It was too complicated for a beginner like me, so I developed a solution that I could understand.

Using the Code

Step 1: add the following code to the form containing the DataGridView. In this case, the form is called "DataGridViewForm".

VB
Private Sub PartsDataGridView_ColumnHeaderMouseClick(ByVal sender As Object, _
        ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) _
        Handles PartsDataGridView.ColumnHeaderMouseClick

  Search.Show()
End Sub

Step 2: Add a form to your project called Search.vb and set the properties the way you would like. For instance, I have set the form properties FormBorderStyle=None, TopMost=True, BackgroundColor, etc.

I added a Label, TextBox, and two Buttons.

This is the code-behind of the form.

VB
Public Class Search
    Dim columnIndex As Integer
    Dim columnName As String

    Private Sub Search_Load(ByVal sender As Object, _
                ByVal e As System.EventArgs) Handles Me.Load
        Dim loc As Point
        loc = Control.MousePosition
        Me.Location = loc
        Me.TextBox1.Focus()

        columnIndex = DataGridViewForm.PartsDataGridView.SortedColumn.Index
        columnName = DataGridViewForm.PartsDataGridView.Columns(_
                            columnIndex).HeaderText.ToString
        Label1.Text = columnName
    End Sub

    Private Sub btnSearch_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnSearch.Click
        Try
            DataGridViewForm.PartsBindingSource.Filter = _
              String.Format("{0} = '{1}'", _
              DataGridViewForm.ShopPartsDataSet.Parts.Columns(_
                columnIndex).ColumnName.ToString, TextBox1.Text)
        Catch
            MessageBox.Show("Database error")
        End Try
        Me.Close()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub
End Class

As you can see in the picture, when you click on the header, a window pops up. You enter some text, a part number in this case, and hit the binoculars. Voila!! Your data is filtered.

Image 1

It's super easy!! This should work with any existing form you have out there!!

Points of Interest

I'm not an expert. One thing that I wasn't able to figure out was how to get the selected column index. So that's why I don't have the Search Form popup when I right click. I need to left click, let the DataGrid sort, and get the index of the sorted column.

Also, I added a button to my ToolStrip to clear the filter and re-fill the DataGrid.

Obviously, you will want to change the code to apply this to your project.

"DataGridViewForm" = "Yourform".
"PartsBindingSource" = "YourBindingSource"
"ShopPartsDataSet" = "YourDataSet"
"PartsDataGridView" = "YourDataGridView"

History

No history. Brand new!!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Hawk Ridge Systems
United States United States
-SolidWorks CSWP
-SolidWorks API Programmer
-Industrial engineer for 15 years
-Robot programmer for 15 years

http://jesseseger.com

Comments and Discussions

 
QuestionSource Code? Pin
Member 305299018-Dec-14 6:46
Member 305299018-Dec-14 6:46 
AnswerRe: Source Code? Pin
jesseseger18-Dec-14 6:49
professionaljesseseger18-Dec-14 6:49 
GeneralRe: Source Code? Pin
Member 126601833-Jan-22 14:53
Member 126601833-Jan-22 14:53 
QuestionNice post Pin
Tridip Bhattacharjee12-Dec-13 21:54
professionalTridip Bhattacharjee12-Dec-13 21:54 
GeneralMy vote of 5 Pin
swdev.bali4-Nov-11 3:22
swdev.bali4-Nov-11 3:22 
GeneralUpgrades [modified] Pin
aaroncampf27-Jan-11 13:37
aaroncampf27-Jan-11 13:37 
I Could not get your Control to work...I do not even know how you got the thing to appear since there was no code or explanation on how that was to happen

Luckily this was so simple that I just reworked it into a better Control

Here is the Code for that new Control
I Still need to clean all of the unused code out of it but I'm still considering how I want to get the Control to do its Filtering


I made the Filtering Locally handled in the Form
All your Control Really Does is Supply 2 Buttons and a TextBox that Appears when you Click a Column Header

Below the My Remake of your control i Added the code I used Filter my DataGridView

I must say this is an Extremely cool way to have additional controls to appear where you want them WHEN you want them!!


Public Class Search
Dim columnIndex As Integer
Dim columnName As String

Private Property IsDataGrid() As DataGridView
Private Property IsBindingSource() As BindingSource
Private Property IsParent() As Form

Sub New(ByVal MyDataGrid As DataGridView, ByVal MyBindingSource As BindingSource, ByVal MyParent As Form)
InitializeComponent() ' This call is required by the designer.

IsDataGrid = MyDataGrid
IsBindingSource = MyBindingSource
IsParent = MyParent
End Sub


Private Sub Search_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Me.TextBox1.Focus()
columnIndex = IsDataGrid.SortedColumn.Index
columnName = IsDataGrid.Columns(columnIndex).HeaderText.ToString
Label1.Text = columnName
Me.Location = Control.MousePosition
End Sub


Public Property Filter1 As String
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
'Try
' IsBindingSource.Filter = Filter1
'Catch : MessageBox.Show("Database error") : End Try

Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub

Private Sub btnRemoveFilter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveFilter.Click
Me.DialogResult = Windows.Forms.DialogResult.No
Me.Close()
End Sub


Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
Me.Close()
End Sub
End Class



Private Sub MergedDataGridView1_ColumnHeaderMouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles MergedDataGridView1.ColumnHeaderMouseClick
Dim IsSearch As New Search(MergedDataGridView1, SAdetailBindingSource, Me)
If IsSearch.ShowDialog() = Windows.Forms.DialogResult.OK Then
Select Case e.ColumnIndex
Case 0
Filter_GV2.ActivateFilter(Mods.Database.FilterType.String_Like, "ItemNo", IsSearch.TextBox1.Text)
Case 2
Filter_GV2.ActivateFilter(Mods.Database.FilterType.String_Like, "SHIPCUSTNO", ComboBox1.Text)
Case 3

Case 4

Case 5
Filter_GV2.ActivateFilter(Mods.Database.FilterType.String_Like, "CATG", IsSearch.TextBox1.Text)
Case 6
Filter_GV2.ActivateFilter(Mods.Database.FilterType.String_Like, "GROUP", IsSearch.TextBox1.Text)
End Select
ElseIf IsSearch.DialogResult = Windows.Forms.DialogResult.No Then
SAdetailBindingSource.RemoveFilter()
End If
End Sub
modified on Thursday, January 27, 2011 10:28 PM

GeneralRe: Upgrades [modified] Pin
machmuel31-May-16 21:37
machmuel31-May-16 21:37 
GeneralMy vote of 5 Pin
Global Analyser3-Nov-10 11:26
Global Analyser3-Nov-10 11:26 
QuestionAny Input?? Pin
jesseseger16-Jun-09 1:15
professionaljesseseger16-Jun-09 1:15 
AnswerRe: Any Input?? Pin
ddewart21-Jun-09 11:07
ddewart21-Jun-09 11:07 
GeneralRe: Any Input?? Pin
jesseseger21-Jun-09 16:42
professionaljesseseger21-Jun-09 16:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.