Hi,
If the default grid filtering experience (see here and here) does not match your needs, you can also consider another built-in integration - with RadFilter: https://demos.telerik.com/aspnet-ajax/filter/examples/integration/defaultcs.aspx?product=filter.
If you would still like to proceed with custom filtering, I advise that you consider filtering the data source itself rather than hacking through the grid. You can easily do that in the NeedDataSource event by processing the data according to the desired logic before passing it to the grid.
Doing this manually is actually going to be rather hard and the provided code will have a lot of limitations. Nevertheless, I modified it so you can get started, and I also left comments with the issues and resources you can use to get things done:
Protected
Sub
ItemCommand(
ByVal
sender
As
Object
,
ByVal
e
As
GridCommandEventArgs)
Dim
filterExpression
As
String
=
String
.Empty
If
e.CommandName =
"FilterAll"
Then
Dim
cmdItem
As
GridCommandItem = calendarTableView.MasterTableView.GetItems(GridItemType.CommandItem)(0)
'TryCast(e.Item, GridCommandItem)
Dim
txtFilter = TryCast(cmdItem.FindControl(
"txtFilter"
), TextBox)
'these loops are unnecessary, it is similar to the operation that needs to be done on the data source for filtering anyway
'also,they will only go through the data on the current page as the grid creates items only for the current page (for performance reasons)
'which is why such custom filtering is best implemented over the data source rather than hacking through the grid
'also, with this code the filter expression will be applied only for the last column where a text occurence exists
For
Each
col
As
GridColumn
In
calendarTableView.MasterTableView.Columns
For
Each
dataItem
As
GridDataItem
In
calendarTableView.Items
If
dataItem(col.UniqueName).Text.Contains(txtFilter.Text)
Then
'sample to aim for
'@"it[""CustomerID""].ToString().Contains(""ALFKI"")"
filterExpression =
"it["
""
+ col.UniqueName +
""
"].ToString().Contains("
""
+ txtFilter.Text +
""
")"
calendarTableView.MasterTableView.FilterExpression = filterExpression
End
If
Next
Next
If
(
Not
String
.IsNullOrEmpty(filterExpression))
Then
'if you won't be doing operations on the grid, you do not really need to rebind it
calendarTableView.MasterTableView.Rebind()
End
If
ElseIf
e.CommandName =
"ClearFilter"
Then
Dim
cmdItem
As
GridCommandItem = calendarTableView.MasterTableView.GetItems(GridItemType.CommandItem)(0)
'Dim cmdItem As GridCommandItem = TryCast(e.Item, GridCommandItem)
Dim
txtFilter
As
TextBox = TryCast(cmdItem.FindControl(
"txtFilter"
), TextBox)
txtFilter.Text =
String
.Empty
For
Each
col
As
GridColumn
In
calendarTableView.MasterTableView.Columns
col.ListOfFilterValues =
Nothing
col.CurrentFilterFunction = GridKnownFunction.NoFilter
col.CurrentFilterValue =
String
.Empty
Next
calendarTableView.MasterTableView.FilterExpression =
String
.Empty
End
If
End
Sub
Protected
Function
GetTestData()
As
DataTable
Dim
tbl
As
New
DataTable()
tbl.Columns.Add(
New
DataColumn(
"someField"
,
GetType
(
String
)))
tbl.Columns.Add(
New
DataColumn(
"otherField"
,
GetType
(
String
)))
tbl.Columns.Add(
New
DataColumn(
"thirdField"
,
GetType
(
Integer
)))
tbl.Columns.Add(
New
DataColumn(
"fourthField"
,
GetType
(
String
)))
tbl.Rows.Add(
New
Object
() {
"first"
,
"firstRecord2"
, 1,
"A"
})
tbl.Rows.Add(
New
Object
() {
"first"
,
"secondRecord2"
, 2,
"B"
})
tbl.Rows.Add(
New
Object
() {
"third"
,
"thirdRecord2"
, 3,
"C"
})
Return
tbl
End
Function
Protected
Sub
calendarTableView_NeedDataSource(sender
As
Object
, e
As
GridNeedDataSourceEventArgs)
DirectCast
(sender, RadGrid).DataSource = GetTestData()
End
Sub
Regards,
Marin Bratanov
Progress Telerik
Get
quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers.
Learn More.