开发winform中,平时经常用到数据列表,我们大多选用datagridview,但是此控件本身没有排序的功能。参阅网上资料。留下标记,以后备用。

datagridview的数据显示一般是通过数据绑定来实现,

即:this.datagridview.DataSource=this.bindingSrc;

this.bindingSrc.DataSource=this.Model;

这种形式就完成了,数据的显示过程。但是要实现点击datagridview实现排序的功能,需要实现对数据源排序的功能。具体代码如下:

public 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 = 0; i < this.Count; i++)
{
if (Equals(prop.GetValue(this[i]), key)) return i;
}
return -1;
} 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 = 0;
if (SortPropertyCore != null)
{
ret = CompareValue(SortPropertyCore.GetValue(o1), SortPropertyCore.GetValue(o2), SortPropertyCore.PropertyType);
}
if (ret == 0 && DefaultSortItem != null)
{
PropertyInfo property = typeof(T).GetProperty(DefaultSortItem, BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.IgnoreCase, null, null, new Type[0], 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 ? 0 : -1;
else if (o2 == null) return 1;
else if (type.IsPrimitive || type.IsEnum) 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());
}
}

实现此类后,就可以通过 this.bindingSrc.DataSource=new SortableBindingList<CnsDetailReport>(this.Model); 实现点击列标题排序的目的了。

datagridview 点击列标题排序的更多相关文章

  1. winform dataGridView 点击列标题排序

    winform手动绑定数据后,点击列标题不能实现自动排序,苦苦寻找方法,发现下面的是可行的. //建立DataTable将当前dataGridView中的数据读进DataTable中 public D ...

  2. WPF中的DataGridTemplateColumn实现点击列标题排序

    在DataGrid中使用模板列时,默认功能中对点击列标题是不对列值进行排序的,要排序就需要添加以下两个属性: 1.CanUserSort="True" 2.SortMemberPa ...

  3. easyUI中点击datagrid列标题排序

    easyUI中点击datagrid的排序有两种,一种是本地的,一种是服务器的.本地的只能排序当前页,而服务器的可以对全部页进行排序.这里主要是分享下服务器排序. 1.为datagrid添加属性remo ...

  4. [WPF]ListView点击列头排序功能实现

    [转]   [WPF]ListView点击列头排序功能实现 这是一个非常常见的功能,要求也很简单,在Column Header上显示一个小三角表示表示现在是在哪个Header上的正序还是倒序就可以了. ...

  5. MFC listcontrol 分列 添加行数据 点击列头排序

    适用于 对话框程序 1.在工具箱中拖出 ListControl,然后右键-属性,view-Report 让你的ListControl变成这幅模样! 2.添加ListControl控件的control类 ...

  6. 单击dbgrid列标题排序 升降序

    delphi中如何通过单击列标题进行升降排序, 在dbgrid的ontitleclick事件里添加这样的事件处理 procedure TForm3.DBGrid1TitleClick(Column: ...

  7. zk listbox 点击列标题实现排序功能

    前台(test.zul): <?page title="测试" contentType="text/html;charset=UTF-8"?> &l ...

  8. 测试table数据 winfrom datagridview 点击标头数字排序的时候table 列类型要为数字类型

    public DataTable GenerateData(int NoOfRecord){DataTable tbl = new DataTable();tbl.Columns.Add(new Da ...

  9. C++ 简单实现MFC ListControl 点击列头排序

    说明: SetItemData可以为每一行绑定一个DWORD类型的变量.用GetItemData可以获得这个变量.举个例子,假设CListCtrl中你需要显示某个数据表中的记录,该表有个流水号主键ID ...

随机推荐

  1. String类详解,StringBuffer

    先说一下String类的equals()方法. 下面我们先看一段代码: 这段代码输出的结果为: ture true -------------- false 咋看之下貌似Object类比较特别,那么我 ...

  2. EMVTag系列1《数据分组》

    数据分组的设计在个人化过程中承担着重要的作用.数据分组标识符(DGI)是两字节十六进制数.数据分组标识的第一个字节等于'01'到'1E',表明数据存储的SFI.第二个字节表明SFI记录的记录编号.其他 ...

  3. 机器学习相关——协同过滤

    在现今的推荐技术和算法中,最被大家广泛认可和采用的就是基于协同过滤的推荐方法.本文将带你深入了解协同过滤的秘密.下面直接进入正题 1 什么是协同过滤 协同过滤是利用集体智慧的一个典型方法.要理解什么是 ...

  4. android开发系列之消息机制

    最近接触到一个比较有挑战性的项目,我发现里面使用大量的消息机制,现在这篇博客我想具体分析一下:android里面的消息到底是什么东西,消息机制到底有什么好处呢? 其实说到android消息机制,我们可 ...

  5. windows phone listbox虚拟化(下)

    之前写过一篇关于listbox虚拟化的文章,那里采用的方法都是自己早期研究的一些思路,然后发现当数据很大的时候,其实性能效果还是不太理想,下面让我们来仔细想一想到底是基于什么原因,我们回去破坏默认的虚 ...

  6. ZLG_GUI配置与函数介绍

    http://www.docin.com/p-825479457.html ZLG_GUI配置与函数介绍

  7. React + Reflux

    React + Reflux 渲染性能优化原理   作者:ManfredHu 链接:http://www.manfredhu.com/2016/11/08/23-reactRenderingPrinc ...

  8. 字符串流sstream[part3/使用字符串流进行安全的类型转换]

    参考: http://blog.163.com/zhuandi_h/blog/static/180270288201291710222975/ http://www.cnblogs.com/games ...

  9. HTML5中的Canvas精品教程

    http://javascript.ruanyifeng.com/htmlapi/canvas.html

  10. android 开发 解码gif图片,获取每帧bitmap

    环境:android 4.3  (注意对于android4.4版本解码出来不正确,除了第一帧正确外,其余的都是显示不同的地方)  通用版本见: android 开发对gif解码(适配android 4 ...