解决DataGridView绑定List后不能排序的问题
???閱讀全文并下載例子 :http://www.sufeinet.com/forum.php?mod=viewthread&tid=190
? 以前不都是用table直接綁定DataGridView的,沒有出現過不能排序的問題,初試List結果發現不管怎么樣都不能實現排序的功能,有朋友說
DataGridView每一列都有個Sortable,默認Automatic,改成NotSortable了,結果怎樣,還是不行啦。
???? 還有朋友說, 你可以拖一個bindingsource控件. bindingsource.datasource=泛型集合 datagridview.datasource=bindingsource;
我發現也是不行,那要怎么辦呢?查一下資料才知道
??? 用泛型會失去DateTable的特性,要實現System.Collections.Generic.IComparer<T> 才能實現排序
?沒有辦法只能實現 一把了
? 看一下下面的代碼吧, 基本 是這樣的
代碼 using?System;using?System.ComponentModel;
using?System.Collections.Generic;
using?System.Reflection;
namespace?BaseFunction
{
????class?ObjectPropertyCompare<T>?:?System.Collections.Generic.IComparer<T>?
????{
????????private?PropertyDescriptor?property;
????????private?ListSortDirection?direction;
????????public?ObjectPropertyCompare(PropertyDescriptor?property,?ListSortDirection?direction)
????????{
????????????this.property?=?property;
????????????this.direction?=?direction;
????????}
????????#region?IComparer<T>
????????///?<summary>
????????///?比較方法
????????///?</summary>
????????///?<param?name="x">相對屬性x</param>
????????///?<param?name="y">相對屬性y</param>
????????///?<returns></returns>
????????public?int?Compare(T?x,?T?y)
????????{
????????????object?xValue?=?x.GetType().GetProperty(property.Name).GetValue(x,?null);
????????????object?yValue?=?y.GetType().GetProperty(property.Name).GetValue(y,?null);
????????????int?returnValue;
????????????if?(xValue?is?IComparable)
????????????{
????????????????returnValue?=?((IComparable)xValue).CompareTo(yValue);
????????????}
????????????else?if?(xValue.Equals(yValue))
????????????{
????????????????returnValue?=?0;
????????????}
????????????else
????????????{
????????????????returnValue?=?xValue.ToString().CompareTo(yValue.ToString());
????????????}
????????????if?(direction?==?ListSortDirection.Ascending)
????????????{
????????????????return?returnValue;
????????????}
????????????else
????????????{
????????????????return?returnValue?*?-1;
????????????}
????????}
????????public?bool?Equals(T?xWord,?T?yWord)
????????{
????????????return?xWord.Equals(yWord);
????????}
????????public?int?GetHashCode(T?obj)
????????{
????????????return?obj.GetHashCode();
????????}
????????#endregion
????}
}
?
在實現了這個接口之后還不能急,我們還要來寫一個SortableBindingList <T> :BindingList <T> 的類用來綁定數據
基本實現
?
代碼 using?System;using?System.ComponentModel;
using?System.Collections.Generic;
using?System.IO;
using?System.Runtime.Serialization.Formatters.Binary;
using?System.Text;
namespace?BaseFunction
{
????public?class?BindingCollection<T>?:?BindingList<T>
????{
????????private?bool?isSorted;
????????private?PropertyDescriptor?sortProperty;
????????private?ListSortDirection?sortDirection;
????????protected?override?bool?IsSortedCore
????????{
????????????get?{?return?isSorted;?}
????????}
????????protected?override?bool?SupportsSortingCore
????????{
????????????get?{?return?true;?}
????????}
????????protected?override?ListSortDirection?SortDirectionCore
????????{
????????????get?{?return?sortDirection;?}
????????}
????????protected?override?PropertyDescriptor?SortPropertyCore
????????{
????????????get?{?return?sortProperty;?}
????????}
????????protected?override?bool?SupportsSearchingCore
????????{
????????????get?{?return?true;?}
????????}
????????protected?override?void?ApplySortCore(PropertyDescriptor?property,?ListSortDirection?direction)
????????{
????????????List<T>?items?=?this.Items?as?List<T>;
????????????if?(items?!=?null)
????????????{
????????????????ObjectPropertyCompare<T>?pc?=?new?ObjectPropertyCompare<T>(property,?direction);
????????????????items.Sort(pc);
????????????????isSorted?=?true;
????????????}
????????????else
????????????{
????????????????isSorted?=?false;
????????????}
????????????sortProperty?=?property;
????????????sortDirection?=?direction;
????????????this.OnListChanged(new?ListChangedEventArgs(ListChangedType.Reset,?-1));
????????}
????????protected?override?void?RemoveSortCore()
????????{
????????????isSorted?=?false;
????????????this.OnListChanged(new?ListChangedEventArgs(ListChangedType.Reset,?-1));
????????}
????????//排序
????????public?void?Sort(PropertyDescriptor?property,?ListSortDirection?direction)
????????{
????????????this.ApplySortCore(property,?direction);
????????}
????}
}
?
現 在應該流到怎么使用了,其實很簡單
直接
?BindingCollection<object?>?objList?=?new?BindingCollection<object>();?objList?=你的結果集;
?this.dataGridView1.DataSource?=?objList;
?
但是現在是問題是我的之前用的是List,不想改,而且調用的是Dll,人家返回的就是一個List,我沒有辦法改成BindingCollection<object?>啊。
想了半天還是想出來了,只是不知道 在性能和別的方面怎么樣,所以把代碼發上來大家討論一下
我是這樣實現 的
代碼 ?//可以實現排序的類????????????BindingCollection<historyorderInfo>?objList?=?new?BindingCollection<historyorderInfo>();
????????????//加載數據
????????????foreach?(historyorderInfo?item?in?tmpList)
????????????{
????????????????objList.Add(item);
????????????}
????????????dgvhistory.DataSource?=?objList;
?
?
這里的tmpList就是我之前使用的系統原本的List,我是使用了?foreach?把原來的數據導入到BindingCollection中的。
這樣的確定是可以實現 我想要的效果的。不知道這樣做有什么不到之處。希望能得到高人的指點啊,呵呵
?
轉載于:https://www.cnblogs.com/sufei/archive/2010/02/04/1663125.html
總結
以上是生活随笔為你收集整理的解决DataGridView绑定List后不能排序的问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adodb.stream对象的方法/属性
- 下一篇: 中文(英译) 爱情一句话哲理