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

    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. OpenStack部署到Hadoop的四种方案

    随着企业開始同一时候利用云计算和大数据技术.如今应当考虑怎样将这些工具结合使用.在这样的情况下,企业将实现最佳的分析处理能力.同一时候利用私有云的高速弹性 (rapid elasticity) 和单一 ...

  2. 【BZOJ4296】[PA2015]Mistrzostwa BFS

    [BZOJ4296][PA2015]Mistrzostwa Description 给定一张n个点m条边的无向图,请找到一个点数最多的点集S,满足:1.对于点集中任何一个点,它至少与d个点集中的点相邻 ...

  3. 【BZOJ3252】攻略 DFS序+线段树(模拟费用流)

    [BZOJ3252]攻略 Description 题目简述:树版[k取方格数] 众所周知,桂木桂马是攻略之神,开启攻略之神模式后,他可以同时攻略k部游戏. 今天他得到了一款新游戏<XX半岛> ...

  4. 基于注解的Sping AOP详解

    一.创建基础业务 package com.kang.sping.aop.service; import org.springframework.stereotype.Service; //使用注解@S ...

  5. javascript 获取当前日期 月份 时间

    <script type="text/javascript"> function getDate() { var date = new Date(); //得到当前日期 ...

  6. moving from a host-centric infrastructure to a container-centric infrastructure

    https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/ Why do I need Kubernetes and what c ...

  7. Android笔记之强大的buildConfigField

    在进行项目开发或维护时,经常会遇到调试和发布所用到的参数值不一致的情况 例如,服务器会分测试和正式,为了能方便地更改(自动更换)服务器地址,buildConfigField就派上用场了 以前都是手动更 ...

  8. xcode7和ios9下UIWebView不能加载网页的解决方法

    错误描述: App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecu ...

  9. listview 没数据内容时显示一个提示文本

    listview和textview 1:1.listview无内容的时候本身是不显示的,所以textview会显示 getlistview获取系统定义的listview

  10. dialog更新数据

    将数据显示在最上面