本文介绍了的BindingList< T> .Sort()的行为就像一个List< T> .Sort()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写的,我可以用我的应用程序SortableBindingList。我发现许多关于如何实现基本排序支持,以便在一个DataGridView或其他绑定控件,包括此帖来自计算器的情况下使用时的BindingList排序将讨论:
http://stackoverflow.com/questions/249779/datagridview-sort-and-e-g-bindinglistt-in-net

这是非常有益的,我已经实现了code,测试等,这一切都工作,但在我的特殊情况,我需要能够支持简单的调用排序()和有调用使用默认IComparable.CompareTo()来进行排序,而不是在打电话给ApplySortCore(的PropertyDescriptor,ListSortDirection)。

的原因是因为我有相当大量的code多数民众赞成根据排序()调用,因为最初是从列表继承,最近改为一个的BindingList这个特定的类。

所以具体而言,我有一类称为变量code和所谓的变量codeLIST一个集合类。变量code实现IComparable接口,并在那里基于以下几个特性是比较复杂的逻辑,等等...

 公共类变量code:... IComparable的...
{
    公众诠释的CompareTo(对象p_Target)
    {
        INT输出= 0;
        //一些有趣的东西在这里
        返回输出;
    }
}

公共类变量codeLIST:SortableBindingList<变量code取代;
{
    公共无效排序()
    {
        //这是我需要帮助
        //我怎么排序使用了IComparable此列表
        //从上面的类的逻辑?
    }
}
 

我做的再利用的ApplySortCore方法排序()几个失败的尝试,但什么让我挫败的是,ApplySortCore需要一个的PropertyDescriptor尽自己的排序,我无法弄清楚如何得到这使用IComparable.CompareTo()的逻辑。

有人点我在正确的方向?

非常感谢。


编辑:这是根据马克的答复以备将来参考最后的code

  ///<总结>
  使用T的默认的IComparer ///排序
  ///< /总结>
  公共无效排序()
  {
     排序(NULL,NULL);
  }
  公共无效排序(IComparer的< T> p_Comparer)
  {
     排序(p_Comparer,NULL);
  }
  公共无效排序(比较< T> p_Comparison)
  {
     排序(空,p_Comparison);
  }
  私人无效排序(IComparer的< T> p_Comparer,比较< T> p_Comparison)
  {

     m_SortProperty = NULL;
     m_SortDirection = ListSortDirection.Ascending;

     //提取物项和排序分别
     名单< T> sortlist中=新的名单,其中,T>();
     this.ForEach(项目=> sortList.Add(项目)); //扩展方法,此调用
     如果(p_Comparison == NULL)
     {
        sortList.Sort(p_Comparer);
     }//如果
     其他
     {
        sortList.Sort(p_Comparison);
     }//其他

     //停用的通知,重建,并重新启用通知
     布尔oldRaise = RaiseListChangedEvents;
     RaiseListChangedEvents = FALSE;
     尝试
     {
        ClearItems();
        sortList.ForEach(项目=> this.Add(项目));
     }
     最后
     {
        RaiseListChangedEvents = oldRaise;
        ResetBindings();
     }

  }
 

解决方案

模拟一个属性只是做排序可能是矫枉过正。看的第一件事就是的Comparer< T> .DEFAULT 。它可能,但是,变成了最容易做的事情是:

  • 提取数据到名单,其中,T> 或类似
  • 排序所提取的数据
  • 禁用通知
  • 重新载入数据
  • 重新启用通知
  • 发送一个复位消息

顺便说一句,你应该将现有的排序过程中禁用通知了。

 公共无效排序(){
    // TODO:明确你的之类的变量(托/顺序)

    T [] ARR =新T [计数]
    CopyTo从(ARR,0);
    的Array.Sort(ARR);
    布尔oldRaise = RaiseListChangedEvents;
    RaiseListChangedEvents = FALSE; //< ===哎呀,说!
    尝试 {
        ClearItems();
        的foreach(T在ARR项){
            添加(项目);
        }
    } 最后 {
        RaiseListChangedEvents = oldRaise;
        ResetBindings();
    }
}
 

