DataGridView DataSource 如何实现排序
将数据绑定在下面的类中就可以实现排序
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 如何实现排序的更多相关文章
- 禁用datagridview中的自动排序功能
把datagridview中的自动排序功能禁用自己收集的两种方法,看看吧①DataGridView中的Columns属性里面可以设置.进入"EditColumns"窗口后,在相应的 ...
- winform中datagridview刷新后的排序记忆
datagridview先点标题排序,但是重新刷新之后,还是变成窗体加载后的样子 我这里用定时器刷新的. 1.先定义三个全局变量 /// <summary> /// 需要排序的列和方向 / ...
- Winform Datagridview 点击headercolumn排序
/// <summary> /// 排序顺序 /// </summary> bool asc; /// <summary> /// Dgv点击排序 /// < ...
- DataGridView.DataSource= list(Of T)
注:本文样例的代码承接上篇文章:DataTable填充实体类返回泛型集合. 在D层查询完毕之后.我们将DataTable转化为泛型集合.然后经过中间各层,返回U层.到了这里,问题来了.我们这时候要将这 ...
- C++: DataGridView::DataSource
#pragma once #include "Form2.h" namespace cdemo { using namespace System; using namespace ...
- DataGridView的自定义列排序
1,将需要进行排序的列做属性的设置 this.colUserName.SortMode = DataGridViewColumnSortMode.Programmatic; 2,添加列的事件 //点击 ...
- 解决DataGridView.DataSource重复赋值而不显示问题
List<Person> list=new List<Person>(); ;i<;i++) { list.Add(new Person(){........}) } d ...
- DataGridView DataSource INotifyPropertyChanged 避免闪烁的方法
代码说话: dgvPosition就是需要避免闪烁的DataGridView 主要是加2段代码 1.SetStyle 2.datagridview设置DoubleBuffered属性为True pub ...
- C# Winfrom DataGridView DataSource绑定数据源后--解决排序问题
帮助类: public class SortBindingHelper<T> : BindingList<T> { private bool isSortedCore = tr ...
随机推荐
- android android:duplicateParentState="true" "false"
今天要做一个效果.组件RelativeLayout上有两个TextView.这两个TextView具有不同的颜色值,如今要的效果是,当RelativeLayout被点击时,整个item有高亮背景. 同 ...
- 【BZOJ1520】[POI2006]Szk-Schools KM算法
[BZOJ1520][POI2006]Szk-Schools Description Input Output 如果有可行解, 输出最小代价,否则输出NIE. Sample Input 5 1 1 2 ...
- ReentrantLock和Synchronized
1 synchronized 1.1 一旦没有获取到就只能一直等待 A和B都获取同一个对象锁,如果A获取了,B没有获取到,那么在A释放该锁之前,B只能无穷等待下去. 1.2 synchronized是 ...
- Java类加载器( 死磕 4)
[正文]Java类加载器( CLassLoader ) 死磕 之4: 神秘的双亲委托机制 本小节目录 4.1. 每个类加载器都有一个parent父加载器 4.2. 类加载器之间的层次关系 4.3. ...
- mount available
Mount (computing), the process of making a file system accessible mount (Unix), the utility in Unix- ...
- ubuntu搜狗拼音安装
1.官方下载deb 2.双击安装 3.终端im-config,选择fcitx 4.重启 5.输入法设置中add一下sougoupinyin
- redis一些笔记
base 字典: hget/hset 在redis字典中值只能是字符串,使用渐进式进行rehash.在rehash的过程中,会保留两个hash结构:查询时会同时查询两个结构:逐渐完成hash的迁移. ...
- linux内核段属性机制【转】
本文转载自:https://github.com/TongxinV/oneBook/issues/9 linux内核段属性机制 以subsys_initcall和module_init为例 subsy ...
- 双端队列篇deque SDUT OJ 双向队列
双向队列 Time Limit: 1000MS Memory limit: 65536K 题目描述 想想双向链表……双向队列的定义差不多,也就是说一个队列的队尾同时也是队首:两头都可以做出队,入队的操 ...
- Window 64位下的客户机配置PLSQL链接远程Oracle
此文章记录的是艰难探索. 完成如下工作: 服务器A为Windows Serve 2016:安装Oracle. 客户机B为Win7 x64位,安装PLSQLDevelop,链接A上的Oracle. 首先 ...