This example illustrates how to filter a ListView displayed in a DashboardView based on another ListView's selection.
Scenario
When a DashboardView contains several list views, it is often required to make them dependent, e.g. display items of one ListView based on items or selection of another ListView.
Implementation Details
- Add a new ViewController to the YourSolutionName.Module project. For more information, refer to the following file: DashboardFilterController.cs.
- In the
OnActivated
method, retrieveDashboardViewItems
via theFindItem
method, and subscribe to theControlCreated
event of aDashboardViewItem
whoseListView
is used to filter data (hereinafter referred to asSourceView
). - In the
ControlCreated
event handler retrieve theSourceView
via the DashboardViewItem.InnerView property and subscribe to its SelectionChanged event. - In the
SelectionChanged
event handler, retrieve the View to be filtered (hereinafter referred to asTargetView
) in the same way as the previous step. - To get an object that is used for filtering, use the ListView.CurrentObject property.
- You can now add
CriteriaOperator
to the TargetView.CollectionSource.Criteria dictionary to filter theTargetView
. In this example, we created anInOperator
that usesSourceView
objects to filter the AssignedTo column.
Files to Review
Documentation
ASP.NET WebForms Version
You can find the ASP.NET WebForms version of this example in the following branch: DevExpress-Examples/xaf-how-to-implement-dependent-views-in-a-dashboardview-filter-based-on-selection at 17.2.3+
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
C#using System;
using System.Linq;
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
using System.Collections.Generic;
using DevExpress.ExpressApp.Editors;
using DevExpress.ExpressApp.SystemModule;
using DXApplication4.Module.BusinessObjects;
namespace Solution3.Module.Controllers {
// For more typical usage scenarios, be sure to check out http://documentation.devexpress.com/#Xaf/clsDevExpressExpressAppViewControllertopic.
public class DashboardFilterController : ViewController<DashboardView> {
private const string DashboardViewId = "MyDashboardView";
private DashboardViewItem SourceItem;
private DashboardViewItem TargetItem;
private const string CriteriaName = "Test";
private void FilterDetailListView(ListView masterListView, ListView detailListView) {
detailListView.CollectionSource.Criteria.Clear();
List<object> searchedObjects = new List<object>();
foreach (object obj in masterListView.SelectedObjects) {
searchedObjects.Add(detailListView.ObjectSpace.GetKeyValue(obj));
}
if (searchedObjects.Count > 0) {
detailListView.CollectionSource.Criteria[CriteriaName] = CriteriaOperator.FromLambda<MyTask>(x => searchedObjects.Contains(x.AssignedTo.ID));
}
}
private void SourceItem_ControlCreated(object sender, EventArgs e) {
DashboardViewItem dashboardItem = (DashboardViewItem)sender;
ListView innerListView = dashboardItem.InnerView as ListView;
if (innerListView != null) {
innerListView.SelectionChanged -= innerListView_SelectionChanged;
innerListView.SelectionChanged += innerListView_SelectionChanged;
}
}
private void innerListView_SelectionChanged(object sender, EventArgs e) {
FilterDetailListView((ListView)SourceItem.InnerView, (ListView)TargetItem.InnerView);
}
private void DisableNavigationActions(Frame frame) {
RecordsNavigationController recordsNavigationController = frame.GetController<RecordsNavigationController>();
if (recordsNavigationController != null) {
recordsNavigationController.Active.SetItemValue("DashboardFiltering", false);
}
}
protected override void OnActivated() {
base.OnActivated();
if (View.Id == DashboardViewId) {
SourceItem = (DashboardViewItem)View.FindItem(FilterSourceID);
TargetItem = (DashboardViewItem)View.FindItem(FilterTargetId);
if (SourceItem != null) {
SourceItem.ControlCreated += SourceItem_ControlCreated;
}
if (TargetItem != null) {
if (TargetItem.Frame != null) {
DisableNavigationActions(TargetItem.Frame);
} else {
TargetItem.ControlCreated += (s, e) => {
DisableNavigationActions(TargetItem.Frame);
};
}
}
}
}
protected override void OnDeactivated() {
if (SourceItem != null) {
SourceItem.ControlCreated -= SourceItem_ControlCreated;
SourceItem = null;
}
TargetItem = null;
base.OnDeactivated();
}
public DashboardFilterController() {
FilterSourceID = "ContactView";
FilterTargetId = "MyTaskView";
}
public string FilterSourceID { get; set; }
public string FilterTargetId { get; set; }
}
}