I am attempting to write a SortableBindingList that I can use for my application. I have found lots of discussion about how to implement basic sorting support so that the BindingList will sort when used in the context of a DataGridView or some other bound control including this post from StackOverflow:
http://stackoverflow.com/questions/249779/datagridview-sort-and-e-g-bindinglistt-in-net

This is all very helpful and I have implemented the code, tested, etc. and it's all working, but in my particular situation, I need to be able to support a simple call to Sort() and have that call use the default IComparable.CompareTo() to do the sorting, rather than making a call to ApplySortCore(PropertyDescriptor, ListSortDirection).

The reason is because I have quite a great deal of code that's depending on the Sort() call because this particular class originally inherited from List and was recently changed to be a BindingList.

So specifically, I have a class called VariableCode and a collection class called VariableCodeList. VariableCode implements IComparable and the logic in there is moderately complex based on several properties, etc...

public class VariableCode : ...  IComparable ...
{
    public int CompareTo(object p_Target)
    {
        int output = 0;
        //some interesting stuff here
        return output;
    }
}

public class VariableCodeList : SortableBindingList<VariableCode>
{
    public void Sort()
    {
        //This is where I need help
        //  How do I sort this list using the IComparable
        //  logic from the class above?
    }
}

I've made a few failed attempts at repurposing the ApplySortCore method in the Sort(), but what keeps thwarting me is that the ApplySortCore expects a PropertyDescriptor to do its sort and I can't figure out how to get that to use the IComparable.CompareTo() logic.

Can someone point me in the right direction?

Many thanks.


EDIT: This is the final code based on Marc's response for future reference.

  /// <summary>
  /// Sorts using the default IComparer of T
  /// </summary>
  public void Sort()
  {
     sort(null, null);
  }
  public void Sort(IComparer<T> p_Comparer)
  {
     sort(p_Comparer, null);
  }
  public void Sort(Comparison<T> p_Comparison)
  {
     sort(null, p_Comparison);
  }
  private void sort(IComparer<T> p_Comparer, Comparison<T> p_Comparison)
  {

     m_SortProperty = null;
     m_SortDirection = ListSortDirection.Ascending;

     //Extract items and sort separately
     List<T> sortList = new List<T>();
     this.ForEach(item => sortList.Add(item));//Extension method for this call
     if (p_Comparison == null)
     {
        sortList.Sort(p_Comparer);
     }//if
     else
     {
        sortList.Sort(p_Comparison);
     }//else

     //Disable notifications, rebuild, and re-enable notifications
     bool oldRaise = RaiseListChangedEvents;
     RaiseListChangedEvents = false;
     try
     {
        ClearItems();
        sortList.ForEach(item => this.Add(item));
     }
     finally
     {
        RaiseListChangedEvents = oldRaise;
        ResetBindings();
     }

  }
解决方案

Emulating a property just to do the sort is probably overkill.The first thing to look at is Comparer<T>.Default. It might, however, turn out that the easiest thing to do is to:

  • extract the data into List<T> or similar
  • sort the extracted data
  • disable notifications
  • reload the data
  • re-enable notifications
  • send a "reset" message

btw, you should be disabling notifications during your existing sort, too.

public void Sort() {
    // TODO: clear your "sort" variables (prop/order)

    T[] arr = new T[Count];
    CopyTo(arr, 0);
    Array.Sort(arr);
    bool oldRaise = RaiseListChangedEvents;
    RaiseListChangedEvents = false; // <=== oops, added!
    try {
        ClearItems();
        foreach (T item in arr) {
            Add(item);
        }
    } finally {
        RaiseListChangedEvents = oldRaise;
        ResetBindings();
    }
}

这篇关于的BindingList&LT; T&GT; .Sort()的行为就像一个List&LT; T&GT; .Sort()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:38