Click here to Skip to main content
15,892,809 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 114K   4.5K   75  
This is a dialog window that allows filtering a DataGridView. It can build filters with any depth of parentheses.
'' FILENAME:        DropDownExpression.vb
'' NAMESPACE:       PI.Dialogs.Filter
'' APPLICATION:     N/A
'' CREATED BY:      Luke Berg
'' CREATED:         8-15-06
'' REVISED BY:      Luke Berg
'' REVISED:         8-16-06
'' DESCRIPTION:     This represents an expression of a filter item that allows you to filter on
''                  a select group of items.

''' <summary>
'''   This represents an expression of a filter item that allows you to filter on
'''   a select group of items.
''' </summary>
''' <remarks>
'''  To use this expression, you will need to pass it the list of criteria with which to populate
'''  the criteria drop down list.
''' </remarks>
Public Class DropDownExpression

    ''' <summary>
    '''   The operands available for the expression
    ''' </summary>
    ''' <remarks></remarks>
    Private Enum Operands As Integer
        EqualTo = 0
        NotEqualTo = 1
    End Enum

    ''' <summary>
    '''   The datasource for the combobox of items to use as criteria
    ''' </summary>
    Public Property Items() As List(Of String)
        Get
            Return ComboBox1.DataSource
        End Get
        Set(ByVal value As List(Of String))
            ComboBox1.DataSource = value
        End Set
    End Property

    ''' <summary>
    '''  Basic constructor - no parameters
    ''' </summary>
    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

    End Sub

    ''' <summary>
    '''  More advanced constructor
    ''' </summary>
    ''' <param name="items">The list of items add to the combobox of possible criteria</param>
    Public Sub New(ByVal field As String, ByVal items As List(Of String))

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Me.Items = items
        Me.field = field

    End Sub

    ''' <summary>
    '''  Handles a selection of an operand
    ''' </summary>
    Public Sub OperandsList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OperandsList.SelectedIndexChanged
        SetValues()
    End Sub

    ''' <summary>
    '''  Handles the selection of a criteria from the possible list of criteria
    ''' </summary>
    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        SetValues()
    End Sub

    ''' <summary>
    '''  Sets the values of the expression's criteria, operand, inverse, readable operand, and readable criteria.
    '''  Called whenever the criteria or operand changes
    ''' </summary>
    Private Sub SetValues()

        '' Checks if nothing is set
        If ComboBox1.Text.Length = 0 Then
            Me._Filter = ""
            Me.RaiseFilterChanged()
            Exit Sub
        End If

        Select Case OperandsList.SelectedIndex
            Case Operands.EqualTo
                Me._Filter = Me.field & " = '" & ComboBox1.Text & "'"
            Case Operands.NotEqualTo
                Me._Filter = Me.field & " <> '" & ComboBox1.Text & "'"
        End Select

        '' Sets a readable version of the operand and readable version of the criteria
        Me.ReadableOperand = OperandsList.Text
        Me.ReadableCriteria = "'" & ComboBox1.Text & "'"

        '' Raises an event to indicate that the filter has changed.
        Me.RaiseFilterChanged()

    End Sub
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