DataGridView и SortableBindingList - PullRequest
       3

DataGridView и SortableBindingList

0 голосов
/ 17 февраля 2020

У меня есть сортируемый список привязок, установленный в качестве источника данных для сетки данных. Элементы этого списка обновляются, добавляются или удаляются на основе данных, полученных из сокета. Он прекрасно обновляется в представлении данных.

//At the time of initialization
SortableBindingList<T> SBL = new SortableBindingList<T>(ListFromSocket);
datagridview1.DataSource = SBL;

При каждом обновлении списка я добавляю / удаляю элементы списка и затем сортирую представление данных. Это сильно замедляет интерфейс.

void UpdateListFromSocket(List<T> newList)
{
    //And on every update of the socket
    foreach(<T> obj in SBL)
    {
        //do some checks
        //update, add or remove items from the list
    }

    datagridview1.Sort(datagridview1.Columns[index], ListSortDirection.Ascending);
}

По мере роста списка вид сетки данных замедляет работу пользовательского интерфейса. Я попытался создать SortableBindingList (уже отсортированный), который является подмножеством исходного SortableBindingList. Но когда я устанавливаю это как источник привязки при обновлении, представление данных начинает мигать.

Я хотел бы знать, как лучше всего справиться с этим без ущерба для производительности.

Я использую SortableBindingList

    public class SortableBindingList<T> : BindingList<T>
    {
        private ArrayList sortedList;
        private bool isSortedValue;

        public SortableBindingList()
        {
        }

        public SortableBindingList(IList<T> list)
        {
            foreach (object o in list)
            {
                this.Add((T)o);
            }
        }

        protected override bool SupportsSortingCore
        {
            get { return true; }
        }


        protected override bool IsSortedCore
        {
            get { return isSortedValue; }
        }

        ListSortDirection sortDirectionValue;
        PropertyDescriptor sortPropertyValue;

        protected override void ApplySortCore(PropertyDescriptor prop,
            ListSortDirection direction)
        {
            sortedList = new ArrayList();

            Type interfaceType = prop.PropertyType.GetInterface("IComparable");

            if (interfaceType == null && prop.PropertyType.IsValueType)
            {
                Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);

                if (underlyingType != null)
                {
                    interfaceType = underlyingType.GetInterface("IComparable");
                }
            }

            if (interfaceType != null)
            {
                sortPropertyValue = prop;
                sortDirectionValue = direction;

                IEnumerable<T> query = base.Items;
                decimal value;
                if (direction == ListSortDirection.Ascending)
                {
                    // check if element count is not 0 and value is decimal
                    if (query.Count() > 0 &&
                        decimal.TryParse(prop.GetValue(query.ElementAt<T>(0)).ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out value))
                    {
                        // convert to decimal and sort
                        query = query.OrderBy(i => Convert.ToDecimal(prop.GetValue(i)));
                    }
                    else query = query.OrderBy(i => prop.GetValue(i));
                }
                else
                {
                    if (query.Count() > 0 &&
                        decimal.TryParse(prop.GetValue(query.ElementAt<T>(0)).ToString(), NumberStyles.Any, CultureInfo.InvariantCulture, out value))
                    {
                        query = query.OrderByDescending(i => Convert.ToDecimal(prop.GetValue(i)));
                    }
                    else query = query.OrderByDescending(i => prop.GetValue(i));
                }
                int newIndex = 0;
                foreach (object item in query)
                {
                    this.Items[newIndex] = (T)item;
                    newIndex++;
                }
                isSortedValue = true;
                this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));

            }
            else
            {
                throw new NotSupportedException("Cannot sort by " + prop.Name +
                    ". This" + prop.PropertyType.ToString() +
                    " does not implement IComparable");
            }
        }

        protected override PropertyDescriptor SortPropertyCore
        {
            get { return sortPropertyValue; }
        }

        protected override ListSortDirection SortDirectionCore
        {
            get { return sortDirectionValue; }
        }

    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...