Click here to Skip to main content
15,892,575 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:        DateExpression.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:     An expression for a filter item that represents filtering on a date

Public Class DateExpression
    Inherits GenericExpression

    ''' <summary>
    '''   An enumeration of the operands available and their indices in the dropdownlist
    ''' </summary>
    ''' <remarks></remarks>
    Private Enum Operands As Integer
        Equals = 0
        NotOn = 1
        After = 2
        OnOrAfter = 3
        Before = 4
        OnOrBefore = 5
    End Enum

    ''' <summary>
    '''  Constructor
    ''' </summary>
    ''' <param name="field">Sets the name of the field to which this expression is tied</param>
    Public Sub New(ByVal field As String)

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

        ' Add any initialization after the InitializeComponent() call.
        Me.field = field
    End Sub

    ''' <summary>
    '''   Handles the changing of the operand
    ''' </summary>
    ''' <param name="sender">the sender of the event</param>
    ''' <param name="e">the arguments of the event</param>
    Public Sub OperandsList_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles OperandsList.SelectedIndexChanged
        SetValues()
    End Sub

    ''' <summary>
    '''  Handles the Date Selected event
    ''' </summary>
    ''' <param name="sender">the sender of the event.</param>
    ''' <param name="e">the arguments of the event.</param>
    Private Sub DateTimePicker1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DateTimePicker1.TextChanged
        SetValues()
    End Sub

    ''' <summary>
    '''   Sets the values of the operand, the criteria, and whether the expression should be inverted,
    '''   for use in building the filter string for this particular expression
    ''' </summary>
    Private Sub SetValues()

        If DateTimePicker1.Text.Length = 0 Then
            Me._Filter = ""
            Me.RaiseFilterChanged()
            Exit Sub
        End If

        Select Case OperandsList.SelectedIndex
            Case Operands.Equals
                Me._Filter = "(" & Me.field & " >= #" & DateTimePicker1.Text & " 12:00am# AND "
                Me._Filter &= Me.field & " <= #" & DateTimePicker1.Text & " 11:59pm#)"
            Case Operands.NotOn
                Me._Filter = "Not (" & Me.field & " >= #" & DateTimePicker1.Text & " 12:00am# AND "
                Me._Filter &= Me.field & " <= #" & DateTimePicker1.Text & " 11:59pm#)"
            Case Operands.After
                Me._Filter = Me.field & " > #" & DateTimePicker1.Text & "#"
            Case Operands.Before
                Me._Filter = Me.field & " < #" & DateTimePicker1.Text & "#"
            Case Operands.OnOrAfter
                Me._Filter = Me.field & " >= #" & DateTimePicker1.Text & "#"
            Case Operands.OnOrBefore
                Me._Filter = " <= #" & DateTimePicker1.Text & "#"
        End Select

        '' Set the readable criteria and readable operand, and raise a filter changed event
        Me.ReadableOperand = OperandsList.Text
        Me.ReadableCriteria = "'" & DateTimePicker1.Text & "'"
        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