smhy8187

 

如何将IList转换为DataTable?

http://www.fjvk.com/csdntopic/bbshtml/t/20050217/02/3786536.html

2005-02-17 02:28:01 在 .NET技术 / C# 提问

我正在按照MSPetShop3的架构开发一个B/S应用程序。  
  我从BLL层获得了IList接口的数据,IList成员为我自定义的类(比如MyProduct),  
   
  将IList对象直接绑定到DataGrid是可以的,但是我想让DataGrid支持排序,而IList没有Sort方法。  
  所以我想将IList对象转换为DataTable,然后由该DataTable创建一个DataView,这样就可以获得DataView的Sort方法了。  
  可是我怎么才能将IList转换为DataTable?  
   
  进一步提问,  
  IList、ArrayList、DataTable……之间除了用遍历的方法,该如何转换? 问题点数:100、回复次数:8Top

1 楼saucer(思归)回复于 2005-02-17 03:05:37 得分 90

你要用DataTable的话,建议你看duwamish   的例子,但感觉这是在倒退  
   
  建议你写个IComparable对象,这样就可以对你的对象排序了,参考  
  http://www.c-sharpcorner.com/Language/IComparablePSD.aspTop

2 楼LoveCherry(论成败,人生豪迈;大不了,重头再来!^_^)回复于 2005-02-17 07:56:50 得分 5

类IComparable接口写一个CompareTo方法Top

3 楼hivak47(比尔)回复于 2005-02-17 09:15:50 得分 0

向楼上的两位学习!!!!!!!!!!!!!!!!!Top

4 楼yizhixiaozhu(天啦,手都起茧了)回复于 2005-02-17 09:20:19 得分 0

高手聚会  
  学习Top

5 楼zhzuo(秋枫)回复于 2005-02-17 09:29:05 得分 5

public   class   DataTable   :   MarshalByValueComponent,   IListSource,  
        ISupportInitialize,   ISerializable  
   
  不能转换。  
   
  如思归大哥说的。Top

6 楼mooddecode1980(心情解码)回复于 2005-02-17 09:48:23 得分 0

:)  
   
  --Top

7 楼shooray(lion)回复于 2005-02-18 03:31:50 得分 0

我认真研究了一下System.Collections下的一些类和接口,  
  暂时得出以下结论,并在没有转换为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作为示例改写过的,其实我要写的应用程序不是购物的。  
   
  总之以上代码达到了我的目的,我可以安心睡觉了,我搜索过以前的文章,也有类似提问,但始终没有一个像样的解答。我把我的收获回馈给大家,希望后来者不要走弯路。也非常感谢前面热心的朋友,没有你们的抛砖引玉,我也无法继续下去。  
   
  问题还没有结束,那就是DataTable与Array,ArrayList……之间,除了用遍历的办法真的无法互相转换了吗?  
  求高手解答,我暂时不结贴。  

posted on 2007-07-17 11:05  new2008  阅读(2657)  评论(0)    收藏  举报

导航