Soru Filtering Binding List

  • 18 Nisan 2012 Çarşamba 13:40
     
     

    Merhaba Arkadaşlar. Projem client-server ilişkili bir program... Birçok client aynı veritabanı üzerine bağlanarak işlem yapıyorlar.

    Programın ilk açılışı list ekranlarını static bir değişkene atıyorum. Daha sonra herhangi bir değişiklikte(silme update insert) o hareketi notify ederek diğer client'ların cache'lerini update ediyorum.

    Burada şöyle bir sıkıntım var. Sınıflarım filtering Binding list ile oluşturuldu.

    Ben bir hareketi bir client'tan notify ettiğimde, diğer client'in açık list ekranında saçma sapan hareketler oluyor. Konumlanma sorunu yaşıyoruz.

    Bu konu hakkında genel olarak yardımcı olabilirseniz sevinirim.


    Gökhan TIKNAZOĞLU

Tüm Yanıtlar

  • 20 Nisan 2012 Cuma 09:43
    Moderatör
     
     

    Gökhan Bey selamlar,

    Bahsettiğiniz işlemleri nasıl yapıyorsunuz örnek kod paylaşma imkanınız var mı?

    İyi çalışmalar.


    Microsoft bu servisi kullanıcılarına yardım etme, Microsoft ürünleri ve teknolojileriyle ilgili bilgi bankasını genişletme amacıyla ücretsiz sunmaktadır.
    Bu içerik olduğu gibi benim tarafımdan hazırlanmış olup Microsoft tarafından herhangi bir sorumluluk üstlenildiği anlamına gelmez.
    Facebook Üzerinden Takip Et!
    Twitter'da Takip Et!

  • 20 Nisan 2012 Cuma 10:46
     
      Kod İçerir

    tabili... 

    Hemen paylaşayım.

    ListChange olduğunda çalışan eventim...

      public static void OnListChanged(ListChangedEventArgs e, GridView gv, EntityName entityName)
            {
                try
                {
                    if (e.ListChangedType == ListChangedType.ItemAdded ||
                        e.ListChangedType == ListChangedType.ItemChanged ||
                        e.ListChangedType == ListChangedType.ItemDeleted ||
                        e.ListChangedType == ListChangedType.ItemMoved)
                    {
                        int focusedRowHandle = gv.FocusedRowHandle;
                        //Refreshler kalktığında konumlanmalar sapıtıyor...
                        gv.RefreshData();
                        gv.GridControl.Refresh();
                        IEnumerable<object> entityDataSource = (IEnumerable<object>)GetCacheList(entityName);
                      
                        var entity =
                            entityDataSource.LastOrDefault(t => DataHelper.Find(t, "Owner", FrmMain.UserKod));
                        UserColumnSettings pk = Cache.UserColumnSettingsList.First(t => t.DefaultCaption == "PK_ALAN" && t.EntityNameEnum == entityName);
    
                        if (entity != null)
                        {
                            entityDataSource.Where(t => DataHelper.Find(t, "Owner", FrmMain.UserKod)).Cast<Entity>().ToList()
                                .ForEach(t => t.Owner = null);
                            //((Entity)entity).Owner = null;
                            try
                            {
                                object value = DataHelper.GetEntityValueFromStringFieldName(entity, pk.FieldName);
                                gv.FocusedRowHandle = gv.LocateByValue(pk.FieldName, value);
                            }
                            catch (Exception ex)
                            {
                                Logger.LogException("FocusedRowHandle " + entityName, ex);
                            }
                        }
                        else
                        {
                            SendFrmMainMessage(e.ListChangedType, pk.FormName, "");
                            gv.FocusedRowHandle = focusedRowHandle;
                            gv.SelectRow(focusedRowHandle);
                        }
                        gv.RefreshData();
                        RefreshGridColumnDatasource(entityName, gv);
    
                    }
                }
                catch (Exception ex)
                {
                    ExceptionManager.Manage(ex, true, "",
                                            string.Format("WinHelper.OnListChanged()\nEntityName: {0}", entityName));
                }
            }
    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Reflection;
    using Utilities;
    
    namespace Model
    {
        /// <summary>
        /// Defines the method signature for a filter
        /// provider method used by FilteredBindingList.
        /// </summary>
        /// <param name="item">The object to be filtered.</param>
        /// <param name="filter">The filter criteria.</param>
        /// <returns><see langword="true"/> if the item matches the filter.</returns>
        public delegate bool FilterProvider(object item, object filter);
    
        /// <summary>
        /// Provides a filtered view into an existing IList(Of T).
        /// </summary>
        /// <typeparam name="T">The type of the objects contained
        /// in the original list.</typeparam>
        public class FilteredBindingList<T> :
          IList<T>, IBindingList,
          ICancelAddNew
        {
            private string _sortPropertyName;
            private ListSortDirection _direction;
            #region ListItem class
    
            private class ListItem
            {
                private readonly object _key;
                private int _baseIndex;
    
                public object Key
                {
                    get { return _key; }
                }
    
                public int BaseIndex
                {
                    get { return _baseIndex; }
                    set { _baseIndex = value; }
                }
    
                public ListItem(object key, int baseIndex)
                {
                    _key = key;
                    _baseIndex = baseIndex;
                }
    
                public override string ToString()
                {
                    return Key.ToString();
                }
    
            }
    
            #endregion
    
            #region Filtered enumerator
    
            private class FilteredEnumerator : IEnumerator<T>
            {
                private IList<T> _list;
                private List<ListItem> _filterIndex;
                private int _index;
    
                public FilteredEnumerator(
                  IList<T> list,
                  List<ListItem> filterIndex)
                {
                    _list = list;
                    _filterIndex = filterIndex;
                    Reset();
                }
    
                public T Current
                {
                    get { return _list[_filterIndex[_index].BaseIndex]; }
                }
    
                Object IEnumerator.Current
                {
                    get { return _list[_filterIndex[_index].BaseIndex]; }
                }
    
                public bool MoveNext()
                {
                    if (_index < _filterIndex.Count - 1)
                    {
                        _index++;
                        return true;
                    }
                    return false;
                }
    
                public void Reset()
                {
                    _index = -1;
                }
    
                #region IDisposable Support
    
                private bool _disposedValue; // To detect redundant calls.
    
                // IDisposable
                protected virtual void Dispose(bool disposing)
                {
                    if (!_disposedValue)
                    {
                        if (disposing)
                        {
                            // TODO: free unmanaged resources when explicitly called
                        }
                        // TODO: free shared unmanaged resources
                    }
                    _disposedValue = true;
                }
    
                // this code added to correctly implement the disposable pattern.
                public void Dispose()
                {
                    // Do not change this code.  Put cleanup code in Dispose(bool disposing) above.
                    Dispose(true);
                    GC.SuppressFinalize(this);
                }
    
                ~FilteredEnumerator()
                {
                    Dispose(false);
                }
    
                #endregion
    
            }
    
            #endregion
    
            #region Filter/Unfilter
    
            private void DoFilter()
            {
                int index = 0;
                _filterIndex.Clear();
    
                if (_provider == null)
                    _provider = DefaultFilter.Filter;
    
                if (_filterBy == null)
                {
                    foreach (T obj in _list)
                    {
                        if (_provider.Invoke(obj, _filter))
                            _filterIndex.Add(new ListItem(obj, index));
                        index++;
                    }
                }
                else
                {
                    foreach (T obj in _list)
                    {
                        object tmp = _filterBy.GetValue(obj);
                        if (_provider.Invoke(tmp, _filter))
                            _filterIndex.Add(new ListItem(tmp, index));
                        index++;
                    }
                }
    
                _filtered = true;
    
                OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));
    
            }
    
            private void UnDoFilter()
            {
                _filterIndex.Clear();
                _filterBy = null;
                _filter = null;
                _filtered = false;
                OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, 0));
            }
    
            public void ChangeList(ListChangedType changedType, int newIndex)
            {
                OnListChanged(new ListChangedEventArgs(changedType, newIndex));
            }
            #endregion
    
            #region IEnumerable<T>
    
            /// <summary>
            /// Gets an enumerator object.
            /// </summary>
            /// <returns></returns>
            public IEnumerator<T> GetEnumerator()
            {
                if (_filtered)
                    return new FilteredEnumerator(_list, _filterIndex);
                return _list.GetEnumerator();
            }
    
            #endregion
    
            #region IBindingList, IList<T>
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            /// <param name="property">Property on which
            /// to build the index.</param>
            public void AddIndex(PropertyDescriptor property)
            {
                if (_supportsBinding)
                    _bindingList.AddIndex(property);
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            public object AddNew()
            {
                T result;
                if (_supportsBinding)
                    result = (T)_bindingList.AddNew();
                else
                    result = default(T);
    
                //_newItem = (T)result;
                return result;
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            public bool AllowEdit
            {
                get
                {
                    if (_supportsBinding)
                        return _bindingList.AllowEdit;
                    return false;
                }
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            public bool AllowNew
            {
                get
                {
                    if (_supportsBinding)
                        return _bindingList.AllowNew;
                    return false;
                }
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            public bool AllowRemove
            {
                get
                {
                    if (_supportsBinding)
                        return _bindingList.AllowRemove;
                    return false;
                }
            }
    
            /// <summary>
            /// Sorts the list if the original list
            /// supports sorting.
            /// </summary>
            /// <param name="property">Property on which to sort.</param>
            /// <param name="direction">Direction of the sort.</param>
            [Obsolete("Moved ApplyLinqSort", false)]
            public void ApplySort(
              PropertyDescriptor property, ListSortDirection direction)
            {
                if (SupportsSorting)
                    _bindingList.ApplySort(property, direction);
                else
                    throw new NotSupportedException("Sorting not supported.");
            }
    
            [Obsolete("Moved ApplyLinqSort", false)]
            public void ApplySort(
              string propertyName, ListSortDirection direction)
            {
                PropertyDescriptor sortProperty = null;
                _direction = direction;
                if (!String.IsNullOrEmpty(propertyName))
                {
                    Type itemType = typeof(T);
                    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(itemType))
                    {
                        if (prop.Name == propertyName)
                        {
                            sortProperty = prop;
                            break;
                        }
                    }
                    if (SupportsSorting)
                        _bindingList.ApplySort(sortProperty, direction);
                    else
                        throw new NotSupportedException("Sorting not supported.");
                }
    
            }
    
            public void ApplySortLinq(
              string propertyName, ListSortDirection direction)
            {
                _sortPropertyName = propertyName;
                if (!string.IsNullOrWhiteSpace(_sortPropertyName))
                {
                    StopFilter();
                    RemoveListChangedEvent();
                    List<T> tmp = new List<T>(this);
                    Clear();
                    PropertyInfo property = typeof(T).GetProperty(propertyName);
                    switch (direction)
                    {
                        case ListSortDirection.Ascending:
                            tmp.OrderBy(e => property.GetValue(e, null)).ToList().ForEach(Add);
                            break;
                        case ListSortDirection.Descending:
                            tmp.OrderByDescending(e => property.GetValue(e, null)).ToList().ForEach(Add);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("direction");
                    }
                    AddListChangedEvent();
                    ResumeFilter();
                }
            }
    
            public void ApplySortLinq2(
              string propertyName, string propertyName2,string propertyName3, ListSortDirection direction)
            {
                _sortPropertyName = propertyName;
                if (!string.IsNullOrWhiteSpace(_sortPropertyName))
                {
                    StopFilter();
                    RemoveListChangedEvent();
                    List<T> tmp = new List<T>(this);
                    
                    Clear();
                    PropertyInfo property = typeof(T).GetProperty(propertyName);
                    PropertyInfo property2 = typeof(T).GetProperty(propertyName2);
                    PropertyInfo property3 = typeof(T).GetProperty(propertyName3);
                    
    
                    switch (direction)
                    {
                        case ListSortDirection.Ascending:
    
                            tmp.OrderBy(e => property.GetValue(e, null)).ThenBy(c => property2.GetValue(c, null)).ThenBy(g => property3.GetValue(g, null)).ToList().ForEach(Add);
                            break;
                        case ListSortDirection.Descending:
                            tmp.OrderByDescending(e => property.GetValue(e, null)).ThenBy(c => property2.GetValue(c, null)).ThenBy(g => property3.GetValue(g, null)).ToList().ForEach(Add);
                            break;
                        default:
                            throw new ArgumentOutOfRangeException("direction");
                    }
    
                    DataTable dt = DataHelper.ToDataTable(tmp);
                    DataView view = new DataView(dt);
                    view.Sort ="FisTarih ASC,FisTipNo,CreationTime ASC";
                    
                    AddListChangedEvent();
                    ResumeFilter();
                }
            }
    
    
            [Obsolete("Moved ApplyLinqSort", false)]
            public void ApplySort(
               ListSortDescriptionCollection sorts)
            {
                ((SortableBindingList<T>)_bindingList).ApplySort(sorts);
            }
    
            public int CoreCount
            {
                get { return ((SortableBindingList<T>)_bindingList).Count; }
            }
    
            public SortableBindingList<T> CoreList
            {
                get { return ((SortableBindingList<T>)_bindingList); }
            }
    
            /// <summary>
            /// Finds an item in the view
            /// </summary>
            /// <param name="propertyName">Name of the property to search</param>
            /// <param name="key">Value to find</param>
            public int Find(string propertyName, object key)
            {
                PropertyDescriptor findProperty = null;
    
                if (!String.IsNullOrEmpty(propertyName))
                {
                    Type itemType = typeof(T);
                    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(itemType))
                    {
                        if (prop.Name == propertyName)
                        {
                            findProperty = prop;
                            break;
                        }
                    }
                }
                return Find(findProperty, key);
    
            }
    
            public List<int> FindInText(string propertyName, object key, StringComparison strComparison)
            {
                PropertyDescriptor findProperty = null;
    
                if (!String.IsNullOrEmpty(propertyName))
                {
                    Type itemType = typeof(T);
                    foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(itemType))
                    {
                        if (prop.Name == propertyName)
                        {
                            findProperty = prop;
                            break;
                        }
                    }
                }
    
                return FindInText(findProperty, key, strComparison);
    
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            /// <param name="key">Key value for which to search.</param>
            /// <param name="property">Property to search for the key
            /// value.</param>
            public int Find(PropertyDescriptor property, object key)
            {
                if (_supportsBinding)
                    return FilteredIndex(_bindingList.Find(property, key));
                return -1;
            }
    
            public List<int> FindInText(PropertyDescriptor property, object key, StringComparison strComparison)
            {
                List<int> hitList = new List<int>();
                if (_supportsBinding)
                {
                    foreach (int index in ((SortableBindingList<T>)_bindingList).FindInText(property, key, strComparison))
                    {
                        hitList.Add(FilteredIndex(index));
                    }
                }
                return hitList;
            }
    
            public void ForEach(Action<T> action)
            {
                ((SortableBindingList<T>)_bindingList).ForEach(action);
            }
    
            /// <summary>
            /// Returns True if the view is currently sorted.
            /// </summary>
            public bool IsSorted
            {
                get
                {
                    if (SupportsSorting)
                        return _bindingList.IsSorted;
                    return false;
                }
            }
    
            /// <summary>
            /// Raised to indicate that the list's data has changed.
            /// </summary>
            /// <remarks>
            /// This event is raised if the underling IList object's data changes
            /// (assuming the underling IList also implements the IBindingList
            /// interface). It is also raised if the filter
            /// is changed to indicate that the view's data has changed.
            /// </remarks>
            public event ListChangedEventHandler ListChanged;
    
            /// <summary>
            /// Raises the ListChanged event.
            /// </summary>
            /// <param name="e">Parameter for the event.</param>
            protected void OnListChanged(ListChangedEventArgs e)
            {
                if (ListChanged != null && !_stopListChangeNotify)
                    ListChanged(this, e);
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            /// <param name="property">Property for which the
            /// index should be removed.</param>
            public void RemoveIndex(PropertyDescriptor property)
            {
                if (_supportsBinding)
                    _bindingList.RemoveIndex(property);
            }
    
            /// <summary>
            /// Removes any sort currently applied to the view.
            /// </summary>
            public void RemoveSort()
            {
                if (SupportsSorting)
                    _bindingList.RemoveSort();
                else
                    throw new NotSupportedException("Sorting not supported");
            }
    
            /// <summary>
            /// Returns the direction of the current sort.
            /// </summary>
            public ListSortDirection SortDirection
            {
                get
                {
                    if (SupportsSorting)
                        return _bindingList.SortDirection;
                    return ListSortDirection.Ascending;
                }
            }
    
            /// <summary>
            /// Returns the PropertyDescriptor of the current sort.
            /// </summary>
            public PropertyDescriptor SortProperty
            {
                get
                {
                    if (SupportsSorting)
                        return _bindingList.SortProperty;
                    return null;
                }
            }
    
            /// <summary>
            /// Returns True since this object does raise the
            /// ListChanged event.
            /// </summary>
            public bool SupportsChangeNotification
            {
                get { return true; }
            }
    
            /// <summary>
            /// Implemented by IList source object.
            /// </summary>
            public bool SupportsSearching
            {
                get
                {
                    if (_supportsBinding)
                        return _bindingList.SupportsSearching;
                    return false;
                }
            }
    
            /// <summary>
            /// Returns True. Sorting is supported.
            /// </summary>
            public bool SupportsSorting
            {
                get
                {
                    if (_supportsBinding)
                        return _bindingList.SupportsSorting;
                    return false;
                }
            }
    
            /// <summary>
            /// Copies the contents of the list to
            /// an array.
            /// </summary>
            /// <param name="array">Array to receive the data.</param>
            /// <param name="arrayIndex">Starting array index.</param>
            public void CopyTo(T[] array, int arrayIndex)
            {
                _list.CopyTo(array, arrayIndex);
            }
    
            void ICollection.CopyTo(Array array, int index)
            {
                CopyTo((T[])array, index);
            }
    
            /// <summary>
            /// Gets the number of items in the list.
            /// </summary>
            public int Count
            {
                get
                {
                    if (_filtered)
                        return _filterIndex.Count;
                    return _list.Count;
                }
            }
    
            bool ICollection.IsSynchronized
            {
                get { return false; }
            }
    
            object ICollection.SyncRoot
            {
                get { return _list; }
            }
    
            public object SyncRoot
            {
                get { return _list; }
            }
    
            IEnumerator IEnumerable.GetEnumerator()
            {
                return GetEnumerator();
            }
    
            /// <summary>
            /// Adds an item to the list.
            /// </summary>
            /// <param name="item">Item to be added.</param>
            public void Add(T item)
            {
                _list.Add(item);
            }
    
            int IList.Add(object value)
            {
                Add((T)value);
                int index = FilteredIndex(_list.Count - 1);
                if (index > -1)
                    return index;
                return 0;
            }
    
            /// <summary>
            /// Clears the list.
            /// </summary>
            public void Clear()
            {
                _list.Clear();
            }
    
            /// <summary>
            /// Determines whether the specified
            /// item is contained in the list.
            /// </summary>
            /// <param name="item">Item to find.</param>
            /// <returns><see langword="true"/> if the item is
            /// contained in the list.</returns>
            public bool Contains(T item)
            {
                return _list.Contains(item);
            }
    
            bool IList.Contains(object value)
            {
                return Contains((T)value);
            }
    
            /// <summary>
            /// Gets the 0-based index of an
            /// item in the list.
            /// </summary>
            /// <param name="item">The item to find.</param>
            /// <returns>0-based index of the item
            /// in the list.</returns>
            public int IndexOf(T item)
            {
                return FilteredIndex(_list.IndexOf(item));
            }
    
            int IList.IndexOf(object value)
            {
                return IndexOf((T)value);
            }
    
            /// <summary>
            /// Inserts an item into the list.
            /// </summary>
            /// <param name="index">Index at
            /// which to insert the item.</param>
            /// <param name="item">Item to insert.</param>
            public void Insert(int index, T item)
            {
                _list.Insert(index, item);
            }
    
            void IList.Insert(int index, object value)
            {
                Insert(index, (T)value);
            }
    
            bool IList.IsFixedSize
            {
                get { return false; }
            }
    
            /// <summary>
            /// Gets a value indicating whether the list
            /// is read-only.
            /// </summary>
            public bool IsReadOnly
            {
                get { return _list.IsReadOnly; }
            }
    
            object IList.this[int index]
            {
                get
                {
                    return this[index];
                }
                set
                {
                    this[index] = (T)value;
                }
            }
    
            /// <summary>
            /// Removes an item from the list.
            /// </summary>
            /// <param name="item">Item to remove.</param>
            /// <returns><see langword="true"/> if the 
            /// remove succeeds.</returns>
            public bool Remove(T item)
            {
                return _list.Remove(item);
            }
    
            void IList.Remove(object value)
            {
                Remove((T)value);
            }
    
            /// <summary>
            /// Removes an item from the list.
            /// </summary>
            /// <param name="index">Index of item
            /// to be removed.</param>
            public void RemoveAt(int index)
            {
                if (_filtered)
                {
                    _list.RemoveAt(OriginalIndex(index));
                }
                else
                    _list.RemoveAt(index);
            }
    
            /// <summary>
            /// Gets or sets the item at 
            /// the specified index.
            /// </summary>
            /// <param name="index">Index of the item.</param>
            /// <returns>Item at the specified index.</returns>
            public T this[int index]
            {
                get
                {
                    if (_filtered)
                    {
                        int src = OriginalIndex(index);
                        return _list[src];
                    }
                    return _list[index];
                }
                set
                {
                    if (_filtered)
                        _list[OriginalIndex(index)] = value;
                    else
                        _list[index] = value;
                }
            }
    
            #endregion
    
            private IList<T> _list;
            private bool _supportsBinding;
            private IBindingList _bindingList;
            private bool _filtered;
            private bool _filterStoped;
            private PropertyDescriptor _filterBy;
            private object _filter;
            FilterProvider _provider = null;
            private List<ListItem> _filterIndex =
              new List<ListItem>();
    
            private PropertyDescriptor _tempFilterBy;
            private object _tempFilter;
            private bool _stopListChangeNotify;
    
            /// <summary>
            /// Creates a new view based on the provided IList object.
            /// </summary>
            /// <param name="list">The IList (collection) containing the data.</param>
            public FilteredBindingList(IList<T> list)
            {
                _list = list;
    
                if (_list is IBindingList)
                {
                    _supportsBinding = true;
                    _bindingList = (IBindingList)_list;
                    _bindingList.ListChanged += SourceChanged;
                }
            }
    
            /// <summary>
            /// Creates a new view based on the provided IList object.
            /// </summary>
            /// <param name="list">The IList (collection) containing the data.</param>
            /// <param name="filterProvider">
            /// Delegate pointer to a method that implements the filter behavior.
            /// </param>
            public FilteredBindingList(IList<T> list, FilterProvider filterProvider)
                : this(list)
            {
                _provider = filterProvider;
            }
    
            /// <summary>
            /// Gets or sets the filter provider method.
            /// </summary>
            /// <value>
            /// Delegate pointer to a method that implements the filter behavior.
            /// </value>
            /// <returns>
            /// Delegate pointer to a method that implements the filter behavior.
            /// </returns>
            /// <remarks>
            /// If this value is set to Nothing (null in C#) then the default
            /// filter provider, <see cref="DefaultFilter" /> will be used.
            /// </remarks>
            public FilterProvider FilterProvider
            {
                get
                {
                    return _provider;
                }
                set
                {
                    _provider = value;
                }
            }
    
            /// <summary>
            /// The property on which the items will be filtered.
            /// </summary>
            /// <value>A descriptor for the property on which
            /// the items in the collection will be filtered.</value>
            /// <returns></returns>
            /// <remarks></remarks>
            public PropertyDescriptor FilterProperty
            {
                get { return _filterBy; }
            }
    
            /// <summary>
            /// Returns True if the view is currently filtered.
            /// </summary>
            public bool IsFiltered
            {
                get { return _filtered; }
            }
    
            public object FilterCond { get { return _filter; } }
    
            /// <summary>
            /// Applies a filter to the view.
            /// </summary>
            /// <param name="propertyName">The text name of the property on which to filter.</param>
            /// <param name="filter">The filter criteria.</param>
            public void ApplyFilter(string propertyName, object filter)
            {
                _filterBy = null;
                _filter = filter;
    
                if (!String.IsNullOrEmpty(propertyName))
                {
                    Type itemType = typeof(T);
                    foreach (PropertyDescriptor prop in
                      TypeDescriptor.GetProperties(itemType))
                    {
                        if (prop.Name == propertyName)
                        {
                            _filterBy = prop;
                            break;
                        }
                    }
                }
                ApplyFilter(_filterBy, filter);
            }
    
            /// <summary>
            /// Applies a filter to the view.
            /// </summary>
            /// <param name="property">A PropertyDescriptor for the property on which to filter.</param>
            /// <param name="filter">The filter criteria.</param>
            public void ApplyFilter(
              PropertyDescriptor property, object filter)
            {
                _filterBy = property;
                _filter = filter;
                DoFilter();
            }
    
            /// <summary>
            /// Removes the filter from the list,
            /// so the view reflects the state of
            /// the original list.
            /// </summary>
            public void RemoveFilter()
            {
                UnDoFilter();
            }
    
            private void SourceChanged(
              object sender, ListChangedEventArgs e)
            {
                if (_filtered)
                {
                    int listIndex;
                    int filteredIndex = -1;
                    T newItem;
                    object newKey;
                    switch (e.ListChangedType)
                    {
                        case ListChangedType.ItemAdded:
                            listIndex = e.NewIndex;
                            // add new value to index
                            newItem = _list[listIndex];
                            if (_filterBy != null)
                                newKey = _filterBy.GetValue(newItem);
                            else
                                newKey = newItem;
                            _filterIndex.Add(
                              new ListItem(newKey, listIndex));
                            filteredIndex = _filterIndex.Count - 1;
                            // raise event 
                            OnListChanged(
                              new ListChangedEventArgs(
                              e.ListChangedType, filteredIndex));
                            break;
    
                        case ListChangedType.ItemChanged:
                            listIndex = e.NewIndex;
                            // update index value
                            filteredIndex = FilteredIndex(listIndex);
                            if (filteredIndex != -1)
                            {
                                newItem = _list[listIndex];
                                if (_filterBy != null)
                                    newKey = _filterBy.GetValue(newItem);
                                else
                                    newKey = newItem;
                                _filterIndex[filteredIndex] =
                                  new ListItem(newKey, listIndex);
                            }
                            // raise event if appropriate
                            if (filteredIndex > -1)
                                OnListChanged(
                                  new ListChangedEventArgs(
                                  e.ListChangedType, filteredIndex));
                            break;
    
                        case ListChangedType.ItemDeleted:
                            listIndex = e.NewIndex;
                            // delete corresponding item from index
                            // (if any)
                            filteredIndex = FilteredIndex(listIndex);
                            if (filteredIndex != -1)
                                _filterIndex.RemoveAt(filteredIndex);
                            // adjust index xref values
                            foreach (ListItem item in _filterIndex)
                                if (item.BaseIndex > e.NewIndex)
                                    item.BaseIndex--;
                            // raise event if appropriate
                            if (filteredIndex > -1)
                                OnListChanged(
                                  new ListChangedEventArgs(
                                  e.ListChangedType, filteredIndex));
                            break;
    
                        case ListChangedType.PropertyDescriptorAdded:
                        case ListChangedType.PropertyDescriptorChanged:
                        case ListChangedType.PropertyDescriptorDeleted:
                            OnListChanged(e);
                            break;
    
                        default:
                            DoFilter();
                            OnListChanged(
                              new ListChangedEventArgs(
                              ListChangedType.Reset, 0));
                            break;
                    }
                }
                else
                    OnListChanged(e);
            }
    
            public void RemoveListChangedEvent()
            {
                _bindingList.ListChanged -= SourceChanged;
                _stopListChangeNotify = true;
            }
    
            public void AddListChangedEvent()
            {
                if (_stopListChangeNotify)
                {
                    _bindingList.ListChanged += SourceChanged;
                    _stopListChangeNotify = false;
                }
            }
    
            private int OriginalIndex(int filteredIndex)
            {
                if (_filtered)
                    return _filterIndex[filteredIndex].BaseIndex;
                return filteredIndex;
            }
    
            private int FilteredIndex(int originalIndex)
            {
                int result = -1;
                if (_filtered)
                {
                    for (int index = 0; index < _filterIndex.Count; index++)
                    {
                        if (_filterIndex[index].BaseIndex == originalIndex)
                        {
                            result = index;
                            break;
                        }
                    }
                }
                else
                    result = originalIndex;
                return result;
            }
    
            public void StopFilter()
            {
                if (_filtered)
                {
                    _filterStoped = true;
                    RemoveListChangedEvent();
                    _tempFilterBy = _filterBy;
                    _tempFilter = _filter;
                    RemoveFilter();
                    AddListChangedEvent();
                }
            }
    
            public void ResumeFilter()
            {
                if (_filterStoped)
                {
                    _filterStoped = false;
                    RemoveListChangedEvent();
                    ApplyFilter(_tempFilterBy, _tempFilter);
                    AddListChangedEvent();
                }
            }
    
            public void RefreshLinqSort()
            {
                ApplySortLinq(_sortPropertyName, _direction);
            }
    
            #region ICancelAddNew Members
    
            //private T _newItem;
    
            //void ICancelAddNew.CancelNew(int itemIndex)
            //{
            //  if (_newItem != null)
            //    Remove(_newItem);
            //}
    
            //void ICancelAddNew.EndNew(int itemIndex)
            //{
            //  // do nothing
            //}
    
            void ICancelAddNew.CancelNew(int itemIndex)
            {
                ICancelAddNew can = _list as ICancelAddNew;
                if (can != null)
                    can.CancelNew(itemIndex);
                else
                    _list.RemoveAt(itemIndex);
            }
    
            void ICancelAddNew.EndNew(int itemIndex)
            {
                ICancelAddNew can = _list as ICancelAddNew;
                if (can != null)
                    can.EndNew(itemIndex);
            }
    
            #endregion
        }
    }
    



    Gökhan TIKNAZOĞLU