帮助类:

 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. Can't create a new thread (errno 11) 解决办法 mysql无法连接

    问题的现象: 错误信息: ERROR 1135 (00000): Can't create a new thread (errno 11); if you are not out of availab ...

  2. 【Leetcode_easy】720. Longest Word in Dictionary

    problem 720. Longest Word in Dictionary 题意: solution1: BFS; class Solution { public: string longestW ...

  3. Spring Aop(十六)——编程式的自定义Advisor

    转发:https://www.iteye.com/blog/elim-2399437 https://www.iteye.com/blogs/subjects/springaop 编程式的自定义Adv ...

  4. Oracle之配置节点间相互信任机制测试

    更改一下,之前的都不对,现在来一版简单有效的ssh互信配置 例如我的是在配置rac,在grid用户下配置互信:建立好相应的文件 mkdir .ssh chmod -R 700 .ssh/ 使用ssh- ...

  5. 数据结构 -- Trie字典树

    简介 字典树:又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种. 优点:利用字符串的公共前缀来减少查询时间,最大限度地减少无谓的字符串比较,查询效率比哈希树高. 性质:   1.  根节 ...

  6. 【坑】springMvc 信息校验,读取不到错误配置信息的问题

    文章目录 前言 ResourceBundleMessageSource 后记 前言 springMvc 的一大利器,validation 检验,通过注解,可以帮我们完成校验,很是顺手. 终极偷懒检验, ...

  7. LaTeX 一些用法实例(并列图片、并列表格、算法代码示例、页眉太长、下划线,等)

    横向并列两个图片 \begin{figure} \begin{minipage}{0.49\linewidth} \centering \includegraphics[width=6.5cm]{Si ...

  8. spring cloud微服务实践一

    最近在学习spring框架.其中spring cloud在微服务方面很火,所以在学习过程中,也做一些记录. 注:这一个系列的开发环境版本为 java1.8, spring boot2.x, sprin ...

  9. Linux 系统下提取 ShellCode

    未完待续 1.使用C语言编写一个获得系统Shell的小程序. #include <unistd.h> int main() { char * shell[2]; shell[0]=&quo ...

  10. Optional 理解

    目录 Optional 理解 1. 含义 2. Optional 类中方法 3. Optional 对象不应该作为方法参数 Optional 理解 1. 含义 Optional 是一个容器对象,该容器 ...