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 ...
随机推荐
- MinGW编译wxWidgets中的问题及解决方法
其实网上wxWidgets编译相关的博文,都没写到关键点上,泛泛而谈——就写了执行几个命令,就万事大吉了! 维基百科上的这个页面讲解了编译中可能遇到的各种问题及解决办法.比较懒,不想翻译.wxWidg ...
- 如何批量转换 WordPress 文章分类
可能建博之初,分类设置过于详细,后来想重新整理并删除一些分类项目,比如删除分类A,并将其中的所有文章划归到分类B中,手动修改文章的分类过于麻烦,有木有什么方法可以批量移动文章到另一个分类中呢? 网上闲 ...
- laravel5.2 学习之服务提供者
契约接口:app\Contracts\LanguageContract.php <?php namespace App\Contracts; interface LanguageContract ...
- android Material Design:主题
<style name="MyTheme" parent="@android:style/android:Theme.Material"> < ...
- 复习URLHttpConnection方式GET,POST方式链接网络解析uri
xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- 03-树3 Tree Traversals Again
二叉树及其遍历 push为前序遍历序列,pop为中序遍历序列.将题目转化为已知前序.中序,求后序. 前序GLR 中序LGR 前序第一个为G,在中序中找到G,左边为左子树L,右边为右子树R. 将左右子树 ...
- 相比于汇编语言的准确性c语言延时精确度如何提升
只要合理的运用,C还是可以达到意想不到的效果.很多朋友抱怨C效率比汇编差了很多,其实如果对Keil C的编译原理有一个较深入的理解,是可以通过恰当的语法运用,让生成的C代码达到最优化.即使这看起来不大 ...
- 5.python的字符串
在前面提起过字符串这个词,现在就来学习什么是字符串. 首先,字符串是python内置的数据类型,其特点是用引号引起来,并且可以是使用单引号('字符串'),双引号("字符串"),三个 ...
- python Django 学习笔记(一)—— Django安装
注:本人python版本2.7.5 ,win7系统 安装Django https://www.djangoproject.com/download/ 官方下载Django-1.5.5.tar.gz 1 ...
- 微信支付开发h5发起支付再次签名,返回给h5前端
注意:参数区分大小写.