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 ...
随机推荐
- linux常用命令--ps、netstat、find
一.ps 1.1ps命令简介 Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那 ...
- 四、MongoDB的查询
一.MongoDB的下载.安装与部署 二.MongoDB的基础知识简介 三.MongoDB的创建.更新和删除 db.blogs.insert([ { "author": " ...
- PHP判断用户所在国家并跳转对应的目录
<?php // 淘宝API查询国家代码 $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".get_client ...
- 批量删除的js代码
<script type="text/javascript"> function seltAll(){ var chckBoxSign = document.getEl ...
- C# 基础 计算平均值的方法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- web.config的奇淫巧技
<connectionStrings configSource="db.config"/> 外部文件db.config: <connectionStrings&g ...
- Python Ogre Blender(转载)
http://www.cppblog.com/Charlib/archive/2010/05/31/python_ogre_blender_1.html PyOgre入门以及如何使用Blender制作 ...
- 任务管理界面添加显示RAM信息
显示RAM信息的核心代码是大蛋的,我只不过是整理下教程而已! 大蛋应该不会介意的吧,首先你需要apktool和SystemUI.apk,framework-res.apk 然后开始加载框架和反编译.. ...
- 启动FM预算基金管理模块后,0L总账消失的解决办法
只要用SE38运行一下:FMGL_CHANGE_APPL_IN_LEDGER 问题就解决了.
- 无法在 Android 模拟器上访问本机的Web服务的解决办法
我在本地跑了一个 Tomcat ,我想在 Android 模拟器中直接通过下面的 url 地址访问 Tomcat 上的服务 http://192.168.0.20:8080/getweather 但是 ...