This is a migrated thread and some comments may be shown as answers.

Loop Through The Filter Items on A Grid

16 Answers 509 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tracy
Top achievements
Rank 1
Tracy asked on 08 Feb 2012, 10:02 PM
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

16 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Feb 2012, 05:31 AM
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.
0
Tracy
Top achievements
Rank 1
answered on 09 Feb 2012, 06:18 PM
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
0
Tsvetoslav
Telerik team
answered on 10 Feb 2012, 09:38 AM
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 >>
0
Tracy
Top achievements
Rank 1
answered on 10 Feb 2012, 10:22 PM
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 
0
Tsvetoslav
Telerik team
answered on 13 Feb 2012, 12:54 PM
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 >>
0
Tracy
Top achievements
Rank 1
answered on 13 Feb 2012, 01:23 PM
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

0
Tsvetoslav
Telerik team
answered on 13 Feb 2012, 06:19 PM
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 >>
0
Tracy
Top achievements
Rank 1
answered on 13 Feb 2012, 10:43 PM
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
0
Tsvetoslav
Telerik team
answered on 16 Feb 2012, 05:26 PM
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 >>
0
Tracy
Top achievements
Rank 1
answered on 16 Feb 2012, 05:40 PM
Hi Tsvetoslav,

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

Thank You
0
Accepted
Tsvetoslav
Telerik team
answered on 21 Feb 2012, 10:15 AM
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 >>
0
Tracy
Top achievements
Rank 1
answered on 22 Feb 2012, 01:49 AM
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
0
Tsvetoslav
Telerik team
answered on 24 Feb 2012, 08:12 AM
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.
0
Tracy
Top achievements
Rank 1
answered on 26 Feb 2012, 04:01 AM
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
0
Accepted
Tsvetoslav
Telerik team
answered on 29 Feb 2012, 02:20 PM
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.
0
Kader
Top achievements
Rank 1
answered on 04 Jun 2015, 10:12 AM

Please try this.

 

Private Sub grd_ItemCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridItemEventArgs) Handles grd.ItemCreated
If TypeOf e.Item Is GridFilteringItem Then
Dim filteringItem As GridFilteringItem = CType(e.Item, GridFilteringItem)

For Each Cell As GridTableCell In filteringItem.Cells

If TypeOf Cell.Column Is GridBoundColumn Then

Dim column = DirectCast(Cell.Column, GridBoundColumn)

column.HeaderStyle.HorizontalAlign = HorizontalAlign.Center

If column.UniqueName = "Name" Then
Dim Name_FB As TextBox = CType(filteringItem("Name").Controls(0), TextBox)
  Name_FB.Width = Unit.Pixel(100)
column.HeaderStyle.Width = Unit.Pixel(100)

Else
Dim txt_FB As TextBox = CType(filteringItem(column.UniqueName).Controls(0), TextBox)
txt_FB.Width = Unit.Pixel(40)
column.HeaderStyle.Width = Unit.Pixel(40)
End If

End If


Next

End If

End Sub

Tags
Grid
Asked by
Tracy
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Tracy
Top achievements
Rank 1
Tsvetoslav
Telerik team
Kader
Top achievements
Rank 1
Share this question
or