Show calendar in Ultragrid Column Filter

天涯浪子 提交于 2019-12-13 15:19:17

问题


I have an UltraGrid in which I have many columns of which 2 columns are DateTime style. Now when I use the filter of that columns it shows all the DateTime values as a text in a dropdown. But I need that as a calendar in order to make the filter easy. It is enough to show just a calendar when clicking the filter.

I have tried some code but it doesn't work.

//Code:

Private Sub grdResult_BeforeRowFilterDropDown(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinGrid.BeforeRowFilterDropDownEventArgs) Handles grdResult.BeforeRowFilterDropDown
                e.Cancel = True
                UltraCalendarCombo1.Visible = True
               UltraCalendarCombo1.Location = New Point(grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Location.X, grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Location.Y - 2)
                UltraCalendarCombo1.Size = New System.Drawing.Size(grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Size.Width, grdResult.Rows.FilterRow.Cells(e.Column).GetUIElement().Rect.Size.Height)
                ' UltraCalendarCombo1.DroppedDown = True

            End Sub

The above event will fire when the filter dropdown is clicked.

    private sub applyCustomeViewSettings(byval gridFormat as GridFormat)
    ....
    ...
       For Each ColumnFormat In gridFormat.ColumnFormats

                    For Each column In Me.grdResult.DisplayLayout.Bands(0).Columns

                        If column.Key.ToUpper = ColumnFormat.ColumnKey.ToUpper Then
                            If column.Key.ToUpper = "PCSSTDT" Then
                                column.Header.Caption = IIf(ColumnFormat.Caption = "", ColumnFormat.ColumnKey, ColumnFormat.Caption)
                                column.Hidden = ColumnFormat.Hidden
                                'column.AllowRowFiltering = IIf(ColumnFormat.AllowRowFiltering = False, ColumnFormat.AllowRowFiltering, DefaultableBoolean.True) 'CType(ColumnFormat.AllowRowFiltering, DefaultableBoolean)
                                column.Width = ColumnFormat.Width
                                column.Header.VisiblePosition = ColumnFormat.VisiblePosition
                                column.Format = ColumnFormat.Format
                                column.SortIndicator = ColumnFormat.SortIndicator
                                ' column.Style = ColumnStyle.Date
                                'column.EditorComponent = UltraCalendarCombo1
                                column.FilterOperandStyle = FilterOperandStyle.Default

                            Else
                                column.Header.Caption = IIf(ColumnFormat.Caption = "", ColumnFormat.ColumnKey, ColumnFormat.Caption)
                                column.Hidden = ColumnFormat.Hidden
                                column.AllowRowFiltering = IIf(ColumnFormat.AllowRowFiltering = False, ColumnFormat.AllowRowFiltering, DefaultableBoolean.True) 'CType(ColumnFormat.AllowRowFiltering, DefaultableBoolean)
                                column.Width = ColumnFormat.Width
                                column.Header.VisiblePosition = ColumnFormat.VisiblePosition
                                column.Format = ColumnFormat.Format
                                column.SortIndicator = ColumnFormat.SortIndicator
                                column.Style = ColumnFormat.Style
                            End If
                        End If

                    Next
    ....
    ...

    End Sub

The above method makes the grid changes(apply settings) to show the filter as calendar. But this doesn't work and showing me the same normal grid.

How can I achieve this?


回答1:


I have managed to show a MonthCalendar when you press the icon to filter a DateTime column in an UltraWinGrid.
When the MonthCalendar is dispayed you could select a specific date using the familiar interface provided by this standard WinForm control. After selecting the date you could use the value to apply programmatically a filter condition to the UltraWinGrid column.

To reach this result you first need to add a reference to the Infragistics4.Win.SupportsDialog.v11.2 assembly where you can find the UltraGridFilterUIProvider class

