DevExpress XAF get selected items in Listview

Date: 2024-04-02

In List View data access modes InstantFeedback, InstantFeedbackView, or ServerView the selected rows/objects are of the type ObjectRecord. In other modes the real type is available. Below a helper function to use one method for all modes.

using DevExpress.ExpressApp;

public static List<T> GetSelectedObjects<T>(IEnumerable selectedObjects, IObjectSpace os)
{
    var selectedItemList = selectedObjects.OfType<ObjectRecord>()
        .Where(x => x.ObjectType == typeof(T))
        .Select(x => os.GetObjectByKey<T>(x.ObjectKeyValue))
        .OfType<T>();
    return selectedItemList.Concat(selectedObjects.OfType<T>()).ToList();
}

private void MyAction_Execute(object sender, SimpleActionExecuteEventArgs e)
{
    var productList = GetSelectedObjects<Product>(ListView.SelectedObjects, ObjectSpace);
    foreach (var product in productList)
    {
        // do something
    }
    ObjectSpace.Refresh();
}
83860cookie-checkDevExpress XAF get selected items in Listview