示例#1
0
        public T Find <T>(Filter <T> filter) where T : class
        {
            if (_currentCodon == null)
            {
                return(null);
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null || bindingList.Count == 0)
            {
                return(null);
            }

            Type tType = typeof(T);

            if (_currentCodon.UpwardCompatible(tType) == false &&
                _currentCodon.Compatible(tType) == false)
            {
                return(null);
            }

            foreach (object obj in bindingList)
            {
                if (Compatible(tType, obj.GetType()) && filter((T)obj))
                {
                    return(obj as T);
                }
            }

            return(null);
        }
示例#2
0
        /// <summary>
        /// 中指定对象(所在的行)
        /// exclusive 用于指定是否是排它性选择,既取消现有选择
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="exclusive"></param>
        public void Select(object obj, bool exclusive)
        {
            Debug.Assert(obj != null, "obj 为 null");

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null || bindingList.Count == 0)
            {
                return;
            }

            if (exclusive)
            {
                ClearSelectedRows();
            }

            if (obj == null)
            {
                return;
            }

            int index = bindingList.IndexOf(obj);

            if (index < 0)
            {
                return;
            }

            _dataGridView.Rows[index].Selected = true;
        }
示例#3
0
        public void Remove(object obj)
        {
            Debug.Assert(obj != null, "obj为null");

            if (obj == null)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null)
            {
                return;
            }

            Debug.Assert(bindingList.Contains(obj), "_bindingList中没有指定对象");

            if (bindingList.Contains(obj) == false)
            {
                return;
            }

            if (_currentCodon.Compatible(obj))
            {
                bindingList.Remove(obj);
            }
        }
示例#4
0
        /// <summary>
        /// 清除列,数据源
        /// </summary>
        public void ClearData()
        {
            _contextData             = null;
            _dataGridView.DataSource = null;

            if (_currentCodon != null)
            {
                IBindingListEx bindingList = GetBindingList(_currentCodon);
                bindingList = null;
            }
        }
示例#5
0
        //MoveBefore和MoveAfter方法共用,用于获取要移动的对象
        //该方法的作用实际上就是用两个过虑器委托分别取出两个对应的对象
        private void GetMoveObject <T>(Filter <T> sourceFilter, Filter <T> targetFilter,
                                       out object sourceObj, out object targetObj) where T : class
        {
            //如果不加 where T : class,不能用 obj as T

            sourceObj = null;
            targetObj = null;

            Debug.Assert(sourceFilter != null && targetFilter != null, "Filter 为 null");

            if (sourceFilter == null || targetFilter == null)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null || bindingList.Count == 0)
            {
                return;
            }

            Type tType = typeof(T);

            if (_currentCodon.UpwardCompatible(tType) == false &&
                _currentCodon.Compatible(tType) == false)
            {
                return;
            }

            foreach (object obj in bindingList)
            {
                if (sourceObj != null && targetObj != null)
                {
                    break;
                }

                if (sourceObj == null && sourceFilter(obj as T))
                {
                    sourceObj = obj;
                }

                if (targetObj == null && targetFilter(obj as T))
                {
                    targetObj = obj;
                }
            }
        }
示例#6
0
        /// <summary>
        /// 将指定的 bindingList 分配给指定的 codon
        /// 如果之前没有为指定的 codon 建立专用的 BindingList,则在 _bindingListPool 建立之
        /// </summary>
        /// <param name="codon"></param>
        /// <param name="bindingList"></param>
        private void AllotBindingList(ITypeBinderDataGridViewTypeCodon codon, IBindingListEx bindingList)
        {
            Debug.Assert(codon != null && bindingList != null, "codon 或 bindingList 为 null");

            if (_bindingListPool.Keys.Contains(codon))
            {
                _bindingListPool[codon] = bindingList;
            }
            else
            {
                _bindingListPool.Add(codon, bindingList);
            }
        }
示例#7
0
        public void Remove <T>(Filter <T> filter) where T : class
        {
            Debug.Assert(filter != null, "Filter 为 null");

            if (filter == null)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null || bindingList.Count == 0)
            {
                return;
            }

            Type tType = typeof(T);

            if (_currentCodon.UpwardCompatible(tType) == false &&
                _currentCodon.Compatible(tType) == false)
            {
                return;
            }

            List <object> willbeDelete = new List <object>();

            for (int i = 0; i < bindingList.Count; i++)
            {
                object obj = bindingList[i];

                //在使用复合 codon 时,BindingList 中会存在多种不同类型的对象
                //所以必须先判断对象是否是泛型参数类型的向下兼容类型
                if (Compatible(tType, obj.GetType()) && filter((T)obj))
                {
                    willbeDelete.Add(obj);
                }
            }

            foreach (var item in willbeDelete)
            {
                bindingList.Remove(item);
            }
        }
示例#8
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="filter"></param>
        /// <param name="exclusive">是否清除之前选定项的选定状态</param>
        public void Select <T>(Filter <T> filter, bool exclusive) where T : class
        {
            Debug.Assert(filter != null, "Filter 为 null");

            if (filter == null)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null || bindingList.Count == 0)
            {
                return;
            }

            Type tType = typeof(T);

            if (_currentCodon.UpwardCompatible(tType) == false &&
                _currentCodon.Compatible(tType) == false)
            {
                return;
            }

            if (exclusive)
            {
                ClearSelectedRows();
            }

            for (int i = 0; i < bindingList.Count; i++)
            {
                object obj = bindingList[i];
                if (obj == null)
                {
                    continue;
                }

                if (Compatible(tType, obj.GetType()) && filter((T)obj))
                {
                    _dataGridView.Rows[i].Selected = true;
                }
            }
        }
