INT32?与IComparable的 [英] Int32? with IComparable
问题描述
我有一个DataGridView的数据源是一个的BindingList。 MyObj中有一些可为空的属性(如int?和日期?)我要实现排序,以我的绑定列表,所以在DataGridView可以对列进行排序,当用户单击列标题。
经过一番周围挖,我发现,跟着这个问题的答案(<一href="http://stackoverflow.com/questions/280948/datagridview-column-sorting-with-business-objects">http://stackoverflow.com/questions/280948/datagridview-column-sorting-with-business-objects).
我不能让这个解决方案工作的可空类型,因为他们没有实现IComparable。即使对于实现IComparable如String类,ApplySortCore(...)失败时的字符串中有一个空值。
有一个解决方案?还是我必须实现一个包装类的Int32? ?
例如
公共类Int32Comparable:IComparable的
{
公众诠释?值{获得;组; }
#地区IComparable的&LT;诠释&GT;会员
公众诠释的CompareTo(对象等)
{
// TODO:在这里实现逻辑
返回-1;
}
#endregion
}
可空&LT; INT&GT;
可能无法实施 IComparable的
,但肯定 INT
一样。而可空&LT; T&GT;
总是拖曳 T
(例如当你施放一个接口,如 IComparable的
,这是一个装箱转换)。所以比较/排序可为空的属性不应该是一个问题。
诠释?值= 1;
IComparable的可比=价值; // 作品;甚至含蓄
所以,在顶部的检查样本中不工作的权利。试试这个:
键入interfaceType = prop.PropertyType.GetInterface(IComparable的);
//接口未对物业的类型中。也许财产是可为空?
//要做到这一点,它必须是值类型。
如果(interfaceType == NULL和放大器;&安培; prop.PropertyType.IsValueType)
{
键入underlyingType = Nullable.GetUnderlyingType(prop.PropertyType);
// Nullable.GetUnderlyingType只返回一个非空值,如果
//提供的类型的确是一个可空类型。
如果(underlyingType!= NULL)
interfaceType = underlyingType.GetInterface(IComparable的);
}
如果(interfaceType!= NULL)
//样品休息
还有一个另外:如果你想空值的工作以及(字符串和可空类型),你可以试试这个重新实施 SortCore(...)的
:
保护覆盖无效ApplySortCore(PropertyDescriptor与道具,ListSortDirection方向)
{
IEnumerable的&LT; MyClass的&GT;查询= base.Items;
如果(方向== ListSortDirection.Ascending)
查询= query.OrderBy(ⅰ=&GT; prop.GetValue(ⅰ));
其他
查询= query.OrderByDescending(ⅰ=&GT; prop.GetValue(ⅰ));
INT newIndex = 0;
的foreach(查询MyClass的项目)
{
this.Items [newIndex] =项目;
newIndex ++;
}
this.OnListChanged(新ListChangedEventArgs(ListChangedType.Reset,-1));
}
有没有必要去寻找 IComparable的
直接,只是让排序方法自己梳理出来。
I have a DataGridView whose datasource is a BindingList. MyObj has a few nullable properties (like int? and DateTime?) I want to implement sorting to my binding list, so the DataGridView can sort the column when the user clicks on the column header.
After some digging around, I found and followed the answer of this question (http://stackoverflow.com/questions/280948/datagridview-column-sorting-with-business-objects).
I can't get that solution to work for Nullable types because they don't implement IComparable. Even for classes that implement IComparable like String, ApplySortCore(...) fails when the String has a null value.
Is there a solution for this? Or do I have to implement a wrapper class for "Int32?" ?
eg
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
}
Nullable<int>
may not implement IComparable
, but surely int
does. And Nullable<T>
always boxes to T
(for instance when you cast to an interface, such as IComparable
, which is a boxing conversion). So comparing/sorting on nullable properties should not be a problem.
int? value = 1;
IComparable comparable = value; // works; even implicitly
So, the check in the sample at the top does not work right. Try this:
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
One more addition: if you want null values to work as well (both string and nullable types), you could try this re-implementation of 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));
}
There's no need to look for IComparable
directly, just let the sorting methods themselves sort it out.
这篇关于INT32?与IComparable的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!