原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案

案例:

  环境:Winform程序

  控件:Datagridview

  现象:Datagridview控件绑定到List<T>泛型数据上不支持排序

     Datagridview控件绑定到DataTable上可以支持排序

  结论:泛型会使Datagridview失去排序特性

  解决:实现BindingList<T>接口

  实现代码:

  

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text; namespace HOET.Plugins.Orders.Model
{
/// <summary>
/// 泛型会失去DataTable特性,DataGridView绑定List<T>后不支持排序
/// </summary>
/// <typeparam name="T"></typeparam>
class SortableBindingList<T> : BindingList<T>
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem; public SortableBindingList() : base() { } public SortableBindingList(IList<T> list) : base(list) { } protected override bool SupportsSortingCore
{
get { return true; }
} protected override bool SupportsSearchingCore
{
get { return true; }
} protected override bool IsSortedCore
{
get { return isSortedCore; }
} protected override ListSortDirection SortDirectionCore
{
get { return sortDirectionCore; }
} protected override PropertyDescriptor SortPropertyCore
{
get { return sortPropertyCore; }
} protected override int FindCore(PropertyDescriptor prop, object key)
{
for(int i = ; i < this.Count; i++)
{
if(Equals(prop.GetValue(this[i]),key))
{
return i;
}
} return -;
} protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
{
isSortedCore = true;
sortPropertyCore = prop;
sortDirectionCore = direction;
Sort();
} protected override void RemoveSortCore()
{
if(isSortedCore)
{
isSortedCore = false;
sortPropertyCore = null;
sortDirectionCore = ListSortDirection.Ascending;
Sort();
}
} public string DefaultSortItem
{
get
{
return defaultSortItem;
}
set
{
if(defaultSortItem != value)
{
defaultSortItem = value;
Sort();
}
}
} private void Sort()
{
List<T> list = this.Items as List<T>;
list.Sort(CompareCore);
ResetBindings();
} private int CompareCore(T o1, T o2)
{
int ret = ;
if(SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if(ret == && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[], null);
if(property != null)
{
ret = CompareValue(property.GetValue(o1, null), property.GetValue(o2, null), property.PropertyType);
}
}
if(SortDirectionCore == ListSortDirection.Descending)
{
ret = -ret;
} return ret;
} private static int CompareValue(object o1, object o2, Type type)
{
if(o1 == null)
{
return o2 == null ? : -;
}
else if(o2 == null)
{
return ;
}
else if(type == typeof(char))
{
return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
else if (type.IsEnum ||type.IsPrimitive)
{
return Convert.ToDouble(o1).CompareTo(Convert.ToDouble(o2));
}
else if(type == typeof(DateTime))
{
return Convert.ToDateTime(o1).CompareTo(o2);
}
else
{
return String.Compare(o1.ToString().Trim(), o2.ToString().Trim());
}
}
}
}

  

winform datagridview 绑定泛型集合变得不支持排序的解决方案的更多相关文章

  1. WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  2. [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决

    背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...

  3. DataGridView绑定泛型List时,利用BindingList来实现增删查改

    DataGridView绑定泛型List时,利用BindingList来实现增删查改  一.   DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...

  4. [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict

    一  需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...

  5. C#中DataTable与泛型集合互转(支持泛型集合中对象包含枚举)

    最近在做WCF,因为是内部接口,很多地方直接用的弱类型返回(DataSet),这其实是一种非常不好的方式,最近将项目做了修改,将所有接口返回值都修改成强类型,这样可以减少很多与客户端开发人员的沟通,结 ...

  6. DataGridview 绑定泛型List<T>

    .DataGridView数据绑定对比(DataTable与泛型List): 当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...

  7. [转]DataGridView绑定泛型List的种种

    1.DataGridView数据绑定对比(DataTable与泛型List):当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...

  8. DataGridView 绑定List集合后实现自定义排序

    这里只贴主要代码,dataList是已添加数据的全局变量,绑定数据源 datagridview1.DataSource = dataList,以下是核心代码. 实现点击列表头实现自定义排序 priva ...

  9. C# winform DataGridView 绑定数据的的几种方法

    1.用DataSet和DataTable为DataGridView提供数据源 String strConn = "Data Source=.;Initial Catalog=His;User ...

随机推荐

  1. v-if 和 v-show

    关于条件渲染 所谓条件渲染,就是根据不同的条件,使用不同的模板来生成 html. 在 Vue.js 中,使用 v-if 和 v-show 指令来控制条件渲染. 区别 v-show 会在app初始化的时 ...

  2. CSS——相对定位、绝对定位、固定定位

    相对定位: position:relative 当元素被设置相对定位或是绝对定位后,将自动产生层叠,他们的层叠级别自然的高于文本流,除非设置其z-index值为负值. 并且我们发现当相对定位元素进行位 ...

  3. shell练习--PAT题目1002:写出这个数(失败案例)

    读入一个正整数 n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式: 每个测试输入包含 1 个测试用例,即给出自然数 n 的值.这里保证 n 小于 1. 输出格式: 在一行内输出 n 的 ...

  4. Java 内存屏障

    内存屏障(Memory Barrier,或有时叫做内存栅栏,Memory Fence)是一种CPU指令,用于控制特定条件下的重排序和内存可见性问题.Java编译器也会根据内存屏障的规则禁止重排序. 内 ...

  5. SpringCloud 教程 (五) 断路器监控(Hystrix Dashboard)

    一.Hystrix Dashboard简介 在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型.断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标.Hystrix ...

  6. [CSP-S模拟测试]:array(单调栈)

    题目描述 在放完棋子之后,$dirty$又开始了新的游戏. 现在他拥有一个长为$n$的数组$A$,他定义第$i$个位置的分值为$i−k+1$,其中$k$需要满足: 对于任意满足$k\leqslant ...

  7. hashcode native

    hashcode Java中的hashCode方法就是根据一定的规则将与对象相关的信息(比如对象的存储地址,对象的字段等)映射成一个数值,这个数值称作为散列值. 在设计hashCode方法和equal ...

  8. docker 在centos上的安装实践

    使用yum安装docker yum -y install docker-io [root@localhost goblin]# yum -y install docker-io Loaded plug ...

  9. Linux内核调试方法总结之bugreport

    bugreport [用途]Android性能分析工具,bugreport记录了Android启动过程日志,启动后的系统状态,包括进程列表.内存信息.VM信息等 [使用方法] Adb bugrepor ...

  10. Linux内核调试方法总结之Kprobes

    Kprobes [用途][参考kernel/Documentation/kprobes.txt帮助文档] Kprobes是一个轻量级内核调试工具,同时又是其他一些更高级的内核调试工具(如perf和sy ...