示例#9
0
        //这里为什么不用泛型方法
        //因为类型可能是由外部变量在运行时决定的,如 Show<e.Type>() ,而实际上这样的写法是不行的
        //泛型参数必须在编译之前静态指定
        /// <summary>
        /// 如果 contextData 为 null,不会把当前上下文对象置为null,而是保持当前上下文对象的值
        /// </summary>
        /// <param name="list"></param>
        /// <param name="type"></param>
        /// <param name="contextData"></param>
        public void DataBind(IList list, Type type, object contextData)
        {
            //这个地方是没有办法通过 list 取到对象类型的
            //集合内可能没有数据,集合本身也无法说明内部数据类型
            _currentCodon = GetCodon(type);

            if (_currentCodon == null)
            {
                Clear();
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            LoadColumns(_currentCodon.Columns);
            this.ContextMenuStrip = _currentCodon.ContextMenuStrip;

            _dataGridView.DataSource = null;

            //因为BindingList需要通过内部元素做类型推导,以便绑定数据呈现数据,
            //只是通过泛型参数object,是不可能获取指定Property的值的
            //所以如果list中没有任何元素,就要把对BindingList的初始化和绑定,推迟到添加元素时(否则单元格无法呈现数据)
            //这种情况主要是针对使用默认 BindingList 的,即 BindingList<object>
            if (list != null && list.Count > 0)
            {
                //_bindingList = new BindingListEx<object>(list.Cast<object>().ToList());
                bindingList = _currentCodon.InitializeBindingList(list);
                if (IsSpecialBindingList(_currentCodon))
                {
                    AllotBindingList(_currentCodon, bindingList);
                }

                _dataGridView.DataSource = bindingList;
            }
            else
            {
                bindingList = null;
                _dataGridView.DataSource = null;
            }

            if (contextData != null)
            {
                _contextData = contextData;
            }
        }
示例#10
0
        public void Add(object obj)
        {
            Debug.Assert(obj != null, "obj为null");

            if (obj == null)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList != null && bindingList.Contains(obj))
            {
                Debug.Assert(false, "_bindingList中已经存在了指定对象");
                return;
            }

            if (_currentCodon.Compatible(obj))
            {
                //如果调用了Clear之后没有Show新的数据,那么_bindingList是为null的
                //如果调用DataBind时list中没有元素,也不会初始化_bindingList,因为BindingList需要用内部元素作类型推导
                if (bindingList == null)
                {
                    bindingList = _currentCodon.InitializeBindingList();
                    if (IsSpecialBindingList(_currentCodon))
                    {
                        AllotBindingList(_currentCodon, bindingList);
                    }
                }

                bindingList.Add(obj);

                if (_dataGridView.DataSource == null)
                {
                    _dataGridView.DataSource = bindingList;
                }
            }
        }
示例#11
0
        /// <summary>
        /// 把目标对象移动到另一个对象之后
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceFilter"></param>
        /// <param name="targetFilter"></param>
        public void MoveAfter <T>(Filter <T> sourceFilter, Filter <T> targetFilter) where T : class
        {
            object sourceObj, targetObj;

            GetMoveObject <T>(sourceFilter, targetFilter, out sourceObj, out targetObj);

            //Debug.Assert(sourceObj != null && targetObj != null, "指定的对象为null");

            if (sourceObj == null && targetObj == null)
            {
                return;
            }

            Debug.Assert(sourceObj != targetObj, "指定的两个对象是同一个对象");

            if (sourceObj == targetObj)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            int sourceIndex = bindingList.IndexOf(sourceObj);
            int targetIndex = bindingList.IndexOf(targetObj);

            //如果targetObj刚好在beforeObj之后,就无需移动了
            if (sourceIndex - targetIndex == 1)
            {
                return;
            }

            bindingList.Remove(sourceObj);
            //要重新 IndexOf(beforeObj) ,因为targetObj被remove之后,beforeObj的下标就可能被改变了
            bindingList.Insert(bindingList.IndexOf(targetObj) + 1, sourceObj);
        }
示例#12
0
        public void Update <T>(Filter <T> filter) where T : class
        {
            Debug.Assert(filter != null, "Filter 为 null");

            if (filter == null)
            {
                return;
            }

            if (_currentCodon == null)
            {
                return;
            }

            IBindingListEx bindingList = GetBindingList(_currentCodon);

            if (bindingList == null || bindingList.Count == 0)
            {
                return;
            }

            Type tType = typeof(T);

            if (_currentCodon.UpwardCompatible(tType) == false &&
                _currentCodon.Compatible(tType) == false)
            {
                return;
            }

            for (int i = 0; i < bindingList.Count; i++)
            {
                object obj = bindingList[i];

                if (Compatible(tType, obj.GetType()) && filter((T)obj))
                {
                    bindingList.ResetItem(i);
                }
            }
        }
示例#13
0
        public void AddCodon(ITypeBinderDataGridViewTypeCodon codon)
        {
            if (codon == null)
            {
                Debug.Assert(false, "codon 为 null");
                throw new ArgumentNullException();
            }

            if (_typeBinderDataGridViewTypeCodons.Contains(codon))
            {
                Debug.Assert(false, "_typeBinderDataGridViewTypeCodons 重复添加:" + codon.ToString());
                throw new ArgumentException();
            }

            Debug.Assert(GetCodon(codon.DataBoundType) == null,
                         "_typeBinderDataGridViewTypeCodons 重复添加类型:" + codon.ToString());

            _typeBinderDataGridViewTypeCodons.Add(codon);

            IBindingListEx bindingList = null;

            _bindingListPool.Add(codon, bindingList);
        }