Welcome to LightSwitch Desktop Edition > OLAP for LightSwitch Overview > Programming OLAP for LightSwitch > Building a Custom User Interface > Filtering the View in Code |
The last step in this sample involves writing code to filter the OLAP view, then connecting that code to the LightSwitch filter buttons. Add the following helper method, SetPriceFilter, which is similar to BuildView except that instead of a string argument, it takes a pair of values representing a numeric range:
C# |
Copy Code
|
---|---|
void SetPriceFilter(double min, double max) { // get olap engine var olap = _olapPanel.OlapEngine; // stop updating until done olap.BeginUpdate(); // make sure unit price field is active in the view var field = olap.Fields["UnitPrice"]; olap.FilterFields.Add(field); // customize the filter var filter = field.Filter; filter.Clear(); filter.Condition1.Operator = C1.Olap.ConditionOperator.GreaterThanOrEqualTo; filter.Condition1.Parameter = min; filter.Condition2.Operator = C1.Olap.ConditionOperator.LessThan; filter.Condition2.Parameter = max; // restore updates olap.EndUpdate(); } |
As before, the code gets a reference to the C1OlapEngine and immediately calls BeginUpdate.
It then gets a reference to the “UnitPrice” field that will be used for filtering the data. The “UnitPrice” field is added to the engine’s FilterFields collection so the filter will be applied to the current view.
This is an important detail. If a field is not included in any of the view collections (RowFields, ColumnFields, ValueFields, FilterFields), then it is not included in the view at all, and its Filter property does not affect the view in any way.
The code proceeds to configure the Filter property of the “UnitPrice” field by setting two conditions that specify the range of values that should be included in the view. The range is defined by the “min” and “max” parameters. Instead of using conditions, you could provide a list of values that should be included. Conditions are usually more convenient when dealing with numeric values, and lists are better for string values and enumerations.
Finally, the code calls EndUpdate. Another helper method, FilterBy, is used to wrap SetPriceFilter and ensure that it is called on the UI thread:
C# |
Copy Code
|
---|---|
void FilterBy(double min, double max) { if (CanBind()) { Dispatchers.Main.BeginInvoke(() => { SetPriceFilter(min, max); }); } } |
Now we can wire up the LightSwitch filter buttons created earlier:
C# |
Copy Code
|
---|---|
partial void FilterByAllPrices_Execute() { FilterBy(double.MinValue, double.MaxValue); } partial void FilterByExpensive_Execute() { FilterBy(50, double.MaxValue); } partial void FilterByInexpensive_Execute() { FilterBy(0, 20); } partial void FilterByModerate_Execute() { FilterBy(20, 50); } |
At this point, the code is sufficient to apply the numeric filters to the view. All that remains is to handle the enabled button states as we did earlier for the view buttons. Add the following lines to the end of the SetPriceFilter helper method:
C# |
Copy Code
|
---|---|
// enable filter buttons this.FindControl("FilterByAllPrices").IsEnabled = (min != double.MinValue); this.FindControl("FilterByExpensive").IsEnabled = (min != 50); this.FindControl("FilterByInexpensive").IsEnabled = (min != 0); this.FindControl("FilterByModerate").IsEnabled = (min != 20); |
Since this is a sample and not a real application, the code assumes that the minimum values are the same as those passed to the FilterBy method in the button Execute handlers. It also assumes that the minimum values are unique for all filter buttons. Lastly, we need to disable the filter button that represents the initial (unfiltered) state. We do this by adding the following line to the end of the Created handler for the screen:
C# |
Copy Code
|
---|---|
// initial filter button state this.FindControl("FilterByAllPrices").IsEnabled = false; |
Now our custom screen is finished, and the user can interact with the buttons to change the views and filter conditions. For example, here is what it looks like when the user selects the Product view and the Expensive filter. Note also that the Total column in the grid has been sorted in descending order, and that the chart is kept in sync with the grid.
You can download the source code for the custom screen pictured here as part of the OlapLightSwitchDemo sample project, available at the following URL:
http://demo.componentone.com/LightSwitch/OLAP/OlapLightSwitchDemo.zip
This project includes the data sources as part of the intrinsic ApplicationDatabase.mdf file, and also demonstrates other programming techniques, such as setting up the initial view for a ComponentOne OLAP Screen in code.