Programmatic filtering on Sever-side
Environment
| Product | Telerik WebForms Grid for ASP.NET AJAX |
Description
How can I filter the Telerik WebForms Grid programmatically on Sever-side (in CodeBehind) or apply a default filter on initial load?
Solution
To filter the Grid programmatically on Server-side, you will need to configure the Columns and trigger the Filter command by using the FireCommandEvent() method.
In this article, we will show examples for each Filter Type:
Example for applying default filter at initial load
Classic
When using the FilterType="Classic", you will need to:
Set the column's CurrentFilterFunction property to any function other than NoFilter.
RadGrid1.MasterTableView.GetColumnSafe("OrderID").CurrentFilterFunction = GridKnownFunction.EqualTo;Access the column's filter Control within the GridHeaderItem and set its value to the value you would like to filter by.
GridFilteringItem filteringItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
(filteringItem["OrderID"].Controls[0] as RadNumericTextBox).Value = 1;Trigger the filter command using the FireCommandEvent(commandName, commandArguments) method of the GridFilteringItem object.
The FireCommandEvent() method parameters:
- The
commandNameparameter is the name of the command to be triggered, in this caseFilter. You can also use the constantRadGrid.FilterCommandNamewhich has the same value. - The
commandArgumentsparameter is aPairobject where:Firstproperty requires the column's UniqueName.Secondproperty requires thestringrepresentation of the filter function.
GridFilteringItem filteringItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
GridColumn orderIdColumn = RadGrid1.MasterTableView.GetColumnSafe("OrderID");
Pair filterArgs = new Pair()
{
First = orderIdColumn.UniqueName,
Second = orderIdColumn.CurrentFilterFunction.ToString()
};
filteringItem.FireCommandEvent(RadGrid.FilterCommandName, filterArgs);The Filter command can only be triggered for one column at a time, however, this will filter multiple columns if they are all prepared in the same way as described above.
Example: Applying the filters on Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
GridTableView tableView = RadGrid1.MasterTableView;
GridFilteringItem filteringItem = tableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
if (filteringItem != null)
{
(filteringItem["OrderID"].Controls[0] as RadNumericTextBox).Value = 1;
tableView.GetColumnSafe("OrderID").CurrentFilterFunction = GridKnownFunction.EqualTo;
(filteringItem["ShipName"].Controls[0] as TextBox).Text = "Name 1";
tableView.GetColumnSafe("ShipName").CurrentFilterFunction = GridKnownFunction.Contains;
(filteringItem["ShipCountry"].Controls[0] as TextBox).Text = "Country 1";
tableView.GetColumnSafe("ShipCountry").CurrentFilterFunction = GridKnownFunction.StartsWith;
string columnUniqueName = "OrderID";
string columnFilterFunction = GridKnownFunction.EqualTo.ToString();
Pair filterArgs = new Pair()
{
First = columnUniqueName,
Second = columnFilterFunction
};
filteringItem.FireCommandEvent(RadGrid.FilterCommandName, filterArgs);
}
}CheckList
When using the FilterType="CheckList", you will need to:
Populate the column's ListOfFilterValues property with the desired values.
RadGrid1.MasterTableView.GetColumnSafe("OrderID").ListOfFilterValues = new string[] { "1", "2", "3" };Set the column's CurrentFilterFunction property to GridKnownFunction.EqualTo.
RadGrid1.MasterTableView.GetColumnSafe("OrderID").CurrentFilterFunction = GridKnownFunction.EqualTo;
FilterType="CheckList"only works with the filter functionsGridKnownFunction.EqualTo(to filter) andGridKnownFunction.NoFilter(to clear the filter).
Trigger the Filter command using the FireCommandEvent(commandName, commandArguments) method of the GridFilteringItem object.
The FireCommandEvent() method parameters are:
- The
commandNameparameter is the name of the command to be triggered, in this caseFilter. You can also use the constantRadGrid.FilterCommandNamewhich has the same value. - The
commandArgumentsparameter is aPairobject where:Firstproperty requires the column's UniqueName.Secondproperty requires thestringrepresentation of the column's filter function.
GridFilteringItem filteringItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
GridColumn orderIdColumn = RadGrid1.MasterTableView.GetColumnSafe("OrderID");
Pair filterArgs = new Pair()
{
First = orderIdColumn.UniqueName,
Second = orderIdColumn.CurrentFilterFunction.ToString()
};
filteringItem.FireCommandEvent(RadGrid.FilterCommandName, filterArgs);The Filter command can only be triggered for one column at a time, however, this will filter multiple columns if they are all prepared in the same way as described above.
Example: Applying the filters on Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
GridTableView tableView = RadGrid1.MasterTableView;
tableView.GetColumnSafe("OrderID").ListOfFilterValues = new string[] { "1", "2", "3" };
tableView.GetColumnSafe("OrderID").CurrentFilterFunction = GridKnownFunction.EqualTo;
tableView.GetColumnSafe("ShipName").ListOfFilterValues = new string[] { "Name 1", "Name 2", "Name 3" };
tableView.GetColumnSafe("ShipName").CurrentFilterFunction = GridKnownFunction.EqualTo;
tableView.GetColumnSafe("ShipCountry").ListOfFilterValues = new string[] { "Country 1", "Country 2", "Country 3" };
tableView.GetColumnSafe("ShipCountry").CurrentFilterFunction = GridKnownFunction.EqualTo;
GridFilteringItem filteringItem = tableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
if (filteringItem != null)
{
string columnUniqueName = "ShipName";
string columnFilterFunction = GridKnownFunction.EqualTo.ToString();
Pair checkListFilterArgs = new Pair()
{
First = columnUniqueName,
Second = columnFilterFunction
};
filteringItem.FireCommandEvent(RadGrid.FilterCommandName, checkListFilterArgs);
}
}Combined
The FilterType="Combined" is the combination of the Classic and CheckList filter types. Check out the CheckList and Classic sections for details.
Example: Applying the filters on Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
GridTableView tableView = RadGrid1.MasterTableView;
GridFilteringItem filteringItem = tableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
if (filteringItem != null)
{
// CheckList
GridColumn orderIdColumn = tableView.GetColumnSafe("OrderID");
orderIdColumn.ListOfFilterValues = new string[] { "1", "2", "3" };
orderIdColumn.CurrentFilterFunction = GridKnownFunction.EqualTo;
// Classic
GridColumn shipNameColumn = tableView.GetColumnSafe("ShipName");
shipNameColumn.CurrentFilterFunction = GridKnownFunction.Contains;
TextBox shipNameFilterControl = (TextBox) filteringItem[shipNameColumn.UniqueName].Controls[0];
shipNameFilterControl.Text = "Name 1";
// Filter Args
Pair filterArgs = new Pair()
{
First = shipNameColumn.UniqueName,
Second = shipNameColumn.CurrentFilterFunction.ToString()
};
// Trigger the Filter Command
filteringItem.FireCommandEvent(RadGrid.FilterCommandName, filterArgs);
}
}HeaderContext
When using the FilterType="HeaderContext", the ContextMenu provides two options for filtering:
To filter using the CheckBoxList values, populate the column's ListOfFilterValues property with an array of string.
RadGrid1.MasterTableView.GetColumnSafe("OrderID").ListOfFilterValues = new string[] { "1", "2", "3" };To filter by the second option, which is located below the CheckBoxList (where you can use two different values and filter functions to create two different conditions) you will need to:
- Set the column's
CurrentFilterValueandCurrentFilterFunctionproperties (first condition) - Set the column's
AndCurrentFilterValueandAndCurrentFilterFunctionproperties (second condition)
GridColumn shipNameColumn = RadGrid1.MasterTableView.GetColumnSafe("ShipName");
// First Filter Condition
shipNameColumn.CurrentFilterValue = "Name 1";
shipNameColumn.CurrentFilterFunction = GridKnownFunction.Contains;
// Second Filter Condition
shipNameColumn.AndCurrentFilterValue = "Name 2";
shipNameColumn.AndCurrentFilterFunction = GridKnownFunction.Contains;Once the columns are configured, trigger the Filter command using the FireCommandEvent(commandName, commandArguments) method of the GridHeaderItem object.
The FireCommandEvent() method parameters are:
- The
commandNameparameter is the name of the command to be triggered, in this caseHeaderContextMenuFilter. You can also use the constantRadGrid.HeaderContextMenuFilterCommandNamewhich has the same value. - The
commandArgumentsparameter is aTripletobject where:Firstproperty requires the column's UniqueNameSecondproperty is aPairobject where:Firstproperty requires the string representation of the column'sCurrentFilterFunctionproperty value.Secondproperty requires column'sCurrentFilterValueproperty value.
Thirdproperty is aPairobject where:Firstproperty requires the string representation of the column'sAndCurrentFilterFunctionproperty value.Secondproperty requires column'sAndCurrentFilterValueproperty value.
GridHeaderItem headerItem = RadGrid1.MasterTableView.GetItems(GridItemType.Header).FirstOrDefault() as GridHeaderItem;
if (headerItem != null)
{
GridColumn orderIdColumn = RadGrid1.MasterTableView.GetColumnSafe("OrderID");
Triplet filterArgs = new Triplet()
{
First = orderIdColumn.UniqueName,
Second = new Pair()
{
First = orderIdColumn.CurrentFilterFunction,
Second = orderIdColumn.CurrentFilterValue
},
Third = new Pair()
{
First = orderIdColumn.AndCurrentFilterFunction,
Second = orderIdColumn.AndCurrentFilterValue
}
};
headerItem.FireCommandEvent(RadGrid.HeaderContextMenuFilterCommandName, filterArgs);
}The Filter command can only be triggered for one column at a time, however, this will filter multiple columns if they are all prepared in the same way as described above.
Example: Applying the filters on Button Click
protected void RadButton1_Click(object sender, EventArgs e)
{
GridTableView tableView = RadGrid1.MasterTableView;
// Filter the OrderID by Option 1
tableView.GetColumnSafe("OrderID").ListOfFilterValues = new string[] { "1", "2", "3" };
// Filter ShipName by Option 2
GridColumn shipNameColumn = tableView.GetColumnSafe("ShipName");
// First Filter Condition
shipNameColumn.CurrentFilterValue = "Name 1";
shipNameColumn.CurrentFilterFunction = GridKnownFunction.Contains;
// Second Filter Condition
shipNameColumn.AndCurrentFilterValue = "Name 2";
shipNameColumn.AndCurrentFilterFunction = GridKnownFunction.Contains;
GridHeaderItem headerItem = tableView.GetItems(GridItemType.Header).FirstOrDefault() as GridHeaderItem;
if (headerItem != null)
{
string columnUniqueName = shipNameColumn.UniqueName;
// First Filter Condition
string currentFilterFunction = shipNameColumn.CurrentFilterFunction;
string currentFilterValue = shipNameColumn.CurrentFilterValue;
// Second Filter Condition
string andCurrentFilterFunction = shipNameColumn.AndCurrentFilterFunction;
string andCurrentFilterValue = shipNameColumn.AndCurrentFilterFunction;
Triplet headerContextFilterArgs = new Triplet()
{
First = columnUniqueName,
Second = new Pair()
{
First = currentFilterFunction,
Second = currentFilterValue
},
Third = new Pair()
{
First = andCurrentFilterFunction,
Second = andCurrentFilterValue
}
};
headerItem.FireCommandEvent(RadGrid.HeaderContextMenuFilterCommandName, headerContextFilterArgs);
}
}Apply Default Filter on Initial Load
To apply a default filter at initial load, move the code in the PreRender event so this gets executed during the Page's LifeCycle.
Check out the following sections to learn how to filter the Grid depending on the selected FilterType
Use the
IsPostBackcondition to limit code execution for onetime, when the page loads at the first time, so users can filter the Grid without those being reset to the initial values.
Example: Applying the filters in the Grid's PreRender event
protected void RadGrid_PreRender(object sender, EventArgs e)
{
if (!IsPostBack)
{
RadGrid grid = (RadGrid) sender;
GridTableView tableView = grid.MasterTableView;
tableView.GetColumnSafe("OrderID").ListOfFilterValues = new string[] { "1", "2", "3" };
tableView.GetColumnSafe("OrderID").CurrentFilterFunction = GridKnownFunction.EqualTo;
tableView.GetColumnSafe("ShipName").ListOfFilterValues = new string[] { "Name 1", "Name 2", "Name 3" };
tableView.GetColumnSafe("ShipName").CurrentFilterFunction = GridKnownFunction.EqualTo;
tableView.GetColumnSafe("ShipCountry").ListOfFilterValues = new string[] { "Country 1", "Country 2", "Country 3" };
tableView.GetColumnSafe("ShipCountry").CurrentFilterFunction = GridKnownFunction.EqualTo;
GridFilteringItem filteringItem = tableView.GetItems(GridItemType.FilteringItem).FirstOrDefault() as GridFilteringItem;
if (filteringItem != null)
{
string columnUniqueName = "ShipName";
string columnFilterFunction = GridKnownFunction.EqualTo.ToString();
Pair checkListFilterArgs = new Pair()
{
First = columnUniqueName,
Second = columnFilterFunction
};
filteringItem.FireCommandEvent(RadGrid.FilterCommandName, checkListFilterArgs);
}
}
}