• 自定义自动搜索控件


    自定义控件代码

        /// <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
    
        }
    View Code
        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; }
        }
    View Code

    出处: http://www.cnblogs.com/windy2008

  • 相关阅读:
    EF6.0新特性-DbCommandInterceptor实现非SQL端读写分离
    【转】VS2012 中文版转英文版 英文版转中文版 界面语言切换
    【转】Repository 返回 IQueryable?还是 IEnumerable?
    迟来的零碎笔记
    mysql 列转行,合并字段的方法
    MySQL 5.7.9版本sql_mode=only_full_group_by问题
    mysql实现full outer join
    ubuntu下如何查找某个文件的路径
    mysql之group_concat函数详解
    MySQL 表别名(Alias)
  • 原文地址:https://www.cnblogs.com/windy2008/p/4813997.html
Copyright © 2020-2023  润新知