XtraGrid 提供了 自定义筛选对话框,在运行时刻为列指定筛选标准。 最终用户可以通过从 筛选下拉列表 中选择 (Custom) 项,访问此对话框。 本文档展示了当选中(Custom) 项时,如何实现特定的行为。 例如,可以提供自定义筛选,提供另一个筛选对话框或定制现有的对话框。 关于在 XtraGrid 中的筛选的总说明,请参阅 筛选概述 章节。 筛选下拉列表 文档描述了如何添加或删除筛选下拉列表中的列表项。
自定义筛选对话框
如果此属性值被设置为 false (默认值),则当从筛选下拉列表中选择 (Custom) 列表项时,网格显示默认的自定义筛选对话框。 这允许设置比较当前列取值和指定取值的筛选标准。
显示在左侧的组合框编辑器允许选择筛选条件的比较运算符。 可以通过显示在右侧的编辑器输入待比较的取值。
如果 ColumnViewOptionsFilter.UseNewCustomFilterDialog 属性值设置为 true,则使用高级的自定义筛选对话框来代替。 此对话框扩展了默认对话框的功能,添加了比较当前列取值和其他列取值的功能。
如果 Field 复选框被清除,则此对话框的行为与默认对话框的相似。 如果 Field 选项被选中,则显示在右侧的编辑器允许选择要比较的列。
当 (Custom) 筛选项被选择时,要实现特定的行为,则可以接管 ColumnView.CustomFilterDialog 事件。 例如,可以阻止显示或定制标准的自定义筛选对话框,而调用您自己的筛选对话框,或自动为列应用筛选标准。ColumnView.ShowCustomFilterDialog 方法允许人工为特定的列激活自定义筛选对话框。
应用自定义筛选标准
在下面的示例中,当 OrderDate、RequiredDate 或 ShippedDate 列的 (Custom) 项被选中时,“Column = TODAY”筛选被应用于相应的列。 要获得关于通过代码创建筛选条件的信息,请参阅 筛选概述 文档。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid.Views.Grid; private void gridView1_CustomFilterDialog(object sender, CustomFilterDialogEventArgs e) { if(e.Column.FieldName == "OrderDate" || e.Column.FieldName == "RequiredDate" || e.Column.FieldName == "ShippedDate") { //Prevent the standard filter dialog from opening e.Handled = true; //New filter criteria e.FilterInfo = new ColumnFilterInfo(DateTime.Today, "[" + e.Column.Caption + "] = TODAY"); } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Columns Imports DevExpress.XtraGrid.Views.Grid Private Sub GridView1_CustomFilterDialog(ByVal sender As Object, _ ByVal e As CustomFilterDialogEventArgs) Handles GridView1.CustomFilterDialog If e.Column.FieldName = "OrderDate" OrElse e.Column.FieldName = "RequiredDate" _ OrElse e.Column.FieldName = "ShippedDate" Then 'Prevent the standard filter dialog from opening e.Handled = True 'New filter criteria e.FilterInfo = New ColumnFilterInfo(DateTime.Today, "[" + e.Column.Caption + "] = TODAY") End If End Sub |
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Grid; //... private void gridView1_ShowFilterPopupListBox(object sender, FilterPopupListBoxEventArgs e) { if (e.Column.FieldName == "OrderDate" || e.Column.FieldName == "RequiredDate" || e.Column.FieldName == "ShippedDate") { // Locate the (Custom) item. for(int i = 0; i < e.ComboBox.Items.Count; i++) { object item = e.ComboBox.Items[i]; if(item is FilterItem && ((FilterItem)item).Value is FilterItem) { object itemValue2 = ((FilterItem)((FilterItem)item).Value).Value; if(itemValue2 is Int32 && Convert.ToInt32(itemValue2) == 1) { (e.ComboBox.Items[i] as FilterItem).Text = "(TODAY)"; break; } } } } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Grid '... Private Sub GridView1_ShowFilterPopupListBox(ByVal sender As System.Object, _ ByVal e As FilterPopupListBoxEventArgs) Handles GridView1.ShowFilterPopupListBox If e.Column.FieldName = "OrderDate" Or e.Column.FieldName = "RequiredDate" _ Or e.Column.FieldName = "ShippedDate" Then ' Locate the (Custom) item. Dim i As Integer For i = 0 To e.ComboBox.Items.Count - 1 Dim item As Object = e.ComboBox.Items(i) If TypeOf item Is FilterItem Then Dim itemValue = CType(item, FilterItem).Value If TypeOf itemValue Is FilterItem Then Dim itemValue2 = CType(itemValue, FilterItem).Value If TypeOf itemValue2 Is Integer Then If Convert.ToInt32(itemValue2) = 1 Then CType(e.ComboBox.Items(i), FilterItem).Text = "(TODAY)" Exit For End If End If End If End If Next End If End Sub |
定制标准的筛选对话框
按照上述说明,当 Field 选项被勾选时,高级自定义筛选对话框允许比较当前列的取值和另一列的取值。 待比较列的标题被显示在取值的组合框编辑器中。 当通过代码创建高级自定义筛选对话框时,可以提供待比较列的集合。 也可以指定是否仅可视列的标题可用,还是集合中的所有列都可用。
下面的示例展示了当 RequiredDate 和 ShippedDate 列的 (Custom) 项被选中时,如何调用高级自定义筛选对话框。 在对话框的构造函数中,第一个参数引用应设置筛选标准的列,第二个参数指定显示在对话框的取值组合框中的列的集合。 第三个参数指定集合中的应在取值组合框中可用的所有列。在创建此对话框之后,它的 Field 复选框被设置为勾选状态。 在 FieldCheckBox 方法中,通过遍历所有对话框控件,Field 复选框被访问。
在对话框被关闭之后,通过列的 GridColumn.FilterInfo 属性,表示用户选择的新的筛选标准已经被设置。 为了防止重写这些标准,必须把 CustomFilterDialogEventArgs.FilterInfo 属性设置为 null (在 Visual Basic 中为 Nothing)。 否则,此属性值将被指派到列的 GridColumn.FilterInfo 属性。 为了在事件处理程序执行完毕之后,阻止标准的筛选对话框被显示,则必须把 CustomFilterDialogEventArgs.Handled 参数设置为 false。
下面的插图举例说明了为 RequiredDate 列显示的定制的筛选对话框。 注意,取值组合框中不包括调用此对话框的列的标题,即使该列已经被包含在所提供的列集合中。
C# | 复制代码 |
---|---|
using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Columns; using DevExpress.XtraEditors; using DevExpress.XtraGrid.Filter; private void gridView1_CustomFilterDialog(object sender, CustomFilterDialogEventArgs e) { GridView View = sender as GridView; if (e.Column.FieldName == "RequiredDate" || e.Column.FieldName == "ShippedDate") { //Create a collection of columns to display in the dialog's value combo boxes GridColumnCollection cols = new GridColumnCollection(null); cols.AddRange(new GridColumn[] {view.Columns["RequiredDate"], View.Columns["ShippedDate"]}); //Create an advanced Custom Filter Dialog and supply the column collection DevExpress.XtraGrid.Filter.FilterCustomDialog2 dlg = new DevExpress.XtraGrid.Filter.FilterCustomDialog2(e.Column, cols, false); //Set the Field options in the dialog to checked setFieldCheckBox(dlg, true); //Display the dialog dlg.ShowDialog(); //The dialog sets filter criteria for the column itself based on the user's choice //Set e.FilterInfo to null to prevent overriding these criteria //after your event handler is performed e.FilterInfo = null; //Prevent the standard filter dialog from displaying e.Handled = true; } } //Sets the Field check boxes in the advanced filter dialog to the specified state private void setFieldCheckBox(FilterCustomDialog2 dialog, bool value) { foreach(Control ctrl in dialog.Controls) if(ctrl is PanelControl) { foreach(Control childCtrl in ctrl.Controls) //Find the GroupBox containing the Field check boxes if(childCtrl is GroupBox) { foreach(Control elem in childCtrl.Controls) { //Find the Field check boxes if(elem is CheckEdit) { ((CheckEdit)elem).Checked = value; } } return; } } } |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraGrid.Views.Grid Imports DevExpress.XtraGrid.Columns Imports DevExpress.XtraEditors Imports DevExpress.XtraGrid.Filter '... Private Sub GridView1_CustomFilterDialog(ByVal sender As Object, _ ByVal e As CustomFilterDialogEventArgs) _ Handles GridView1.CustomFilterDialog Dim View As GridView = sender If e.Column.FieldName = "RequiredDate" OrElse e.Column.FieldName = "ShippedDate" Then 'Create a collection of columns to display in the dialog's value combo boxes Dim cols As GridColumnCollection = New GridColumnCollection(Nothing) cols.AddRange(New GridColumn() {view.Columns("RequiredDate"), View.Columns("ShippedDate")}) 'Create an advanced Custom Filter Dialog and supply the column collection Dim dlg As DevExpress.XtraGrid.Filter.FilterCustomDialog2 = _ New DevExpress.XtraGrid.Filter.FilterCustomDialog2(e.Column, cols, False) 'Set the Field options in the dialog to checked setFieldCheckBox(dlg, True) 'Display the dialog dlg.ShowDialog() 'The dialog sets filter criteria for the column itself based on user's choice 'Set e.FilterInfo to null to prevent overriding these criteria 'safter your event handler is performed e.FilterInfo = Nothing 'Prevent the standard filter dialog from displaying e.Handled = True End If End Sub ' Sets the Field check boxes in the advanced filter dialog to the specified state Private Sub setFieldCheckBox(ByVal dialog As FilterCustomDialog2, ByVal value As Boolean) For Each ctrl As Control In dialog.Controls If TypeOf ctrl Is PanelControl Then For Each childCtrl As Control In ctrl.Controls ' Find the GroupBox containing the Field check boxes If TypeOf childCtrl Is GroupBox Then For Each elem As Control In childCtrl.Controls ' Find the Field check boxes If TypeOf elem Is CheckEdit Then CType(elem, CheckEdit).Checked = value End If Next Exit Sub End If Next End If Next End Sub |