实现DevExpress的GridLookUpEditor控件多项(列)模糊筛选的方法

时间:2020-3-4 作者:WilliamChan

GridLookUpEditor控件自身只支持根据 DisplayMember 绑定的字段来进行模糊筛选,可在使用过程常常还需要使用其他的列来筛选,但控件本身是不支持的。为了实现这一扩展功能,我们需要使用反射功能封装成方法,组合控件的事件一起使用,来实现DevExpress的GridLookUpEditor控件多项(列)模糊筛选的方法。具体代码如下:

#region GridLookUpEdit 模糊查询        
        /// <summary>
        /// GridLookUpEdit模糊查询方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arrFilterField">需要查询的列</param>
        void FilterGridLookUpEdit(object sender, string[] arrFilterField)
        {
            var edit = sender as GridLookUpEdit;
            var gridView = edit.Properties.View as GridView;
            var fi = gridView.GetType().GetField("extraFilter", BindingFlags.NonPublic | BindingFlags.Instance);
            var arrCriteriaOperator = new CriteriaOperator[arrFilterField.Length];
            for (int i = 0; i < arrFilterField.Length; i++)
            {
                arrCriteriaOperator[i] = new BinaryOperator(arrFilterField[i], "%" + edit.AutoSearchText + "%", BinaryOperatorType.Like);
            }
            var filterCondition = new GroupOperator(GroupOperatorType.Or, arrCriteriaOperator).ToString();
            fi.SetValue(gridView, filterCondition);
            var mi = gridView.GetType().GetMethod("ApplyColumnsFilterEx", BindingFlags.NonPublic | BindingFlags.Instance);
            mi.Invoke(gridView, null);
        }

        void GridLookUpEdit_Properties_Popup(object sender, EventArgs e)
        {
            FilterGridLookUpEdit(sender, new string[] { "U_NAME", "U_ID", "U_PYM" });
        }

        void GridLookUpEdit_TextChanged(object sender, EventArgs e)
        {
            GridLookUpEdit edit = sender as GridLookUpEdit;
            edit.SelectionStart = edit.Text.Length;
        }

        void GridLookUpEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            this.BeginInvoke(new MethodInvoker(delegate
            {
                FilterGridLookUpEdit(sender, new string[] { "U_NAME", "U_ID", "U_PYM" });
            }));
        }

        #endregion

使用时,只要相应的绑定方法到GridLookUpEditor控件对应的事件就可以了。

以上就是知道不整理的“实现DevExpress的GridLookUpEditor控件多项(列)模糊筛选的方法”,希望对您有所帮助。

声明:若您对本文内容有任何意见,欢迎发送邮件至:admin@zhidaobu.com,本站会尽快回应并做出相应处理。