Loop Through The Filter Items on A Grid

16 posts, 2 answers
  1. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 08 Feb 2012 Link to this post

    Hi,

    Can you tell me how to loop through all the filter items on a grid and get the unique name, type and selected value of each item.  I would like to achieve this on a button click.

    For Example,

    Protected

     

    Sub btnSettings_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSettings.Click

     

     

    For Each filterItem As GridFilteringItem In rgvEmployees.MasterTableView.GetItems(GridItemType.FilteringItem)

     

     

    'Loop through each item and get uniquename, type and selectedvalue        

     

     

    Next

     


    Thank you for your help.
    Tracy

  2. Princy
    Princy avatar
    17224 posts
    Member since:
    Mar 2007

    Posted 08 Feb 2012 Link to this post

    Hello,

    Try the following code.
    VB:

    Protected Sub Button1_Click(sender As Object, e As EventArgs)
        For Each item As GridFilteringItem In RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)
            For Each column As GridColumn In RadGrid1.MasterTableView.Columns
                Dim uname As String = column.UniqueName
            Next
        Next
    End Sub

    Thanks,
    Princy.

  3. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 09 Feb 2012 Link to this post

    Hi Princy,

    Thanks for your response. 

    I tried the code you sent and it is not returning the filter columns, it is returning the dataitem columns in the grid.  Can you tell me how to get the filter columns .

    Thank You
    Tracy

  4. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 10 Feb 2012 Link to this post

    Hi Tracy,

    The columns in the grid are a single set no matter if some of those are filtered on or not. So what you need to do is: while looping through the Columns collection of the grid, check if the columns is a GridBoundColumn (or any other type of column you might have), cast the column to the respective type and inspect its CurrentFilterValue and CurrentFilterFunction properties. If a filter expression is applied on a column, those properties will be set to the values selected.

    Hope it helps.

    Regards,
    Tsvetoslav
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  5. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 10 Feb 2012 Link to this post

    Hi Tsvetoslav,

    Based on your post I was able to get the filter columns using the following code

    For Each fltItem As GridFilteringItem In rgvEmployees.MasterTableView.GetItems(GridItemType.FilteringItem)
        For Each column As GridColumn In rgvEmployees.MasterTableView.Columns
            If column.ColumnType = "GridBoundColumn" Or column.ColumnType = "GridTemplateColumn" Then
                If column.ColumnType = "GridBoundColumn" Then
                    strFilterName = column.UniqueName
                ElseIf column.ColumnType = "GridTemplateColumn" Then
                    strFilterName = Right(column.UniqueName, Len(column.UniqueName) - 3)
                End If
                If Not TryCast(fltItem(column.UniqueName).FindControl(strFilterName), RadComboBox) Is Nothing Then
                    Dim rcbFilter As RadComboBox = DirectCast(fltItem(column.UniqueName).FindControl("rcbFilter" + strFilterName), RadComboBox)
                    strFilterFields = strFilterFields + strFilterName + "," + rcbFilter.Text + ";"
                End If
            End If
        Next
    Next

    But I should have been more clear on what I am trying to do.  I have filter templates on each of my columns in my grid and I am trying to loop through all the columns on the grid and determine if the column has a filter template associated with it.  If it does I would like to retrieve the control within the filter template and get its unique name, text value, selected index, type of control.  I want to be able to do this without knowing what the unique name of the filter template is.
    Tracy 

  6. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 13 Feb 2012 Link to this post

    Hi Tracy,

    I could not understand from your last ticket if you have managed to fulfill your requirement. Going by your code snippet your approach is the right one.

    Regards,
    Tsvetoslav
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  7. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 13 Feb 2012 Link to this post

    Hi Tsvetoslav,

    Actually my question hasn't been answered,  I should have been more clear on what I am trying to do.  I have filter templates on each of my columns in my grid and I am trying to loop through all the columns on the grid and determine if the column has a filter template associated with it.  If it does I would like to retrieve the control within the filter template and get its unique name, text value, selected index, type of control.  I want to be able to do this without knowing what the unique name of the filter template is.


    Thank you for your assistance.

    Tracy

  8. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 13 Feb 2012 Link to this post

    Hi Tracy,

    In you case, you should check the columns' FilterTemplate property if it is null or not and if not, retrieve the filter control through the FindControl method using the approach you have already taken.

    Greetings,
    Tsvetoslav
    the Telerik team

    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  9. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 13 Feb 2012 Link to this post

    Hi Tsvetoslav,

    I can loop through the columns and find the ones with a filter template but your suggestion on using the Find Control method is working because the find control method requires that I know the unique name of the filter column.  One of the values I am trying to get is the unique name of the filter template.  I want to be able to loop through the controls and retrieve the unique name of the control in the filter template.

    Thank You

  10. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 16 Feb 2012 Link to this post

    Hi Tracy,

    I am confused on what you are trying to achieve. Anyhow, if you need to loop through the controls in the filter template, just loop through the Controls collection of the filter cell. And the filter cell you get by indexing into the filter item by unique column name.

    Regards, Tsvetoslav
    the Telerik team

    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  11. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 16 Feb 2012 Link to this post

    Hi Tsvetoslav,

    Can you show me the code I would use to loop through the filter cells as you are suggesting below.

    Thank You

  12. Answer
    Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 21 Feb 2012 Link to this post

    Hi Tracy,

    Attached is a small sample.

    All the best,
    Tsvetoslav
    the Telerik team

    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

  13. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 21 Feb 2012 Link to this post

    Hi  Tsvetoslav,

    Thank you for the sample project but this is not solving my problem. Your solution assumes that I know the unique name of the filter template.  I want to be able to loop through the filter templates and retrieve the unique name.

    Thanks

    Tracy

  14. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 24 Feb 2012 Link to this post

    Hello Tracy,

    I don't see how I use the name of the template anywhere in my code - all I have employed is the ID of the combo box within the template. However, you don't need to know this as well and that's why I added the following comment below the statements that get hold of the combo box object:

    //NB: Besides, filterItem[column.UniqueName].Controls will give you access to all the controls rendered by the filter template of the column

    This means that by iterating through the Controls collection mentioned above you can get reference of the controls required.

    Greetings,
    Tsvetoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

  15. Tracy
    Tracy avatar
    114 posts
    Member since:
    Nov 2010

    Posted 25 Feb 2012 Link to this post

    Hi Tsvetoslav,

    Thank you for the sample.   I tried converting the code to VB through Telerik's code converter and it wouldn't work therefore I converted it to VB myself and converted it incorrectly and therefore misread the code.  I have since successfully converted the code to VB and now have an understanding of what you were showing me in the sample you provided.

    Your sample provided me with the solution I was looking for.

    Thank you for your assistance.

    Tracy

  16. Answer
    Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts
    Member since:
    Sep 2012

    Posted 29 Feb 2012 Link to this post

    Hi Tracy,

    You are welcome. Good luck with your development.


    All the best, Tsvetoslav
    the Telerik team

    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.

Back to Top