C# Int32?与我相比
我有一个DataGridView,它的数据源是BindingList。MyObj有几个可为空的属性(比如int?和DateTime?),我想对绑定列表进行排序,这样当用户单击列标题时,DataGridView可以对列进行排序 经过一番挖掘,我找到了这个问题的答案,并遵循了答案() 我无法使该解决方案适用于可空类型,因为它们没有实现IComparable。即使对于实现类似于字符串的IComparable的类,当字符串具有空值时,ApplySortCore(…)也会失败 有解决办法吗?或者我必须为“Int32”实现一个包装器类吗 乙二醇C# Int32?与我相比,c#,.net,C#,.net,我有一个DataGridView,它的数据源是BindingList。MyObj有几个可为空的属性(比如int?和DateTime?),我想对绑定列表进行排序,这样当用户单击列标题时,DataGridView可以对列进行排序 经过一番挖掘,我找到了这个问题的答案,并遵循了答案() 我无法使该解决方案适用于可空类型,因为它们没有实现IComparable。即使对于实现类似于字符串的IComparable的类,当字符串具有空值时,ApplySortCore(…)也会失败 有解决办法吗?或者我必须为“
公共类Int32Compariable:iCompariable
{
公共int?值{get;set;}
#区域i可比较成员
公共整数比较(对象其他)
{
//TODO:在这里实现逻辑
返回-1;
}
#端区
}
在比较可为空的类型时,您可以这样做吗
Int32? val1 = 30;
Int32 val2 = 50;
Int32 result = (val1 as IComparable).CompareTo(val2);
Nullable
可能无法实现IComparable
,但肯定int
可以实现。和Nullable
始终将框设置为T
(例如,当您强制转换到接口时,例如IComparable
,这是一种装箱转换)。因此,对可空属性进行比较/排序应该不是问题
int? value = 1;
IComparable comparable = value; // works; even implicitly
因此,顶部样本中的检查不正确。试试这个:
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
还有一个补充:如果您希望空值也能工作(字符串和可空类型),您可以尝试重新实现SortCore(…)
:
protected override void ApplySortCore(PropertyDescriptor属性、ListSortDirection方向)
{
IEnumerable query=base.Items;
if(方向==ListSortDirection.Ascending)
query=query.OrderBy(i=>prop.GetValue(i));
其他的
query=query.OrderByDescending(i=>prop.GetValue(i));
int newIndex=0;
foreach(查询中的MyClass项)
{
此.Items[newIndex]=项;
newIndex++;
}
this.OnListChanged(新的ListChangedEventArgs(ListChangedType.Reset,-1));
}
无需直接查找
IComparable
,只需让排序方法自己进行排序即可。非常感谢。我还需要设置_sortPropertyValue=prop_sortDirectionValue=方向_isSortedValue=true;为了让代码正常工作。非常感谢:)Re“对可空属性进行比较/排序应该不会有问题。”-除非该属性具有值null
,否则在这种情况下,您将获得运行时异常。(我假设-我没有尝试过。)@ToolmakerSteve、OrderBy
和OrderByDescending
不介意属性何时返回null
。它使用的默认比较器将对顶部的null
值进行排序。只有当列表本身包含null
条目时,才会出现问题。
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
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));
}