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

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.

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
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.
Tsvetoslav
the Telerik team

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
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.
Tsvetoslav
the Telerik team

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

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

Can you show me the code I would use to loop through the filter cells as you are suggesting below.
Thank You
Attached is a small sample.
All the best,
Tsvetoslav
the Telerik team

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

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
You are welcome. Good luck with your development.
All the best, Tsvetoslav
the Telerik team

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