Now, in your form where you need the filtering to appear, add this code: (it is just an example because I haven't your datasource and thus I have a prebuilt one with just one datetime column)

Imports Infragistics.Win.UltraWinGrid
Imports Infragistics.Win.SupportDialogs.FilterUIProvider

Public Class Form1
    ' This is the key object that let us customize ' 
    ' the Filter for the UltraWinGrid'
    Dim _filterUIProvider as UltraGridFilterUIProvider

    ' In the InitializeLayout event we substitute the normal 
    ' filter handler with the custom filter'
    Private Sub UltraGrid1_InitializeLayout(sender As Object, e As Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs) Handles UltraGrid1.InitializeLayout

        e.Layout.Override.AllowRowFiltering = Infragistics.Win.DefaultableBoolean.True
        _filterUIProvider = New UltraGridFilterUIProvider()

        ' Comment out the following line to test the default 
        ' **Excel Filter Style Interface** '
        AddHandler _filterUIProvider.BeforeMenuPopulate, AddressOf _filterUIProvider_BeforeMenuPopulate
        e.Layout.Override.FilterUIProvider = _filterUIProvider
    End Sub

    ' Before the UltraGridFilterUIProvider shows its standard form interface 
    ' we start a custom form  used to apply our filtering logic '
    ' and block the display of the UltraGridFilterUIProvider interface '
    Private Sub _filterUIProvider_BeforeMenuPopulate(sender As Object, e As Infragistics.Win.SupportDialogs.FilterUIProvider.BeforeMenuPopulateEventArgs)

        ' A custom form with the MonthCalendar and 3 buttons '
        ' to handle the filter logic '
        Using fDate = new FormDate() 

            ' Open our custom form with the monthcalendar
            if (DialogResult.OK = fDate.ShowDialog())  

                ' We need a nullable date to allow the removing of the filter'
                Dim dtFilter As DateTime? = fDate.SelectedDate
                if (dtFilter.HasValue)

                    ' Apply programmatically a filtercondition to the column 
                    ' In this case I have only one column. so I use the index 0
                    ' in your case this should change to reflect your column index
                    Dim fc = new FilterCondition(FilterComparisionOperator.Equals, dtFilter.Value)
                    ultraGrid1.DisplayLayout.Bands(0).ColumnFilters(0).FilterConditions.Add(fc)

                Else
                    ultraGrid1.DisplayLayout.Bands(0).ColumnFilters.ClearAllFilters()
                End If
            End If
        End Using
        e.Handled = true ' Stop the standard interface'
    End Sub
End Class

Now we need only a simple form called FormDate that contains a MonthCalendar and three buttons (Set, Clear, Cancel) that return (Set) a Date to set the filter, (Clear) a null value to remove previous filter and a cancel button to abort the processing

Here the code for the form, the design is trivial

Public Class FormDate
    Public Property SelectedDate As DateTime?

    Private Sub FormDate_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.SelectedDate = Nothing
    End Sub

    Private Sub cmdSet_Click(sender As Object, e As EventArgs) Handles cmdSet.Click
        'This button has DialogResult=OK'
        Me.SelectedDate = monthCalendar1.SelectionStart
    End Sub

    Private Sub cmdClear_Click(sender As Object, e As EventArgs) Handles cmdClear.Click
        'This button has DialogResult=OK'
        Me.SelectedDate = Nothing
    End Sub
End Class

This could resolve your problem, however, I have discoverd what seems to be a bug in UltraGridFilterUIProvider. When I call the e.Handled=True my expected result is the filter to not show anything but, instead a small window still appears and I have to press Escape to hide it. I have not found any way to automatically hide it.
It seems to be a problem to signal to the Infragistics team.

I suggest you also to test the Excel Style Filter Interface provided automatically by the UltraGridFilterUIProvider. This interface has a lot of options and is much more preferable to the standard UI filter. To test this interface you should only comment out the AddHandler line above

EDIT Following the comment from @Alhalama I have tried to use the BeforeRowFilterDropDown event and the result are better (well it is perfect now). So I have commented out the line with the AddHandler, removed the code for the BeforeMenuPopulate and added the code for the BeforeRowFilterDropDown

Private Sub UltraGrid1_BeforeRowFilterDropDown(sender As Object, e As BeforeRowFilterDropDownEventArgs) Handles UltraGrid1.BeforeRowFilterDropDown
    If e.Column.Key = "DateRequest" Then
        Using fDate = New FormDate()
            If DialogResult.OK = fDate.ShowDialog() Then
                Dim dtFilter As DateTime? = fDate.SelectedDate
                If (dtFilter.HasValue) Then
                    Dim fc As FilterCondition = New FilterCondition(FilterComparisionOperator.Equals, dtFilter.Value)
                    UltraGrid1.DisplayLayout.Bands(0).ColumnFilters(0).FilterConditions.Add(fc)
                Else
                    UltraGrid1.DisplayLayout.Bands(0).ColumnFilters.ClearAllFilters()
                End If
            End If
        End Using
        e.Cancel = True
    End If
End Sub

Now, when I try to open the filter for the column named DateRequest I open immediately the FormDate and at the end I set the Cancel property of the BeforeRowFilterDropDownEventArgs to true to avoid further processing of the filter dialog. This seems to be perfect...... Great credit for this to Mr. Alhalama. If you wish I think you should post your own answer because your suggestion really makes the difference.



来源:https://stackoverflow.com/questions/17943824/show-calendar-in-ultragrid-column-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!