INT32?与IComparable

Dav*_*vid 10 .net c#

我有一个DataGridView,其数据源是BindingList.MyObj有一些可以为空的属性(比如int?和DateTime?)我想实现对绑定列表的排序,因此当用户点击列标题时,DataGridView可以对列进行排序.

经过一番挖掘,我发现并遵循了这个问题的答案(使用Business Objects进行DataGridView列排序).

我无法让这个解决方案适用于Nullable类型,因为它们没有实现IComparable.即使对于像String这样实现IComparable的类,当String具有空值时,ApplySortCore(...)也会失败.

这有解决方案吗?或者我是否必须为"Int32"实现包装类??

例如

public class Int32Comparable : IComparable
{
    public int? Value { get; set; }

    #region IComparable<int?> Members

    public int CompareTo(object other)
    {
        // TODO: Implement logic here
        return -1;
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

Rub*_*ben 11

Nullable<int>可能没有实现IComparable,但肯定int会.并且Nullable<T>总是框T(例如当你转换为界面时,例如IComparable,这是一个拳击转换).因此,对可空属性进行比较/排序应该不是问题.

int? value = 1;
IComparable comparable = value; // works; even implicitly
Run Code Online (Sandbox Code Playgroud)

因此,顶部样本的检查无法正常工作.试试这个:

Type interfaceType = prop.PropertyType.GetInterface("IComparable");
// Interface not found on the property's type. Maybe the property was nullable?
// For that to happen, it must be value type.
if (interfaceType == null && prop.PropertyType.IsValueType)
{
    Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
    // Nullable.GetUnderlyingType only returns a non-null value if the
    // supplied type was indeed a nullable type.
    if (underlyingType != null)
        interfaceType = underlyingType.GetInterface("IComparable");
}
if (interfaceType != null)
   // rest of sample
Run Code Online (Sandbox Code Playgroud)

还有一个补充:如果你想要null值也可以工作(字符串和可空类型),你可以试试这个重新实现SortCore(...):

protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
    IEnumerable<MyClass> query = base.Items;
    if (direction == ListSortDirection.Ascending)
        query = query.OrderBy( i => prop.GetValue(i) );
    else
        query = query.OrderByDescending( i => prop.GetValue(i) );
    int newIndex = 0;
    foreach (MyClass item in query)
    {
        this.Items[newIndex] = item;
        newIndex++;
    }
    this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
}
Run Code Online (Sandbox Code Playgroud)

没有必要IComparable直接寻找,只需让排序方法自己解决它.