How to: Filter Large List Views using the Auto Filter Row 如何:使用自动筛选行筛选大型列表视图

This topic demonstrates how to avoid displaying the entire collection of a List View with a large object count in Windows Forms applications. One of the ways to do this is to use the Auto Filter Row of the XtraGrid used in Windows Forms applications.

本主题演示如何避免在 Windows 窗体应用程序中显示具有大量对象计数的列表视图的整个集合。执行此操作的方法之一是使用 Windows 窗体应用程序中使用的 XtraGrid 的自动筛选器行。

ActiveFilterRow

To activate the Auto Filter Row in an XAF application, use the IModelListViewShowAutoFilterRow.ShowAutoFilterRow property of the Application Model's Views | <ListView> node. When this property is set to true, the Auto Filter Row is available.

要激活 XAF 应用程序中的自动筛选行,请使用 IModelListViewShow 自动筛选行.显示应用程序模型视图的自动筛选行属性 |<ListView>节点。当此属性设置为 true 时,自动筛选器行可用。

By default, all existing objects are retrieved while the current filter is empty. To prohibit the retrieval of objects, implement a new View Controller as shown below.

默认情况下,在当前筛选器为空时检索所有现有对象。要禁止检索对象,实现一个新的视图控制器,如下所示。

Assume your application uses the Task business class from the Business Class Library. Imagine that the Task objects collection is very large, so you will need an empty List View to be displayed when you click the Task item in the navigation control. However, to get the required objects, the Auto Filter Row is made available via the Model Editor.

假设您的应用程序使用来自 Business 类库的任务业务类。假设 Task 对象集合非常大,因此当您单击导航控件中的"任务"项时,需要显示一个空列表视图。但是,要获取所需的对象,自动筛选行可通过模型编辑器提供。

  • Create a new View Controller. (In this example, it is called AutoFilterRowController.) To learn how to implement Controllers, refer to the Tutorial's Extend Functionality section.
  • Set the Controller's scope so that it is only activated for a root Task List View. Set the ViewController.TargetViewType property to ListView, the ViewController.TargetViewNesting to Root, and the ViewController.TargetObjectType to DevExpress.Persistent.BaseImpl.Task to do this.
  • To prohibit the retrieval of Task objects when the Task List View is displayed and the grid's filter has not yet been applied, set a false criteria for the View's Collection Source. To do this, access the View's Grid control by handling the Controller's Controller.Activated event to subscribe to the View's View.ControlsCreated event.

  • 创建新的视图控制器。(在此示例中,它被称为自动筛选行控制器。要了解如何实现控制器,请参阅教程的扩展功能部分。

  • 设置控制器的范围,以便仅为根任务列表视图激活控制器。将视图控制器.TargetViewType 属性设置为列表视图、视图控制器.TargetView 到根目录,以及视图控制器.目标对象类型到 DevExpress.持久.BaseImpl.taskImpl.任务以执行此操作。

  • 要禁止在显示任务列表视图且尚未应用网格筛选器时检索 Task 对象,为视图的集合源设置错误条件。为此,通过处理控制器控制器访问视图的网格控件。
public partial class AutoFilterRowController : ViewController { 
   //...
   private ListView view;

   private void AutoFilterRowController_Activated(object sender, EventArgs e) {
      view = (ListView)View;
      view.ControlsCreated += new EventHandler(AutoFilterRowController_ControlsCreated);
   }
}
  • In the ControlsCreated event handler, access the View's Grid control and set a false criteria if the active filter of its GridView is empty:

  • 在"控制创建"事件处理程序中,访问视图的网格控件,如果其 GridView 的活动筛选器为空,则设置错误条件:

    using DevExpress.XtraGrid.Views.Base;
    using DevExpress.Data.Filtering;
    using DevExpress.XtraGrid;
    //...
    public partial class AutoFilterRowController : ViewController {
       //...
       private ColumnView gridView;
       private CriteriaOperator falseCriteria = CriteriaOperator.Parse("1=0");
    
       void AutoFilterRowController_ControlsCreated(object sender, EventArgs e) {
          GridControl grid = view.Control as GridControl;
          gridView = grid.FocusedView as ColumnView;
          if(gridView.ActiveFilter.IsEmpty) {
             view.CollectionSource.Criteria["FalseCriteria"] = falseCriteria;
          }
          gridView.ActiveFilter.Changed += new EventHandler(ActiveFilter_Changed);
       }
    }
  • As you can see in the code above, we subscribe to the active filter's Changed event. This is necessary in order to retrieve all Task objects when you need to get those that satisfy the filters generated by an end-user via the Auto Filter Row. In addition, apply the false criteria when an end-user cancels the active filter. In this case, the event handler is:

  • 如您在上面的代码中看到的,我们订阅活动筛选器的"更改"事件。当您需要获取满足最终用户通过自动筛选器行生成的筛选器的对象时,这是检索所有 Task 对象所必需的。此外,当最终用户取消活动筛选器时,应用 false 标准。在这种情况下,事件处理程序为:

    public partial class AutoFilterRowController : ViewController {
       //...
       void ActiveFilter_Changed(object sender, EventArgs e) {
          if(((ViewFilter)sender).IsEmpty) {
            view.CollectionSource.Criteria["FalseCriteria"] = falseCriteria;
          }
          else {
             if(view.CollectionSource.Criteria.ContainsKey("FalseCriteria")) {
                view.CollectionSource.Criteria.Remove("FalseCriteria");
             }
          }
       }
    }
  • Run the application and make sure that everything works as required.
  • 运行应用程序并确保一切工作正常。
原文地址:https://www.cnblogs.com/foreachlife/p/How-to-Filter-Large-List-Views-using-the-Auto-Filter-Row.html