Skip to main content
A newer version of this page is available. .

How to: Filter a Link Dialog's List View

  • 6 minutes to read

This topic details how to filter List Views in the Link Action’s pop-up Window. Sometimes, you may need to filter the List View based on specific criteria. Oftentimes, you may need to make the List View data source dependent on property editor values of the source Detail View. The eXpressApp Framework provides the DataSourcePropertyAttribute and DataSourceCriteriaAttribute for this purpose. These attributes can be applied both in code and in the Application Model, via the Model Editor. This topic demonstrates how to apply the DataSourceProperty and DataSourceCriteria attributes in code, using a special data source generated on the fly.

Note

Tip

A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e235/how-to-filter-a-link-dialog-s-list-view.

Initial Implementation

Initially, a Contact includes a Position and Task collection. Contact and Task objects are related by the Many-to-Many relationship. Contact and Position objects are related by the One-to-Many relationship. The following code shows how these classes can be implemented:

using System.ComponentModel;
using DevExpress.Data.Filtering;
//...
[DefaultClassOptions]
public class Contact : Person {
   public Contact(Session session) : base(session) {}
   private Position position;
   public Position Position {
      get {
         return position;
      }
      set {
         SetPropertyValue("Position", ref position, value);
      }
   }
   [Association("Contact-DemoTask")]
   public XPCollection<DemoTask> Tasks {
      get {
         return GetCollection<DemoTask>("Tasks");
      }
   }
}
[DefaultClassOptions]
[System.ComponentModel.DefaultProperty("Title")]
public class Position : BaseObject {
   public Position(Session session) : base(session) {}
   private string title;
   public string Title {
      get {
         return title;
      }
      set {
         SetPropertyValue("Title", ref title, value);
      }
   }
}
[DefaultClassOptions]
public class DemoTask : Task {
   private Priority priority;
   public DemoTask(Session session): base(session) {}
   public Priority Priority {
      get {
         return priority;
      }
      set {
         SetPropertyValue("Priority", ref priority, value);
      }
   }
   [Association("Contact-DemoTask")]
   public XPCollection<Contact> Contacts {
      get {
         return GetCollection<Contact>("Contacts");
      }
   }
   //...
}
public enum Priority {
   Low = 0,
   Normal = 1,
   High = 2
}

The image below demonstrates the Contact Detail View in a Windows Forms application:

HowToFilterLinkDialogListView

Here, the Link Action’s pop-up Window displays a List View with all existing Task objects. However, certain scenarios may require a filtered collection to be displayed by this List View.

Assume that high priority tasks can only be assigned to managers. The List View in the Link Action’s pop-up window should be filtered according to the criteria [Priority] = “High” to implement this. In this instance, the DataSourceCriteriaAttribute can be applied to the Contact.Tasks property. However, we decided that this List View should be filtered only when the current Contact object’s Position is manager. Thus, we need to generate a filtered collection of Task objects only when the Title property of the current Contact’s Position is set to “Manager”. In all other instances, the List View will contain all existing Task objects. For this purpose, the DataSourcePropertyAttribute is very helpful. We will apply it to the Contact.Tasks property and pass our new collection as the data source. The following code demonstrates how to do this:

using System.ComponentModel;
using DevExpress.Data.Filtering;
//...
[DefaultClassOptions]
public class Contact : Person {
   //...
   [Association("Contact-DemoTask")]
   // Set the AvailableTasks collection as the data source for the Tasks property
   [DataSourceProperty("AvailableTasks")]
   public XPCollection<DemoTask> Tasks {
      get {
         return GetCollection<DemoTask>("Tasks");
      }
   }
   private XPCollection<DemoTask> availableTasks;
   [Browsable(false)] // Prohibits showing the AvailableTasks collection separately
   public XPCollection<DemoTask> AvailableTasks {
      get {
         if(availableTasks == null) {
            // Retrieve all Task objects
            availableTasks = new XPCollection<DemoTask>(Session);
         }
      // Filter the retrieved collection according to the current conditions
      RefreshAvailableTasks();
      // Return the filtered collection of Task objects
      return availableTasks;
      }
   }
   private void RefreshAvailableTasks() {
      if(availableTasks == null)
         return;
      if((Position != null) && (Position.Title == "Manager")) {
         //Filter the collection
         availableTasks.Criteria = CriteriaOperator.Parse("[Priority] = 'High'");
      }
      else {
         //Remove the applied filter
         availableTasks.Criteria = null;
      }
   }
   private Position position;
   public Position Position {
      get {
         return position;
      }
      set {
         SetPropertyValue("Position", ref position, value);
         // Refresh the Tasks property data source
         RefreshAvailableTasks();
      }
   }
}

The following image shows a Contact Detail View and the Link Action’s filtered List View:

HowToFilterLinkDialogListView_Result

See Also