将数据绑定在下面的类中就可以实现排序

    public class SortableBindingList<T> : BindingList<T>
{
private ArrayList sortedList;
private bool isSortedValue; public SortableBindingList()
{
} public SortableBindingList(IList<T> list)
{
foreach (object o in list)
{
this.Add((T)o);
}
} protected override bool SupportsSortingCore
{
get { return true; }
} protected override bool IsSortedCore
{
get { return isSortedValue; }
} ListSortDirection sortDirectionValue;
PropertyDescriptor sortPropertyValue; protected override void ApplySortCore(PropertyDescriptor prop,
ListSortDirection direction)
{
sortedList = new ArrayList(); Type interfaceType = prop.PropertyType.GetInterface("IComparable"); if (interfaceType == null && prop.PropertyType.IsValueType)
{
Type underlyingType = Nullable.GetUnderlyingType(prop.PropertyType); if (underlyingType != null)
{
interfaceType = underlyingType.GetInterface("IComparable");
}
} if (interfaceType != null)
{
sortPropertyValue = prop;
sortDirectionValue = direction; IEnumerable<T> query = base.Items;
if (direction == ListSortDirection.Ascending)
{
query = query.OrderBy(i => prop.GetValue(i));
}
else
{
query = query.OrderByDescending(i => prop.GetValue(i));
}
int newIndex = 0;
foreach (object item in query)
{
this.Items[newIndex] = (T)item;
newIndex++;
}
isSortedValue = true;
this.OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1)); }
else
{
throw new NotSupportedException("Cannot sort by " + prop.Name +
". This" + prop.PropertyType.ToString() +
" does not implement IComparable");
}
} protected override PropertyDescriptor SortPropertyCore
{
get { return sortPropertyValue; }
} protected override ListSortDirection SortDirectionCore
{
get { return sortDirectionValue; }
} }

DataGridView DataSource 如何实现排序的更多相关文章

  1. 禁用datagridview中的自动排序功能

    把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...

  2. winform中datagridview刷新后的排序记忆

    datagridview先点标题排序,但是重新刷新之后,还是变成窗体加载后的样子 我这里用定时器刷新的. 1.先定义三个全局变量 /// <summary> /// 需要排序的列和方向 / ...

  3. Winform Datagridview 点击headercolumn排序

    /// <summary> /// 排序顺序 /// </summary> bool asc; /// <summary> /// Dgv点击排序 /// < ...

  4. DataGridView.DataSource= list(Of T)

    注:本文样例的代码承接上篇文章:DataTable填充实体类返回泛型集合. 在D层查询完毕之后.我们将DataTable转化为泛型集合.然后经过中间各层,返回U层.到了这里,问题来了.我们这时候要将这 ...

  5. C++: DataGridView::DataSource

    #pragma once #include "Form2.h" namespace cdemo { using namespace System; using namespace ...

  6. DataGridView的自定义列排序

    1,将需要进行排序的列做属性的设置 this.colUserName.SortMode = DataGridViewColumnSortMode.Programmatic; 2,添加列的事件 //点击 ...

  7. 解决DataGridView.DataSource重复赋值而不显示问题

    List<Person> list=new List<Person>(); ;i<;i++) { list.Add(new Person(){........}) } d ...

  8. DataGridView DataSource INotifyPropertyChanged 避免闪烁的方法

    代码说话: dgvPosition就是需要避免闪烁的DataGridView 主要是加2段代码 1.SetStyle 2.datagridview设置DoubleBuffered属性为True pub ...

  9. C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题

    帮助类: public class SortBindingHelper<T> : BindingList<T> { private bool isSortedCore = tr ...

随机推荐

  1. Leetcode 001-twosum

    #Given an array of integers, return indices of the two numbers such that they add up to a specific t ...

  2. Topcoder SRM 638 DIV 2 (大力出奇迹)

    水题,就是一个暴力.大力出奇迹. Problem Statement   There is a narrow passage. Inside the passage there are some wo ...

  3. Java类加载器( 死磕 6)

    [正文]Java类加载器(  CLassLoader )死磕 6:  自定义网络类加载器 本小节目录 6.1. 自定义网络类加载器的类设计 6.2. 文件传输Server端的源码 6.3. 文件传输C ...

  4. aop学习总结三----aop的相关概念

    aop学习总结三----aop的相关概念 public Object invoke(Object proxy, Method method, Object[] args) throws Throwab ...

  5. 阿里Java开发手册学习 3 MYSQL规约

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  6. System.IO.File类和System.IO.FileInfo类

    1.System.IO.File类 ※文件create, copy,move,SetAttributes,open,exists ※由于File.Create方法默认向所有用户授予对新文件的完全读写. ...

  7. appium(4)-Automating mobile web apps

    Automating mobile web apps If you’re interested in automating your web app in Mobile Safari on iOS o ...

  8. PAT 天梯赛 L3-010. 是否完全二叉搜索树 【Tree】

    题目链接 https://www.patest.cn/contests/gplt/L3-010 思路 因为是 完全二叉搜索树 可以用 数据 建树的方式 然后 遍历一遍这个 数字 就是 层序遍历 遍历的 ...

  9. Whats the difference between service tomcat ./startup.sh and ./catalina.sh run

    stack overflow 给出的答案: catalina.sh run starts tomcat in the foreground, displaying the logs on the co ...

  10. html页面表格导出到excel总结

    转载:http://www.cnblogs.com/liuguanghai/archive/2012/12/31/2840262.html <table id="tableExcel& ...