C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题
帮助类:
public class SortBindingHelper<T> : BindingList<T>
{
private bool isSortedCore = true;
private ListSortDirection sortDirectionCore = ListSortDirection.Ascending;
private PropertyDescriptor sortPropertyCore = null;
private string defaultSortItem; public SortBindingHelper() : base() { } public SortBindingHelper(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());
}
}
使用方式:
private void NewMethod()
{
List<StudentInfo> list = new List<StudentInfo>();
for (int i = 1; i <= 50; i++)
{
StudentInfo stu = new StudentInfo();
stu.NameA = "学生aaaaaaaa" + i;
string str = i.ToString().PadLeft(3, '0');//001
stu.Age = str;
list.Add(stu);
} SortBindingHelper<StudentInfo> temp = new SortBindingHelper<StudentInfo>();
foreach (StudentInfo item in list)
{
temp.Add(item);
} this.dgvList.DataSource = temp;
}
C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题的更多相关文章
- ComboBox控件绑定数据源后,添加'请选择'或'全部'
ComboBox控件绑定数据源后,添加'请选择'或'全部' 当使用ComboBox控件绑定数据源之后,通过Items 属性添加的数据是无效的,此时如果要在所有选项前添加 选项 ,则需要考虑从数据源下手 ...
- DataGridView绑定数据源后添加行
本文链接:https://blog.csdn.net/u012386475/article/details/88639799 在已经绑定数据源时,无法以Add的方式方式添加行,会报错 解决方法一: D ...
- DropDownList绑定数据源后,要插入项的处理
private void BindDivision() { DivisionService divisionService = new DivisionService(); var divisions ...
- Winfrom 中 ComboBox 绑定数据后设置选定项问题
在为 ComboBox 当定数据的时候,如果遇到界面显示需要用文本,而获取选定项的值时需要用数字,我们就很习惯使用 DataSource 来进行绑定. 例如以下代码: List<TextVal ...
- 章鱼哥出品—VB.NET DataGridView绑定数据源 "与货币管理器的位置关联的行不能设置为不可见" 问题的解决
DtaGridView绑定数据源后.假设想让数据条件显示的话,直接使用 My_Row.Visible = False就会出错.错误类型是 "与货币管理器的位置关联的行不能设置为不可见&qu ...
- DataGridView绑定数据源
给DataGridView绑定数据源比較简单,方法主要有两种: 1.直接在控件属性中绑定数据源,这样的方法最简单,但它是直接连接数据库的,这样就和传DataTable的后果差点儿相同了,所以还是尽量避 ...
- Winfrom中ListBox绑定List数据源更新问题
Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...
- C# DataGridView绑定数据源的几种常见方式
开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...
- DataGridView绑定数据源的几种方式
使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...
随机推荐
- Python扫描器-常用库-Request
1.常用库-Request 1.1. 介绍 #安装:pip3 install requests #各种请求方式:常用的就是requests.get()和requests.post() >> ...
- 【c# 学习笔记】显示接口实现方法
在接口 一张中,使用了隐式的接口实现方式,即在实现代码中没有制定实现那个接口中的CompareTo方法.相应地,自然就有显式的 接口实现方式,它指的是在实现过程中,明确指出实现哪一个接口中的哪一个方 ...
- numpy 构建深度神经网络来识别图片中是否有猫
目录 1 构建数据 2 随机初始化数据 3 前向传播 4 计算损失 5 反向传播 6 更新参数 7 构建模型 8 预测 9 开始训练 10 进行预测 11 以图片的形式展示预测后的结果 搭建简单神经网 ...
- 2019年11月27日 Linux所学知识 总结
查看网络信息和网络状态 nmcli connection show 使用con-name参数指定公司使用的网络会话名称company,然后依次用ifname参数指定本机的网卡名称. 用autoconn ...
- Mac brew命令踩坑
brew是mac上的在线包管理软件,相当于linux apt以及yum包管理工具 1.取消brew的自动更新(https://blog.csdn.net/yf9595/article/details/ ...
- springboot集成webSocket能启动,但是打包不了war
1.pom.xml少packing元素 https://www.cnblogs.com/zeussbook/p/10790339.html 2.SpringBoot项目中增加了WebSocket功能无 ...
- 权限管理ranger
为超级管理员airflow赋权: 在ranger 中的hive中加入root policy, url policy 在hdfs中加入/ 的poclicy 你如果需要自动ldap同步,时间1小时,ran ...
- 提车应该检查哪?4S店都怕你检查这4个“雷区”,别等后悔才知道
https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9381645601643691163% ...
- 获取Android手机日志
方式一:使用USB连接 1.在手机上启用USB调试2.在终端输入adb devices 3.获取日志 只连接一个设备:1)清除已缓存日志:adb logcat -c2)获取日志并保存到本地:adb l ...
- js实现放烟花效果——点击处会从下向上升起烟花
一个box中,点击其中的任意位置,会有烟花从正下方升起到点击处,并燃放出一圈圆形的烟花.代码如下: 首先是布局以及样式: <style> .box{ width: 1100px; heig ...