c#SortableBindingList无法对具有对象值的列进行排序

c#SortableBindingList无法对具有对象值的列进行排序,c#,datagridview,bindinglist,C#,Datagridview,Bindinglist,我有一个DataGridView组件,其中填充了SortableBindingList,可以在SQL Server SDK中找到Microsoft.SqlServer.Management.Controls。此类还用作XML序列化的根节点 列表的成员属于具有两个基本字段和一个对象字段的类型。包含基元值的列按预期进行排序。但是,对包含对象字段的列进行排序时,会引发以下异常: System.InvalidOperationException未处理 Message=无法比较数组中的两个元素。 Sour

我有一个
DataGridView
组件,其中填充了
SortableBindingList
,可以在SQL Server SDK中找到
Microsoft.SqlServer.Management.Controls
。此类还用作XML序列化的根节点

列表的成员属于具有两个基本字段和一个对象字段的类型。包含基元值的列按预期进行排序。但是,对包含对象字段的列进行排序时,会引发以下异常:

System.InvalidOperationException未处理
Message=无法比较数组中的两个元素。
Source=mscorlib
堆栈跟踪:
位于System.Collections.Generic.ArraySortHelper`1.Sort(T[]键,Int32索引,Int32长度,IComparer`1比较器)
在System.Array.Sort[T](T[]数组,Int32索引,Int32长度,IComparer`1比较器)
at System.Collections.Generic.List`1.Sort(比较`1比较)
位于Microsoft.SqlServer.Management.Controls.SortableBindingList`1.ApplySortCore(PropertyDescriptor属性,ListSortDirection方向)
位于System.ComponentModel.BindingList`1.System.ComponentModel.IBindingList.ApplySort(PropertyDescriptor属性,ListSortDirection方向)
位于System.Windows.Forms.BindingSource.ApplySort(PropertyDescriptor属性,ListSortDirection排序)
...
InnerException:System.ArgumentException
Message=至少一个对象必须实现IComparable。
Source=mscorlib
堆栈跟踪:
在System.Collections.Comparer.Compare处(对象a、对象b)
在Microsoft.SqlServer.Management.Controls.SortableBindingList`1.c\u DisplayClass1.b\u 0(t1,t2)
在System.Array.FunctorComparer`1.Compare处(tx,ty)
位于System.Collections.Generic.ArraySortHelper`1.SwapIfGreaterWithItems(T[]键,IComparer`1比较器,Int32 a,Int32 b)
位于System.Collections.Generic.ArraySortHelper`1.QuickSort(T[]键,左Int32,右Int32,IComparer`1比较器)
位于System.Collections.Generic.ArraySortHelper`1.Sort(T[]键,Int32索引,Int32长度,IComparer`1比较器)
从堆栈跟踪中可以很清楚地看出,正在比较的对象不是可比较的,只是它们是可比较的。或者至少他们应该这样。所讨论的班级如下:

使用系统;
使用System.Xml.Serialization;
名称空间myapp.xmlobjects{
[XmlType(“error_after”)]
公共类ErrorAfterObject:IComparable{
[XmlAttribute(“小时”)]
公共整数小时数{get;set;}
[xmldattribute(“分钟”)]
公共整数分钟数{get;set;}
public ErrorAfterObject(){}
公共ErrorAfterObject(整数小时,整数分钟){
这个.小时=小时;
这个。分钟=分钟;
}
公共重写字符串ToString(){
返回string.Format(“{0}hr{1}min”,this.Hours,this.Minutes);
}
公共整数比较(ErrorAfterObject其他){
返回(this.Hours*60+this.Minutes).比较(other.Hours*60+other.Minutes);
}
}
}
作为健全性检查,我在数据绑定后添加了以下调试代码:

Console.WriteLine(myGridView.Rows[0].Cells[2].ValueType.ToString());
正如我所期望的那样,它返回为
myapp.xmlobjects.ErrorAfterObject

有三个问题可能有助于解决这个问题:我的对象是否被类型化为不可编译的东西?是否可以准确检查正在比较的对象类型?我是否错过了
IComparable
实现中的某些内容


提前感谢。

事实证明,
IComparable
IComparable
不是一回事。将类定义替换为:

public class ErrorAfterObject : IComparable
与以下方法进行比较:

public int CompareTo(object other) {
    if(this.GetType() != other.GetType()) {
        return Comparer.Default.Compare(this, other);
    }
    return (this.Hours*60 + this.Minutes).CompareTo(
        ((ErrorAfterObject)other).Hours*60 + ((ErrorAfterObject)other).Minutes);
}
正如人们所期望的那样