There are some cases in which you may want to reduce the filter menu options and display only a subset of the available ones. This is easily attainable following the steps below:
- hook the Init event of the grid
- traverse the items in the filtering menu and determine which of them should be removed by checking their Text attribute
- delete the items which you do not want to be rendered calling the RemoveAt(index) method of the FilterMenu.Items collection
Note: The filtering menu of Telerik RadGrid is presented by single object server-side. This way of implementation has been chosen to speed up the grid performance by merely creating one menu instance server side and cloning the instance for different columns. The filtering menu is independent for each column in RadGrid - this means that the filtering menu options vary by the DataType of the corresponding column. Hence integer column will have one set of filter menu options (EqualTo, NotEqualTo, GreaterThan, LessThan, etc.), string column will have additional options (Contains, StartsWith. etc.) and so on. However, if you remove some of the options from the menu, this will affect all grid columns and they will be stripped from each column filter menu options (if available by default for that type of column).
The code below shows how to retain only the items which has NoFilter, Contains, EqualTo, GreaterThan and LessThan text value:
| ASPX/ASCX |
Copy Code |
|
<rad:RadGrid id="RadGrid1" runat="server" AllowFilteringByColumn="true" Width="500px" /> |
And in the code-behind:
| C# |
Copy Code |
|
private void RadGrid1_NeedDataSource(object source, Telerik.WebControls.GridNeedDataSourceEventArgs e)
{ OleDbConnection MyOleDbConnection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath( "~/Grid/Data/Access/Nwind.mdb")); OleDbDataAdapter MyOleDbDataAdapter = new OleDbDataAdapter(); MyOleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT TOP 10 OrderID, OrderDate, ShipName, ShipCity FROM Orders", MyOleDbConnection);
DataTable myDataTable = new DataTable();
MyOleDbConnection. Open(); try { MyOleDbDataAdapter.Fill(myDataTable); } finally { MyOleDbConnection. Close(); }
RadGrid1.DataSource = myDataTable.DefaultView;
}
private void RadGrid1_Init(object sender, System.EventArgs e) { GridFilterMenu menu = RadGrid1.FilterMenu; int i = 0;
while(i < menu.Items.Count) { if(menu.Items[i].Text == "NoFilter" || menu.Items[i].Text == "Contains" || menu.Items[i].Text == "EqualTo" || menu.Items[i].Text == "GreaterThan" || menu.Items[i].Text == "LessThan") { i++; } else { menu.Items.RemoveAt(i); } }
}
private void RadGrid1_ColumnCreated(object sender, Telerik.WebControls.GridColumnCreatedEventArgs e) { if(e.Column.UniqueName == "OrderDate") { GridBoundColumn boundColumn = e.Column as GridBoundColumn; boundColumn.DataFormatString = "{0:d}"; } } |
| VB.NET |
Copy Code |
|
Private Sub RadGrid1_NeedDataSource([source] As Object, e As Telerik.WebControls.GridNeedDataSourceEventArgs) Handles RadGrid1.NeedDataSource
Dim MyOleDbConnection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + Server.MapPath ("~/Grid/Data/Access/Nwind.mdb")) Dim MyOleDbDataAdapter As New OleDbDataAdapter() MyOleDbDataAdapter.SelectCommand = New OleDbCommand("SELECT TOP 10 OrderID, OrderDate, ShipName, ShipCity FROM Orders", MyOleDbConnection)
Dim myDataTable As New DataTable()
MyOleDbConnection.Open() Try MyOleDbDataAdapter.Fill(myDataTable) Finally MyOleDbConnection. Close() End Try
RadGrid1.DataSource = myDataTable.DefaultView End Sub
Private Sub RadGrid1_Init(sender As Object, e As System.EventArgs) Handles RadGrid1.Init Dim menu As GridFilterMenu = RadGrid1.FilterMenu Dim i As Integer = 0
While i < menu.Items.Count If menu.Items(i).Text = "NoFilter" Or menu.Items(i).Text = "Contains" Or menu.Items(i).Text = "EqualTo" Or menu.Items(i).Text = "GreaterThan" Or menu.Items(i).Text = "LessThan" Then i = i + 1 Else menu.Items.RemoveAt(i) End If End While End Sub
Private Sub RadGrid1_ColumnCreated(sender As Object, e As Telerik.WebControls.GridColumnCreatedEventArgs) Handes RadGrid1.ColumnCreated If e.Column.UniqueName = "OrderDate" Then Dim boundColumn As GridBoundColumn = CType(e.Column, GridBoundColumn) boundColumn.DataFormatString = "{0:d}" End If End Sub |