Click here to Skip to main content
15,884,298 members
Articles / Web Development / ASP.NET

A Filter Dialog for a DataGridView

Rate me:
Please Sign up or sign in to vote.
4.04/5 (9 votes)
28 Sep 20067 min read 113.8K   4.5K   75  
This is a dialog window that allows filtering a DataGridView. It can build filters with any depth of parentheses.
'' FILENAME:        FilterItem.vb
'' NAMESPACE:       PI.Dialogs.Filter
'' APPLICATION:     N/A
'' CREATED BY:      Luke Berg
'' CREATED:         8-14-06
'' REVISED BY:      Luke Berg
'' REVISED:         8-29-06
'' DESCRIPTION:     An individual line (item) of the full filter string

Public Class FilterItem
    Implements IFilterItem

    Private expression As GenericExpression '' A control that contains an operand and criteria

    ''' <summary>
    '''   An enumeration of the different types of filter expressions
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum FieldTypes
        NumberExpression
        DateExpression
        StringExpression
        DropDownExpression
    End Enum

    Public Event SelectedAndOr(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangedEventArgs)
    Public Event SelectedEnd(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangedEventArgs)
    Public Event SelectedDelete(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangingEventArgs)
    Public Event SelectedInsert(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangingEventArgs)
    Public Event SelectedMakeSubgroup(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangingEventArgs)
    Public Event FilterChanged(ByVal sender As Object, ByVal e As EventArgs)

#Region "Properties"

    ''' <summary>
    '''   The type of the field (or column) of the filter item.
    ''' </summary>
    ''' <value>One of the FilterItem.FieldTypes</value>
    ''' <remarks>Used to change the operands and criteria editor</remarks>
    Public WriteOnly Property FieldType() As FieldTypes
        Set(ByVal value As FieldTypes)

            If FieldsList.SelectedItem Is Nothing Then
                Exit Property
            End If

            '' Removes the handler from the old expression object
            If Not expression Is Nothing Then
                RemoveHandler expression.FilterChanged, AddressOf Expression_FilterChanged
            End If

            '' creates a new expression object based on the field type selected
            Select Case value
                Case FieldTypes.StringExpression
                    expression = New StringExpression(FieldsList.SelectedItem.Value)
                Case FieldTypes.DateExpression
                    expression = New DateExpression(FieldsList.SelectedItem.Value)
                Case FieldTypes.NumberExpression
                    expression = New NumberExpression(FieldsList.SelectedItem.Value)
                Case FieldTypes.DropDownExpression
                    Dim fieldItem As FilterItemField
                    fieldItem = FieldsList.Items(FieldsList.SelectedIndex)
                    expression = New DropDownExpression(FieldsList.SelectedItem.Value, fieldItem.DistinctValues)
            End Select

            '' clears the old expression object
            ExpressionPanel.Controls.Clear()

            '' adds a handler to the new expression object, and adds it in place of the old.
            AddHandler expression.FilterChanged, AddressOf Expression_FilterChanged
            ExpressionPanel.Controls.Add(expression)

            RaiseEvent FilterChanged(Me, New EventArgs)
        End Set
    End Property

    ''' <summary>
    '''   Allows setting or retrieving of the menu for the filter item.
    ''' </summary>
    Public Property SelectedMenuItem() As FilterItemMenuButton.Items Implements IFilterItem.SelectedMenuItem
        Get
            Return FilterMenu.Item
        End Get
        Set(ByVal value As FilterItemMenuButton.Items)
            FilterMenu.Item = value
        End Set
    End Property

    ''' <summary>
    '''   The filter built by the individual item.
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Filter() As String Implements IFilterItem.Filter
        Get
            Return expression.Filter
        End Get
    End Property

    ''' <summary>
    '''  A human readable filter string that represents the actual filter string
    ''' </summary>
    Public ReadOnly Property ReadableFilter() As String Implements IFilterItem.ReadableFilter
        Get
            Try
                If expression.ReadableCriteria.Length > 0 And expression.ReadableOperand.Length > 0 Then
                    Return FieldsList.Text & " " & expression.ReadableOperand & " " & expression.ReadableCriteria
                End If
            Catch ex As Exception
            End Try
            Return ""
        End Get
    End Property

    ''' <summary>
    '''   The boolean conjunction to follow the filter
    ''' </summary>
    Public ReadOnly Property Conjunction() As String Implements IFilterItem.Conjunction
        Get
            If Me.SelectedMenuItem = FilterItemMenuButton.Items.AndItem Then
                Return "AND"
            ElseIf Me.SelectedMenuItem = FilterItemMenuButton.Items.OrItem Then
                Return "OR"
            Else
                Return ""
            End If
        End Get
    End Property

    ''' <summary>
    '''   The list of fields to be filtered on
    ''' </summary>
    ''' <value>A List of filterItemFields</value>
    ''' <returns>An list of filterItemFields</returns>
    ''' <remarks></remarks>
    Public Property Fields() As List(Of FilterItemField)
        Get
            Return FieldsList.DataSource
        End Get
        Set(ByVal value As List(Of FilterItemField))
            FieldsList.DataSource = value
            FieldsList.DisplayMember = "Display"
            FieldsList.ValueMember = "Value"
        End Set
    End Property

