All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.net页面输出成HTML页面_[Asp.Net教程]
php获得二维或多维数组的第一维的长度_php资料_编程技术
ASP.NET,Atlas简单控件介绍之界面控件_[Asp.Net教程]
GridView显示服务器图片(保存为图片路径)_[Asp.Net教程]
ASP.NET实现下拉框二级联动组件_[Asp.Net教程]
.net读取扩展名为xml的资源文件_[Asp.Net教程]
XML入门教程-使用CSS显示XML_[XML教程]
实例分享:自己开发的自定义分页控件_.net资料_编程技术
C++类的定义与实现
asp.net实例:图片转换成字符_[Asp.Net教程]
Reading number is top 10 pictures
谁认识这位校花
The real super beauty12
Send some Valentine's day cartoon
男人巳快沦落成动物了
囚犯暴乱了咋办?
So beauty, will let you spray blood3
Wild animals melee moment of life and death1
Take you to walk into the most true north Korea rural4
Beauty Sun Feifei
漂亮脸蛋魔鬼身材2
Download software ranking
塘西风月痕
Professional killers2 data package
打鸟视频
linux初级教程
Unix video tutorial20
Unix video tutorial1
Call Of Duty2
The hero
apache-tomcat-6.0.33
徐若瑄成名作“魔鬼天使”
归海一刀 published in(发表于) 2014/1/30 1:26:37 Edit(编辑)
DataGridView控件和List绑定问题-怎么排序_[Asp.Net教程]

DataGridView控件和List绑定问题-怎么排序_[Asp.Net教程]

DataGridView控件和List绑定问题:怎么排序_[Asp.Net教程]
以前不都是用table直接绑定DataGridView的,没有出现过不能排序的问题,初试List结果发现不管怎么样都不能实现排序的功能,有朋友说

DataGridView每一列都有个Sortable,默认Automatic,改成NotSortable了,结果怎样,还是不行啦。

还有朋友说, 你可以拖一个bindingsource控件. bindingsource.datasource=泛型集合 datagridview.datasource=bindingsource;

我发现也是不行,那要怎么办呢?查一下资料才知道

用泛型会失去DateTable的特性,要实现System.Collections.Generic.IComparer 才能实现排序

没有办法只能实现 一把了

看一下下面的代码吧, 基本 是这样的

代码
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Reflection;

namespace BaseFunction
{
class ObjectPropertyCompare : System.Collections.Generic.IComparer
{
private PropertyDescriptor property;
private ListSortDirection direction;

public ObjectPropertyCompare(PropertyDescriptor property, ListSortDirection direction)
{
this.property = property;
this.direction = direction;
}

#region IComparer

///
/// 比较方法
///

/// 相对属性x
/// 相对属性y
///
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 :BindingList 的类用来绑定数据

基本实现

代码
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 : BindingList
{
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 items = this.Items as List;

if (items != null)
{
ObjectPropertyCompare pc = new ObjectPropertyCompare(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 objList = new BindingCollection();
objList =你的结果集;
this.dataGridView1.DataSource = objList;


但是现在是问题是我的之前用的是List,不想改,而且调用的是Dll,人家返回的就是一个List,我没有办法改成BindingCollection啊。

想了半天还是想出来了,只是不知道 在性能和别的方面怎么样,所以把代码发上来大家讨论一下

我是这样实现 的

代码
//可以实现排序的类
BindingCollection objList = new BindingCollection();
//加载数据
foreach (historyorderInfo item in tmpList)
{
objList.Add(item);
}
dgvhistory.DataSource = objList;

这里的tmpList就是我之前使用的系统原本的List,我是使用了 foreach 把原来的数据导入到BindingCollection中的。


添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.