Winform DataGridView扩展
- public class ObjectPropertyCompare<T> : IComparer<T>
- {
- private readonly PropertyDescriptor property;
- private readonly ListSortDirection direction;
- public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
- {
- this.property = property;
- this.direction = direction;
- }
- public int Compare(T x, T y)
- {
- object xValue = x.GetType().GetProperty(property.Name).GetValue(x, null);
- object yValue = y.GetType().GetProperty(property.Name).GetValue(y, null);
- int returnValue;
- if (xValue is IComparable)
- {
- returnValue = ((IComparable)xValue).CompareTo(yValue);
- }
- else if (xValue.Equals(yValue))
- {
- returnValue = ;
- }
- else
- {
- returnValue = xValue.ToString().CompareTo(yValue.ToString());
- }
- if (direction == ListSortDirection.Ascending)
- {
- return returnValue;
- }
- return returnValue * -;
- }
- }
- public class BindingCollection<T> : BindingList<T>
- {
- private bool isSorted;
- private PropertyDescriptor sortProperty;
- private ListSortDirection sortDirection;
- public BindingCollection()
- {
- }
- public BindingCollection(IList<T> list)
- : base(list)
- {
- }
- protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
- {
- var items = Items as List<T>;
- if (items != null)
- {
- var pc = new ObjectPropertyCompare<T>(property, direction);
- try
- {
- items.Sort(pc);
- isSorted = true;
- }
- catch (Exception)
- {
- isSorted = false;
- }
- }
- else
- {
- isSorted = false;
- }
- sortProperty = property;
- sortDirection = direction;
- OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -));
- }
- protected override bool IsSortedCore
- {
- get { return isSorted; }
- }
- protected override bool SupportsSortingCore
- {
- get { return true; }
- }
- protected override ListSortDirection SortDirectionCore
- {
- get { return sortDirection; }
- }
- protected override PropertyDescriptor SortPropertyCore
- {
- get { return sortProperty; }
- }
- protected override void RemoveSortCore()
- {
- isSorted = false;
- OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -));
- }
- }
Winform DataGridView扩展的更多相关文章
- C#实现WinForm DataGridView控件支持叠加数据绑定
我们都知道WinForm DataGridView控件支持数据绑定,使用方法很简单,只需将DataSource属性指定到相应的数据源即可,但需注意数据源必须支持IListSource类型,这里说的是支 ...
- 关于C# Winform DataGridView 设置DefaultCellStyle无效的原因与解决方案
上周在开发Winform 项目中,我曾遇到一个看似简单,但一直都没有解决的问题,那就是:设置winform DataGridView控件的行DefaultCellStyle,但却没有任何变化,我也曾求 ...
- C#Winform使用扩展方法自定义富文本框(RichTextBox)字体颜色
在利用C#开发Winform应用程序的时候,我们有可能使用RichTextBox来实现实时显示应用程序日志的功能,日志又分为:一般消息,警告提示 和错误等类别.为了更好地区分不同类型的日志,我们需要使 ...
- WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...
- C# winform DataGridView 常见属性
C# winform DataGridView 属性说明① 取得或者修改当前单元格的内容 ② 设定单元格只读 ③ 不显示最下面的新行 ④ 判断新增行 ⑤ 行的用户删除操作的自定义 ⑥ 行.列的隐藏和删 ...
- Winform Datagridview 单元格html格式化支持富文本
Winform Datagridview 单元格html格式化支持富文本 示例: 源码:https://github.com/OceanAirdrop/DataGridViewHTMLCell 参考: ...
- [WinForm]DataGridView列头右键菜单
[WinForm]DataGridView列头右键菜单 前言 继续"不误正业" - - #,记录一下.有时候有这样的需求:DataGridView的列头菜单可以选择具体显示哪些列, ...
- winform datagridview 绑定泛型集合变得不支持排序的解决方案
原文:winform datagridview 绑定泛型集合变得不支持排序的解决方案 案例: 环境:Winform程序 控件:Datagridview 现象:Datagridview控件绑定到List ...
- [转]WinForm DataGridView 绑定泛型List(List<T>)/ArrayList不显示的原因和解决
背景:无意间遇到了一个不大不小的问题,希望对一些遇到的人有所帮助! 一.问题 WinForm DataGridView 绑定泛型List (List<T>)/ArrayList不显示,UI ...
随机推荐
- Cassandra 计数器counter类型和它的限制
文档基础 Cassandra 2.* CQL3.1 翻译多数来自这个文档 更新于2015年9月7日,最后有参考资料 作为Cassandra的一种类型之一,Counter类型算是限制最多的一个.Coun ...
- phpStorm使用技巧及快捷键
下面是PhpStorm的注册码.Key,其license由用户名和License值组成. User name: EMBRACE License key: ===== LICENSE BEGIN === ...
- Ghost命令使用方法
我们知道,一般使用Ghost时,都是在DOS提示符后先键入"Ghost",然后再进入Ghost的图形界面操作:那么可不可以让Ghost也只通过命令行的方式工作呢?答案是肯定的,在键 ...
- linux回环网卡驱动设计
回环网卡驱动 1.回环网卡和普通网卡的区别是他是虚拟的不是实际的物理网卡,它相当于把普通网卡的发送端和接收端短接在一起. 2.在内核源代码里的回环网卡程序(drivers/net/loopback.c ...
- 在EF的code frist下写稳健的权限管理系统:开篇(一)
环境:EF6.0.0.0+Autofac3.5.0.0+MVC4.0+pure6.0+Jquery IDE:vs2012,数据库:vs2008r2 搭建环境如下: 我给它取名字为cactus:仙人球, ...
- Jquery + echarts 使用
常规用法,就不细说了,按照官网一步步来. 本文主要解决问题(已参考网上其他文章): 1.把echarts给扩展到JQuery上,做到更方便调用. 2.多图共存 3.常见的X轴格式化,钻取时传业务实体I ...
- wpf 获取DataGrid某一个单元格,设置此单元格ToolTip内容和背景颜色
public void GetCell() { for (int i = 0; i < this.datagrid1.Items.Count; i++) ...
- XAML(3) - 附带属性
WPF元素也可以从父元素中获得特性.例如,如果Button元素为了Canvas元素中,按钮的Top和Lef属性把父元素的名称作为前缀.这种属性成为附带属性: <Canvas> <Bu ...
- MongoDB分片简单实例
分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据也足以提供可接受的读写吞吐量.这时,我 ...
- mysql索引合并:一条sql可以使用多个索引
前言 mysql的索引合并并不是什么新特性.早在mysql5.0版本就已经实现.之所以还写这篇博文,是因为好多人还一直保留着一条sql语句只能使用一个索引的错误观念.本文会通过一些示例来说明如何使用索 ...