RadGridView - Excel filter enhancement (WinForms)

5 posts, 0 answers
  1. Theo
    Theo avatar
    19 posts
    Member since:
    Sep 2011

    Posted 31 May 2013 Link to this post

    Requirements

    RadControls version 2012.3.1017.40
    .NET version 4.0+
    Visual Studio version 2012/2010
    programming language VB.NET

    PROJECT DESCRIPTION
    This is a bit of an enhancement for the excel filtering that is built into the RadGridView.
    The tasks were:
    a) When the filter popup is opened, the search text box should have the focus.
    b) All the items in the filter list should be sorted.
    c) When the filter is applied, the first row of the remaining visible rows should be selected and current.

    SOLUTION
    1. Create a custom implementation of the RadListFilterPopup
    Imports Telerik.WinControls.UI
     
    Public Class ucRadGridViewFilterPopup
        Inherits RadListFilterPopup
     
        Public Sub New(column As GridViewDataColumn)
            MyBase.New(column)
        End Sub
     
        Protected Overrides Sub OnPopupOpened()
            MyBase.OnPopupOpened()
            Try
                If TypeOf Me.DataColumn Is GridViewMultiComboBoxColumn OrElse TypeOf Me.DataColumn Is GridViewComboBoxColumn Then
                    Me.MenuTreeElement.TreeView.Nodes.Where(Function(node) node.Text.ToLower = "all").FirstOrDefault().TreeView.SortOrder = SortOrder.Ascending
                End If
            Catch ex As Exception
                Console.WriteLine(ex.Message)
            End Try
            Me.TextBoxMenuItem.TextBox.TextBoxElement.TextBoxItem.TextBoxControl.Focus()
        End Sub
    End Class

    This is a 2-fold solution.

    Firstly (b) you check if the column is a look-up type column. In the case that the data source is linked with a foreign key relationship, the bound value of the column would be an integer which is the identity value for the row that this record relates to in a child relationship. Due to this, the sorting in the filter list happens ascending on the numerical value and not on the text display value. This in turn causes the list to look "unsorted". So we force sort the display values that are under the ALL node in the tree view.

    Secondly (a), we get access to the search text box in the popup and set the focus to the control so that when the popup opens or displays, you can just start typing to search for the value in the column.


    Add these methods to the form where the grid is hosted.

    2. Use the new popup implementation in the grid
    ''' <summary>
    ''' Custom implementation of the filter popup so that the Search text box has the focus by default when the popup is opened
    ''' and also the values to filter on are sorted in ascending order
    ''' </summary>
    ''' <param name="sender">Column filter popup that initiates the event</param>
    ''' <param name="e">Arguments surrounding the event invocation</param>
    Private Sub dgvGrid_FilterPopupRequired(sender As Object, e As FilterPopupRequiredEventArgs) Handles dgvCulminGrid.FilterPopupRequired
        Try
            e.FilterPopup = New ucRadGridViewFilterPopup(e.Column)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub


    3. Thirdly (c) ensure that after filter is applied, the first row in the remaining visible rows is selected and also the current row
    Private Sub dgv_FilterChanged(sender As Object, e As GridViewCollectionChangedEventArgs) Handles grid.FilterChanged
        Try
            With grid
                If .MasterView.Rows.Count > 0 Then
                    .CurrentRow = .MasterView.Rows(0)
                    'Refresh the childgrids
                    'RefreshChildGrids(.MasterView.Rows(0).Cells(keyFieldName).Value)
                End If
            End With
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
    End Sub
    As soon as the filter has been applied, check that there are remaining rows visible. If so, set the current row to the first row in the collection (grid.rows is the datasource rows, grid.masterview.rows is the visible row collection). We also take this opportunity to update and reload all the related grids on the screen by using the unique key from the master record to update related child record grids.


    So that is it, simple yet very effective to give your users that polished feel.

    Hope you find this informative and possibly open some doors for even more functionality.

    Thank you
    Theo Jacobs

    P.S. Leave a comment if you think this is worth some much loved Telerik Points or if this helped you in any way. cheers
  2. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 04 Jun 2013 Link to this post

    Hi Theo,

    Thank you for sharing your solution with the community. I am sure it will benefit from it.

    Your
    Telerik Points have been updated accordingly.

    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  3. Theo
    Theo avatar
    19 posts
    Member since:
    Sep 2011

    Posted 12 Jun 2013 Link to this post

    Hi there

    I have extended the above example to also address a PITS issue (http://www.telerik.com/support/pits.aspx#/public/winforms/10850). Attached you will find a solution containing both VB.NET and C# project with internal documentation to explain.

    Please refer to the above post for further explanation as the attached project contains most of the above code (excluding the front end set current row to first visible row after filter)

    Let me know if you require assistance, I would be more than glad to help where I can.

    Thank you
    Theo Jacobs
  4. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 14 Jun 2013 Link to this post

    Thank you Theo.

    Regards,
    Stefan
    Telerik
    RadChart for WinForms is obsolete. Now what?
  5. Jared
    Jared avatar
    6 posts
    Member since:
    Oct 2015

    Posted 23 Aug Link to this post

    Very helpful Theo.  Thanks for the solution!
Back to Top