winform datagridview 绑定泛型集合变得不支持排序的解决方案
原文: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 绑定泛型集合变得不支持排序的解决方案的更多相关文章
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...
- [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...
- DataGridView绑定泛型List时,利用BindingList来实现增删查改
DataGridView绑定泛型List时,利用BindingList来实现增删查改 一. DataGridView绑定泛型List的种种 1.DataGridView数据绑定对比(DataTa ...
- [WinForm] DataGridView 绑定 DT && ComboBox 列绑定 Dict
一 需求介绍 一般像枚举类型的数据,我们在数据库里存储着诸如(1.2.3.4-)或者("001"."002"."003"-)此类,但是界面 ...
- C#中DataTable与泛型集合互转(支持泛型集合中对象包含枚举)
最近在做WCF,因为是内部接口,很多地方直接用的弱类型返回(DataSet),这其实是一种非常不好的方式,最近将项目做了修改,将所有接口返回值都修改成强类型,这样可以减少很多与客户端开发人员的沟通,结 ...
- DataGridview 绑定泛型List<T>
.DataGridView数据绑定对比(DataTable与泛型List): 当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...
- [转]DataGridView绑定泛型List的种种
1.DataGridView数据绑定对比(DataTable与泛型List):当DataGridView的DataSource是DataTable的时候,DataTable的数据改变时,DataGri ...
- DataGridView 绑定List集合后实现自定义排序
这里只贴主要代码,dataList是已添加数据的全局变量,绑定数据源 datagridview1.DataSource = dataList,以下是核心代码. 实现点击列表头实现自定义排序 priva ...
- C# winform DataGridView 绑定数据的的几种方法
1.用DataSet和DataTable为DataGridView提供数据源 String strConn = "Data Source=.;Initial Catalog=His;User ...
随机推荐
- 对redis高并发测试的研究
以下引用大神的: 测试项目: https://github.com/14251104246/redis-demo.git 准备 使用docker-compose命令启动redis服务器(可以用其他方式 ...
- Linux GDB 程序调试工具使用详解
转自 http://www.codeceo.com/article/linux-gdb-tools.html 整理的挺全的 GDB概述 GDB是GNU开源组织发布的一个强大的UNIX下的程序调试 ...
- input 禁止删除部分文字
用label和所需的input链接,label部分就是禁止删除的部分.<input type="text" name="city" value=" ...
- git 部署服务
git 知识 服务器知识 1.在本地完成代码的编写, 然后通过 git 管理版本. 在编码完成后 git push 到 git 云端(github 或者 码云 及其他). 2.在服务器端安装 git ...
- hihocoder1286 : 子矩阵求和
http://hihocoder.com/problemset/problem/1286 题解 NB分析题. 首先我们令\(s[i][j]\)表示以\((i,j)\)为左上角的矩形的权值和. 因为\( ...
- 原生javascript兼容性总结
1.addEventListener() :方法用于向指定元素添加事件句柄.// Internet Explorer 8 及更早IE版本不支持,Opera 7.0 及 Opera 更早版本也不支持. ...
- 使用eclipse导入新项目时中文出现乱码问题
有时候在github上看到别人不错的项目想要拉下来学习学习的时候,总会出现这样的情况,实在蛋疼. 一般出现这种问题,会有三个地方需要改动: 在项目上右键选择 properties 将 text fil ...
- 关于【C++项目:无法解析的外部符号】
1,基本原因,[链接器]->[附加库目录]没有填写相关库的路径.或没有在[链接器]->[输入]->[附加依赖项]中填写相关库的名称 2,高级原因:如果不是1的原因,那就有可能是平台与 ...
- Linux驱动开发4——并发和竞态
Linux系统处于一个高并发的运行环境,不管是系统调用还是中断都要求可重入,但是有一些系统资源处于临界区,因此,必须保证临界区资源访问的原子性. 对于临界区资源被占用时,发起访问的进程,有三种处理方法 ...
- ora600
4节点RAC:版本oracle11.2.0.4 22:20——23:40发生ora600 alert日志: Errors in file /u01/app/oracle/diag/rdbms/orcl ...