#End Region

#Region "Constructors"

    ''' <summary>
    '''  Basic constructor used by the designer
    ''' </summary>
    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.SelectedMenuItem = FilterItemMenuButton.Items.EndItem
    End Sub

    ''' <summary>
    '''  Constructor
    ''' </summary>
    ''' <param name="type">The type of expression (operand and criteria) to begin the filter with</param>
    ''' <param name="fields">The list used to populate the dropdownlist of fields to filter on</param>
    Public Sub New(ByVal type As FieldTypes, ByVal fields As List(Of FilterItemField))
        ' require by the designer
        InitializeComponent()

        ' initializes the properties
        Me.FieldType = type
        Me.Fields = fields
        Me.SelectedMenuItem = FilterItemMenuButton.Items.EndItem
    End Sub

#End Region

#Region "Handlers"

    ''' <summary>
    '''   Handles the changing of the selected field to filter on.
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub FieldsList_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles FieldsList.TextChanged
        Dim cb As ComboBox = CType(sender, ComboBox)

        '' Sets the type of the filter item to the type of the field that was selected
        Try
            Dim fieldItem As FilterItemField
            fieldItem = cb.Items(cb.SelectedIndex)
            Me.FieldType = fieldItem.Type
        Catch ex As Exception

        End Try
    End Sub

    ''' <summary>
    '''  Handles the selection changed event of the filterGroupMenuButton, and raises the corresponding events
    ''' </summary>
    ''' <param name="sender">the sender of the event</param>
    ''' <param name="e">the arguments of the event</param>
    Private Sub FilterMenu_SelectionChanged(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangedEventArgs) Handles FilterMenu.SelectionChanged
        Select Case e.NewSelectedIndex
            Case FilterItemMenuButton.Items.AndItem, FilterItemMenuButton.Items.OrItem
                RaiseEvent SelectedAndOr(Me, e)
            Case FilterItemMenuButton.Items.EndItem
                RaiseEvent SelectedEnd(Me, e)
        End Select
    End Sub

    ''' <summary>
    '''  Handles the filter menu selection changing event.  If the selection is delete, insert or make subgroup, then the
    '''  event is canceled (just so the checkbox position doesn't change), and an event is raised
    ''' </summary>
    ''' <param name="sender">the sender of the event</param>
    ''' <param name="e">the arguments of the event</param>
    Private Sub FilterMenu_SelectionChanging(ByVal sender As Object, ByVal e As MenuButton.MenuButtonSelectionChangingEventArgs) Handles FilterMenu.SelectionChanging
        Select Case e.NewSelectedIndex
            Case FilterItemMenuButton.Items.Delete
                e.Cancel = True
                RaiseEvent SelectedDelete(Me, e)
            Case FilterItemMenuButton.Items.Insert
                e.Cancel = True
                RaiseEvent SelectedInsert(Me, e)
            Case FilterItemMenuButton.Items.MakeSubgroup
                e.Cancel = True
                RaiseEvent SelectedMakeSubgroup(Me, e)
        End Select
    End Sub

    ''' <summary>
    '''  Handles the filter changed event of the expression object, and raises it's own event.
    ''' </summary>
    ''' <param name="sender">the sender of the event</param>
    ''' <param name="e">the arguments of the event</param>
    Private Sub Expression_FilterChanged(ByVal sender As Object, ByVal e As EventArgs)
        RaiseEvent FilterChanged(Me, e)
    End Sub

#End Region
End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions