帮助类:

 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绑定数据源后--解决排序问题的更多相关文章

  1. ComboBox控件绑定数据源后,添加'请选择'或'全部'

    ComboBox控件绑定数据源后,添加'请选择'或'全部' 当使用ComboBox控件绑定数据源之后,通过Items 属性添加的数据是无效的,此时如果要在所有选项前添加 选项 ,则需要考虑从数据源下手 ...

  2. DataGridView绑定数据源后添加行

    本文链接:https://blog.csdn.net/u012386475/article/details/88639799 在已经绑定数据源时,无法以Add的方式方式添加行,会报错 解决方法一: D ...

  3. DropDownList绑定数据源后,要插入项的处理

    private void BindDivision() { DivisionService divisionService = new DivisionService(); var divisions ...

  4. Winfrom 中 ComboBox 绑定数据后设置选定项问题

    在为 ComboBox 当定数据的时候,如果遇到界面显示需要用文本,而获取选定项的值时需要用数字,我们就很习惯使用 DataSource  来进行绑定. 例如以下代码: List<TextVal ...

  5. 章鱼哥出品—VB.NET DataGridView绑定数据源 &quot;与货币管理器的位置关联的行不能设置为不可见&quot; 问题的解决

    DtaGridView绑定数据源后.假设想让数据条件显示的话,直接使用  My_Row.Visible = False就会出错.错误类型是 "与货币管理器的位置关联的行不能设置为不可见&qu ...

  6. DataGridView绑定数据源

    给DataGridView绑定数据源比較简单,方法主要有两种: 1.直接在控件属性中绑定数据源,这样的方法最简单,但它是直接连接数据库的,这样就和传DataTable的后果差点儿相同了,所以还是尽量避 ...

  7. Winfrom中ListBox绑定List数据源更新问题

    Winfrom中ListBox绑定List数据源更新问题 摘自:http://xiaocai.info/2010/09/winform-listbox-datasource-update/ Winfr ...

  8. C# DataGridView绑定数据源的几种常见方式

    开始以前,先认识一下WinForm控件数据绑定的两种形式,简单数据绑定和复杂数据绑定. 1. 简单的数据绑定 例1 using (SqlConnection conn = new SqlConnect ...

  9. DataGridView绑定数据源的几种方式

    使用DataGridView控件,可以显示和编辑来自多种不同类型的数据源的表格数据. 将数据绑定到DataGridView控件非常简单和直观,在大多数情况下,只需设置DataSource属性即可.在绑 ...

随机推荐

  1. iOS使用UIImageView展现网络图片(转载)

    在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下: [cpp] view plaincopy   - (void)viewDidLoad { [sup ...

  2. Docker 镜像小结---操作指令介绍(七)

    目录 一.搜索镜像 二.下载镜像 三.查看本地镜像 四.显示镜像构建历史 五.删除镜像 六.镜像创建 七.上传镜像 八.给镜像打 tag 九.存出和载入镜像 一.搜索镜像 很多情况下我们可能需要下载某 ...

  3. thinkphp5 引用 phpass加密算法

    引入phpass thinkPHP5 放到扩展目录里 自动加载配置 直接实例化 // 初始化散列器为不可移植(这样更安全) $PasswordHashs = new \PasswordHashs(8, ...

  4. angular入门 - 环境安装及项目创建

    1.安装node.js 下载,安装,在终端测试安装是否成功:node -v(查看nodejs版本) npm -v(查看npm版本) 下载地址:https://nodejs.org/en/downloa ...

  5. MySQL主从复制与读写分离实践

    MySQL主从复制(Master-Slave)与读写分离(MySQL-Proxy)实践  目录: 介绍 MySQL的安装与配置 MySQL主从复制 MySQL读写分离 编译安装lua 安装配置MySQ ...

  6. 后ARM时代,嵌入式工程师的自我修养

    1 嵌入式学习的一些概念理解误区 很多嵌入式初学者认为,学嵌入式,就是学习ARM,就是学习开发板.买一块开发板,然后在上面“移植”u-boot.Linux内核,再使用busybox制作一个根文件系统, ...

  7. CDH6.2的fair-scheduler.xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><alloc ...

  8. IO-file-05 文件夹的创建

    package com.bwie.io; import java.io.File; /** * 创建目录 * 1.mkdir:确保上级目录存在,不存在创建失败 * 2.mkdirs:上级目录可以不存在 ...

  9. IP地址 子网掩码 网络地址 主机地址 广播地址

    1.一定要明白各自的概念分别表示什么 IP地址:IP地址是用来识别网络上的设备,因此,IP地址是由网络地址与主机地址两部分所组成. 子网掩码:子网掩码不能单独存在,它必须结合IP地址一起使用.子网掩码 ...

  10. wireguard使用方法

    1.翻墙访问网页:https://cryptostorm.is/wireguard.cgi 并下载客户端 2. 选者第二个并打开 3.复制publickey 4.黏贴在第二行并addkey: 5.将获 ...