在Ultragrid列过滤器中显示日历

3

我有一个UltraGrid,其中有许多列,其中2列是DateTime样式。现在当我使用该列的筛选器时,它会将所有DateTime值显示为下拉框中的文本。但我需要将其作为日历显示,以使筛选更加容易。只需在单击筛选时显示一个日历即可。

我尝试了一些代码,但它不起作用。

//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

点击筛选器下拉菜单时,将触发上述事件。
    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

上述方法可以使表格更改(应用设置)并显示过滤器作为日历。但这并不起作用,仍显示相同的普通表格。
我该如何实现呢?

让我更好地理解,您想要在日期时间列的标题中单击筛选器图标。您希望显示一个日历,用户可以从中选择日期,并使用所选数据过滤网格,而不是日期列表?此外,您还想包括列中不存在的日期吗? - Steve
@Steve:感谢您的回复。只需要包含所有日期的日历即可。当用户在日历中选择与网格中日期列表不匹配的日期时,网格应为空。问题是当用户单击网格的DateTime列过滤器时,应显示日历。 - A Coder
1个回答

4
我成功地在UltraWinGrid中的DateTime列上按下筛选图标时显示了一个MonthCalendar。当MonthCalendar被展示出来后,您可以使用这个标准WinForm控件所提供的熟悉接口选择特定日期。选择日期后,您可以使用该值编程方式将筛选条件应用于UltraWinGrid列。
要实现此结果,首先需要添加对Infragistics4.Win.SupportsDialog.v11.2程序集的引用,在其中可以找到UltraGridFilterUIProvider类。
现在,在需要出现筛选的窗体中添加以下代码:(这只是一个示例,因为我没有您的数据源,因此我有一个预构建的只有一个日期时间列)
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

现在我们只需要一个简单的表单,名为FormDate,其中包含一个MonthCalendar和三个按钮(Set、Clear、Cancel),它们分别返回(Set)一个日期来设置过滤器,(Clear)一个空值来删除先前的过滤器,以及一个取消按钮来中止处理。
以下是表单的代码,设计非常简单。
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

这可能解决您的问题,但是我发现UltraGridFilterUIProvider中似乎存在一个bug。当我调用e.Handled=True时,我的预期结果是过滤器不显示任何内容,但是仍然会出现一个小窗口,我必须按Escape键才能隐藏它。我还没有找到自动隐藏它的方法。
这似乎是向Infragistics团队发信号的问题。

我建议您也测试由UltraGridFilterUIProvider自动提供的Excel样式过滤接口。这个界面有很多选项,比标准UI过滤器更可取。要测试此界面,您只需注释上面的AddHandler行即可。

编辑根据@Alhalama的评论,我尝试使用BeforeRowFilterDropDown事件,结果更好了(现在完美了)。因此,我已经注释了AddHandler行,删除了BeforeMenuPopulate的代码,并添加了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

现在,当我尝试打开名为DateRequest的列的过滤器时,我立即打开了FormDate,并在最后将BeforeRowFilterDropDownEventArgs的Cancel属性设置为true,以避免进一步处理过滤对话框。这似乎是完美的......这要归功于Alhalama先生。如果你愿意,我认为你应该发表自己的答案,因为你的建议真的很有区别。

非常感谢Steve。我可以知道在哪里下载Infragistics4.Win.SupportsDialog.v11.2 dll吗? - A Coder
这是NetAdvantage套件中包含的一个程序集。你应该已经拥有它,也许是不同版本的,但我认为功能应该是相同的。查看你的引用对话框并搜索它。 - Steve
@Steve 在 BeforeMenuPopulate 事件中将 e.Handled 设置为 True 将无法阻止筛选菜单的显示。有关此操作的更多详细信息,请参阅 API 文档:http://help.infragistics.com/NetAdvantage/WinForms/Current/CLR4.0/?page=Infragistics4.Win.SupportDialogs.v13.1~Infragistics.Win.SupportDialogs.FilterUIProvider.UltraGridFilterUIProvider~BeforeMenuPopulate_EV.html - alhalama
2
@Steve 既然您提供了自己的对话框并手动添加了 FilterCondition,也许您想要处理网格的 BeforeRowFilterDropDown 事件,并在编辑 DateTime 的列时取消此操作。有关 BeforeRowFilterDropDown 事件的更多信息,请参见:http://help.infragistics.com/NetAdvantage/WinForms/Current/CLR4.0/?page=Infragistics4.Win.UltraWinGrid.v13.1~Infragistics.Win.UltraWinGrid.UltraGridBase~BeforeRowFilterDropDown_EV.html - alhalama
@alhalama 我明白了,它只是用来停止添加菜单项的。你知道有没有一种自动关闭空窗口的方法吗?无论如何,感谢你的关注。 - Steve
@alhalama 很有趣,我现在会尝试你的建议。 - Steve

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接