Filter Lookup Editor Data Source

In this lesson, you will learn how to filter the data displayed by a lookup editor. This editor is shown in the Detail Views for reference properties. It contains a list of objects of another related class. In this lesson, the Contact.Position lookup editor will be filtered. For this purpose, a Many-to-Many relationship will be set between the Position class and the Department class. Then, the objects of the Position class in the Detail View of the Contact object will be filtered, displaying only those Positions that are related to a corresponding Department.

Note

Before proceeding, take a moment to review the following lessons:

  • Set a Many-to-Many relationship between the Position and Department classes. For details, refer to the Set a Many-to-Many Relationship (XPO/EF) lesson.

    eXpress Persistent Objects

    [DefaultClassOptions]
    [System.ComponentModel.DefaultProperty("Title")]
     public class Department : BaseObject {
       //...
       [Association("Departments-Positions")]
       public XPCollection<Position> Positions {
          get { return GetCollection<Position>("Positions"); }
       }
    }
    
    [DefaultClassOptions]
    [System.ComponentModel.DefaultProperty("Title")]
    public class Position : BaseObject {
          //...
       [Association("Departments-Positions")]
       public XPCollection<Department> Departments {
          get { return GetCollection<Department>("Departments"); }
       }
    }
    

    Entity Framework

    public class Position {
        public Position() {
            //...
            Departments = new List<Department>(); 
        }
        //...
        public virtual IList<Department> Departments { get; set; }
    }
    
    public class Department {
        public Department() {
            //...
            Positions = new List<Position>();
        }
        //...
        public virtual IList<Position> Positions { get; set; }
    }
    
  • Invoke the Model Editor for the MySolution.Module project. Navigate to the BOModel | MySolution.Module.BusinessObjects node. Expand the Contact child node and select the Position child node. The properties to the right define the Contact.Position property. Set the DataSourceProperty property to "Department.Positions". As a result, the Position lookup editor will display the Department.Positions collection.
  • Set the DataSourcePropertyIsNullMode property to "SelectAll", to display all existing objects in the Contact.Position editor when the Department.Positions property is not specified.

    Tutorial_UIC_Lesson7_1

    Note

    You can perform the task defined above in code. See the Implement Dependent Reference Properties (XPO) topic.

  • The data source for the Position property is changed each time the Department property is changed. So, the Position property value should be set to "null" ("Nothing" in VB) after its data source has changed. To set a new value from the recreated data source, replace the Department property declaration with the following code.

    eXpress Persistent Objects

    [Association("Department-Contacts", typeof(Department)), ImmediatePostData]
    public Department Department {
       get {return department;}
       set {
          SetPropertyValue("Department", ref department, value);
          if(!IsLoading) {
             Position = null;
             if(Manager != null && Manager.Department != value) {
                Manager = null;
             }
          }
       }
    }
    
    Note

    The similar functionality can not be implemented for the Entity Framework because the current version of EF does not allow to check where are the assignment signal comes from.

  • Run the WinForms or ASP.NET application. Specify the Positions property for Department objects. Invoke a Contact Detail View. The dropdown list for the Position lookup editor contains the Positions assigned to the Department object that is specified by the Department editor:

    Tutorial_UIC_Lesson7_2

     

    Tutorial_UIC_Lesson7_2_0

You can see the changes made in this lesson in the Main Demo | MainDemo.Module project. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 18.2\Components\eXpressApp Framework\MainDemo by default. The ASP.NET version is available online at http://demos.devexpress.com/XAF/MainDemo/.

 

Next Lesson: Format a Property Value

See Also