我正在按照MSPetShop3的架构开发一个B/S应用程序。
我从BLL层获得了IList接口的数据,IList成员为我自定义的类(比如MyProduct),将IList对象直接绑定到DataGrid是可以的,但是我想让DataGrid支持排序,而IList没有Sort方法。
所以我想将IList对象转换为DataTable,然后由该DataTable创建一个DataView,这样就可以获得DataView的Sort方法了。
可是我怎么才能将IList转换为DataTable?进一步提问,
IList、ArrayList、DataTable……之间除了用遍历的方法,该如何转换?
我从BLL层获得了IList接口的数据,IList成员为我自定义的类(比如MyProduct),将IList对象直接绑定到DataGrid是可以的,但是我想让DataGrid支持排序,而IList没有Sort方法。
所以我想将IList对象转换为DataTable,然后由该DataTable创建一个DataView,这样就可以获得DataView的Sort方法了。
可是我怎么才能将IList转换为DataTable?进一步提问,
IList、ArrayList、DataTable……之间除了用遍历的方法,该如何转换?
dt = (DataTable)IList;
代码如下:
public void Sort(TemplateComparer.ComparisonType ctype)
{
TemplateComparer tmpCompate = new TemplateComparer();
tmpCompate.WhichComparison = (int)ctype;
this.InnerList.Sort(tmpCompate);
}
TemplateComparer是继承自IComparer的一个类。
暂时得出以下结论,并在没有转换为DataTable的前提下,实现了排序(逆序)。首先,我的提问不够专业。“我从BLL层获得了IList接口的数据,IList成员为我自定义的类(比如MyProduct),”,事实上,IList只是接口,IList的底层实现其实是ArrayList(PetShop3中的SQLServerDAL中可以证明),明确这一点,就好办了。
原来的提问其实就是ArrayList如何向DataTable转换的问题。我有个前提是不用遍历的方法(在通过IList转递数据前,我用遍历创建了ArrayList,到了表示层又遍历一遍来获得DataTable,实在没有什么理由让我这么做,但是的确遍历是无所不能的),所以问题又转换为如何对ArrayList进行排序。感谢 saucer(思归) ,给了
http://www.c-sharpcorner.com/Language/IComparablePSD.asp
这样一个例子,让我对Sort有了信心。
这个例子是对普通的数组Array,通过实现IComparable接口的CompareTo方法实现排序。
然而,我得到的是可以动态增加的特殊数组对象ArrayList,所以必须进行转换ArrayList arrayList = (ArrayList) myProduct.GetProducts(productID);
object[] temp = arrayList.ToArray();
Array.Sort(temp);
MyDataGrid.DataSource = arrayList;
MyDataGrid.DataBind();以上代码需要在Model层中实现IComparable接口,并且ArrayList向Array转换后,数组元素成了Object,使用起来还要进行类型强制转换。另外一个问题是,我无法对选择排序的成员。
所以,我决定放弃Array和IComparable接口,因为我发现ArrayList也有Sort方法,而且比IComparable更好的排序手段是转递一个System.Collections.IComparer给Sort方法(Array和ArrayList都可以)。具体实现如下:
我在Model层中的MyProductInfo类中加入下面一个类
public class MyComparer : IComparer
{
// Calls CaseInsensitiveComparer.Compare with the parameters reversed.
private string _what;
private bool _desc = false;
public MyComparer(string what)
{
_what = what;
}
public MyComparer(string what, bool desc)
{
_what = what;
_desc = desc;
}
int IComparer.Compare(object m, object n)
{
MyProductInfo x = (MyProductInfo)m;
MyProductInfo y = (MyProductInfo)n;
int returnValue;
switch(_what)
{
case "ProductID":
returnValue = (x.ProductID>y.ProductID?1:(x.ProductID==y.ProductID?0:-1));
break;
case "ProductSum":
returnValue = (x.ProductSum>y.ProductSum?1:(x.ProductSum==y.ProductSum?0:-1));
break;
case "ProductName":
returnValue = (String.Compare(x.ProductName,y.ProductName));
break;
/*其他需要排序的属性*/
default:
returnValue = (x.ProductID>y.ProductID?1:(x.ProductID==y.ProductID?0:-1));
break;
};
if(_desc)returnValue = -returnValue;
return(returnValue);
}
}_desc的加入是为了产生逆序。然后在表示层这样调用:
private void BindData()
{
long productID = 1; //just a test productID
MyProduct myProduct = new MyProduct();
ArrayList arrayList = (ArrayList) myProduct.GetProducts(productID);
IComparer myComparer = new MyProductInfo.MyComparer("ProductName",true);
arrayList.Sort(myComparer);
MyDataGrid.DataSource = arrayList;
MyDataGrid.DataBind();
}当然啦,我后来通过编写MyDataGrid_SortCommand方法实现了任意字段的顺序、逆序,以及在Session暂存ArrayList提高效率就不提了。
以上代码是我以MyProduct作为示例改写过的,其实我要写的应用程序不是购物的。总之以上代码达到了我的目的,我可以安心睡觉了,我搜索过CSDN以前的文章,也有类似提问,总之没有一个像样的解答。我把我的收获回馈给大家,希望后来者不要走弯路。也非常感谢前面热心的朋友,没有你们的抛砖引玉,我也无法继续下去。问题还没有结束,那就是DataTable与Array,ArrayList……之间,除了用遍历的办法真的无法互相转换了吗?
求高手解答,我暂时不结贴。