自定义自动搜索控件
自定义控件代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/// <summary> /// Assemble Lookupedit and gridview. /// </summary> public partial class AutoLookupControl : DevExpress.XtraEditors.XtraUserControl { #region Properties private object _initData; private object _normData; private string _dispField; private string _valueField; private DictionaryEntry _autoText; private List<GridColInfo> _colInfos; public object InitData { set { _initData = value; } } public object NormData { set { _normData = value; } } public string DispField { set { _dispField = value; } } public string ValueField { set { _valueField = value; } } public List<GridColInfo> ColInfos { set { _colInfos = value; } } public DictionaryEntry AutoText { get { return _autoText; } set { _autoText = value; } } //Constructor public AutoLookupControl() { InitializeComponent(); EnableLookupAttrs(); } #endregion #region Methods public void LoadControl() { if (_initData != null) gridControl1.DataSource = _initData; if (_normData != null) { lookupEditor.Properties.DataSource = _normData; lookupEditor.Properties.DisplayMember = _dispField; lookupEditor.Properties.ValueMember = _valueField; } foreach (var ci in _colInfos) { lookupEditor.Properties.View.AddViewColumn(ci.ColField, ci.ColCaption, ci.ColVisible, ci.ColWidth); gridView1.AddViewColumn(ci.ColField, ci.ColCaption, ci.ColVisible, ci.ColWidth); } } private void EnableLookupAttrs() { gridView1.OptionsView.ShowGroupPanel = false; gridView1.OptionsView.ShowIndicator = false; gridView1.OptionsBehavior.Editable = false; gridView1.OptionsView.ColumnAutoWidth = true; gridView1.OptionsBehavior.AutoPopulateColumns = false; lookupEditor.Properties.View.BestFitColumns(); lookupEditor.Properties.View.OptionsView.ShowAutoFilterRow = true; lookupEditor.Properties.View.OptionsBehavior.AutoPopulateColumns = false; lookupEditor.Properties.NullText = ""; lookupEditor.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True; lookupEditor.Properties.ShowFooter = false; lookupEditor.Properties.AutoComplete = false; lookupEditor.Properties.ImmediatePopup = true; lookupEditor.Properties.PopupFilterMode = PopupFilterMode.Contains; lookupEditor.Properties.TextEditStyle = TextEditStyles.Standard; lookupEditor.Properties.PopupFormSize = new System.Drawing.Size(530, 300); } #endregion #region Events private void lookupEditor_Click(object sender, EventArgs e) { if (lookupEditor.EditValue == null || lookupEditor.EditValue.ToString() == "") { gridControl1.Visible = true; } else { gridControl1.Visible = false; lookupEditor.ShowPopup(); } } private void lookupEditor_EditValueChanged(object sender, EventArgs e) { _autoText.Key = lookupEditor.EditValue; _autoText.Value = lookupEditor.Text; } private void lookupEditor_EditValueChanging(object sender, ChangingEventArgs e) { this.BeginInvoke(new System.Windows.Forms.MethodInvoker(delegate { XLookupHelper.FilterLookup(sender); })); } private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e) { object obj = gridView1.GetRow(gridView1.FocusedRowHandle); object value = obj.GetType().GetProperty(_valueField).GetValue(obj, null); lookupEditor.EditValue = value; gridControl1.Visible = false; } #endregion }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public static class XLookupHelper { /// <summary> /// Multiple columns filter for LookupEdit /// </summary> /// <param name="sender">GridLookUpEdit</param> public static void FilterLookup(object sender) { GridLookUpEdit edit = sender as GridLookUpEdit; GridView gridView = edit.Properties.View as GridView; string srchText = edit.AutoSearchText; GridColumnCollection cols = gridView.Columns; List<CriteriaOperator> ops = new List<CriteriaOperator>(); for (int i = 0; i < cols.Count; i++) { BinaryOperator op = new BinaryOperator( cols[i].FieldName, srchText + "%", BinaryOperatorType.Like ); ops.Add(op); } string filterCondition = new GroupOperator(GroupOperatorType.Or, ops).ToString(); System.Reflection.FieldInfo fi = gridView.GetType().GetField("extraFilter", BindingFlags.NonPublic | BindingFlags.Instance); fi.SetValue(gridView, filterCondition); MethodInfo mi = gridView.GetType().GetMethod("ApplyColumnsFilterEx", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(gridView, null); } /// <summary> /// Init Multiple columns filter for LookupEdit /// </summary> /// <param name="sender">GridLookUpEdit</param> public static void InitFilterCode(object sender) { GridLookUpEdit edit = sender as GridLookUpEdit; GridView gridView = edit.Properties.View as GridView; string constCondition = "[DADA_ID] in ('M54.5','S33','M51','M54')"; System.Reflection.FieldInfo fi = gridView.GetType().GetField("extraFilter", BindingFlags.NonPublic | BindingFlags.Instance); fi.SetValue(gridView, constCondition); MethodInfo mi = gridView.GetType().GetMethod("ApplyColumnsFilterEx", BindingFlags.NonPublic | BindingFlags.Instance); mi.Invoke(gridView, null); } /// <summary> /// External method for adding view column /// </summary> public static int AddViewColumn(this GridLookUpEdit edit, string field, string caption, bool visible, int? width) { GridColumn col = new GridColumn() { FieldName = field, Caption = caption, Visible = visible }; if (width != null) col.Width = width ?? 0; return edit.Properties.View.Columns.Add(col); } public static int AddViewColumn(this GridView view, string field, string caption, int visIndex, int? width) { GridColumn col = new GridColumn() { FieldName = field, Caption = caption, VisibleIndex = visIndex }; if (width != null) col.Width = width ?? 0; return view.Columns.Add(col); } /// <summary> /// Enable GridLookUpEdit some attibutors /// </summary> /// <param name="lookupEdit"></param> public static void EnableLookupEditAttrs(this GridLookUpEdit lookupEdit) { lookupEdit.Properties.NullText = ""; lookupEdit.Properties.AllowNullInput = DevExpress.Utils.DefaultBoolean.True; lookupEdit.Properties.View.BestFitColumns(); lookupEdit.Properties.ShowFooter = true; //lookupEdit.Properties.View.OptionsView.ShowAutoFilterRow = true; lookupEdit.Properties.AutoComplete = false; lookupEdit.Properties.ImmediatePopup = true; lookupEdit.Properties.PopupFilterMode = PopupFilterMode.Contains; lookupEdit.Properties.TextEditStyle = TextEditStyles.Standard; lookupEdit.Properties.PopupFormSize = new System.Drawing.Size(530, 300); lookupEdit.Properties.EditValueChangedFiringMode = DevExpress.XtraEditors.Controls.EditValueChangedFiringMode.Buffered; lookupEdit.Properties.View.OptionsView.ShowColumnHeaders = false; lookupEdit.Properties.View.OptionsView.ShowIndicator = false; } } //Grid column info public class GridColInfo { public GridColInfo(string field, string caption, int visIndex, int? width) { ColField = field; ColCaption = caption; ColVisible = visIndex; ColWidth = width; } public string ColField { get; set; } public string ColCaption { get; set; } public int ColVisible { get; set; } public int? ColWidth { get; set; } }