巧用Deklarit中DataProvider的Order属性进行动态排序
2008-05-08 23:38 Kevin-wang 阅读(307) 评论(1) 编辑 收藏 举报
1.查看 DataProvider生成DataAdapter的类代码,其中有下面三个private成员和一个public属性用做排序
private string orderString ; //正序,根据用户设置的排序顺序
private string reverseOrderString ; //反序,与用户设置的排序顺序相反
private ArrayList orderArray ; //存储排序的属性信息
public ArrayList Order
{
get
{
return orderArray ;
}
set
{
orderArray = value ;
}
}
private string reverseOrderString ; //反序,与用户设置的排序顺序相反
private ArrayList orderArray ; //存储排序的属性信息
public ArrayList Order
{
get
{
return orderArray ;
}
set
{
orderArray = value ;
}
}
2.根据DataProvider中设置的属性列新建的枚举类型与允许排序的数据列名称,如下所示:
1 public enum Attribute {Note_ID, Note_Title, Class_ID, Class_Name, Note_UpdateTime, Class_Grade, Note_FileName};
2 private static String[] attributeNames = new String[] {"TM1.[Note_ID]", "TM1.[Note_Title]", "TM1.[Class_ID]", "T2.[Class_Name]", "TM1.[Note_UpdateTime]", "T2.[Class_Grade]", "TM1.[Note_FileName]"} ;
3
2 private static String[] attributeNames = new String[] {"TM1.[Note_ID]", "TM1.[Note_Title]", "TM1.[Class_ID]", "T2.[Class_Name]", "TM1.[Note_UpdateTime]", "T2.[Class_Grade]", "TM1.[Note_FileName]"} ;
3
3.定义排序属性类,此类包含三个成员,OrderAttributeName:排序枚举、OrderAttributeAscending:排序顺序、OrderAttributeString:排序字段
1 public class OrderAttribute
2 {
3 public OrderAttribute( Attribute orderAttributeName ,
4 bool orderAttributeAscending )
5 {
6 this.OrderAttributeName = orderAttributeName ;
7 this.OrderAttributeAscending = orderAttributeAscending ;
8 this.OrderAttributeString = attributeNames[((int)(this.OrderAttributeName))] ;
9 }
10
11 public OrderAttribute( Attribute orderAttributeName )
12 {
13 this.OrderAttributeName = orderAttributeName ;
14 this.OrderAttributeAscending = true ;
15 this.OrderAttributeString = attributeNames[((int)(this.OrderAttributeName))] ;
16 }
17
18 public Attribute OrderAttributeName ; //排序枚举
19 public bool OrderAttributeAscending ; //排序顺序,true:升序,false:降序
20 public String OrderAttributeString ; //排序数据列名称
21 }
2 {
3 public OrderAttribute( Attribute orderAttributeName ,
4 bool orderAttributeAscending )
5 {
6 this.OrderAttributeName = orderAttributeName ;
7 this.OrderAttributeAscending = orderAttributeAscending ;
8 this.OrderAttributeString = attributeNames[((int)(this.OrderAttributeName))] ;
9 }
10
11 public OrderAttribute( Attribute orderAttributeName )
12 {
13 this.OrderAttributeName = orderAttributeName ;
14 this.OrderAttributeAscending = true ;
15 this.OrderAttributeString = attributeNames[((int)(this.OrderAttributeName))] ;
16 }
17
18 public Attribute OrderAttributeName ; //排序枚举
19 public bool OrderAttributeAscending ; //排序顺序,true:升序,false:降序
20 public String OrderAttributeString ; //排序数据列名称
21 }
4.产生Select命令的Order by子句:
1 public static string GetOrderString(ArrayList ListOrder, bool AscendingOrder)
2 {
3 string orderString = "" ;
4 string prevString = "" ;
5 if ( ( ListOrder.Count > 0 ) )
6 {
7 prevString = " ORDER BY " ;
8 }
9 foreach( OrderAttribute orderAtt in ListOrder )
10 {
11 if ( ( orderAtt.OrderAttributeAscending == AscendingOrder ) )
12 {
13 orderString = orderString + prevString + " " + orderAtt.OrderAttributeString ;
14 }
15 else
16 {
17 orderString = orderString + prevString + " " + orderAtt.OrderAttributeString + " DESC" ;
18 }
19 prevString = ", ";
20 }
21 return orderString;
22 }
2 {
3 string orderString = "" ;
4 string prevString = "" ;
5 if ( ( ListOrder.Count > 0 ) )
6 {
7 prevString = " ORDER BY " ;
8 }
9 foreach( OrderAttribute orderAtt in ListOrder )
10 {
11 if ( ( orderAtt.OrderAttributeAscending == AscendingOrder ) )
12 {
13 orderString = orderString + prevString + " " + orderAtt.OrderAttributeString ;
14 }
15 else
16 {
17 orderString = orderString + prevString + " " + orderAtt.OrderAttributeString + " DESC" ;
18 }
19 prevString = ", ";
20 }
21 return orderString;
22 }
5.查询数据时获取Order by子句语句,注意看标注为红色的代码行,得到Order by子句保存在orderString和reverseOrderString成员,最后在执行executePrivate方法时使用orderString中保存的子句组成最终的Select查询语句:
1 public virtual int FillPage( ClassNoteListDataSet dataSet ,
2 int startRow ,
3 int maxRows )
4 {
5 this.Initialize( ) ;
6 ClassNoteListSet = dataSet ;
7 rowClassNote = ClassNoteListSet.ClassNote.NewClassNoteRow() ;
8 orderString = GetOrderString( this.Order, true) ; //获取正序的Order by子句
9 reverseOrderString = GetOrderString( this.Order, false) ; //获取反序的Order by 子句
10 try
11 {
12 executePrivate( startRow, maxRows) ;
13 }
14 finally
15 {
16 this.Cleanup();
17 }
18 return 0 ;
19 }
2 int startRow ,
3 int maxRows )
4 {
5 this.Initialize( ) ;
6 ClassNoteListSet = dataSet ;
7 rowClassNote = ClassNoteListSet.ClassNote.NewClassNoteRow() ;
8 orderString = GetOrderString( this.Order, true) ; //获取正序的Order by子句
9 reverseOrderString = GetOrderString( this.Order, false) ; //获取反序的Order by 子句
10 try
11 {
12 executePrivate( startRow, maxRows) ;
13 }
14 finally
15 {
16 this.Cleanup();
17 }
18 return 0 ;
19 }
6.使用动态排序:
1
2 ClassNoteListDataAdapter dataAdapter=new ClassNoteListDataAdapter();
3 dataAdapter.Order.Clear();//此处很重要
4 dataAdapter.Order.Add(new ClassNoteListDataAdapter.OrderAttribute(ClassNoteListDataAdapter.Attribute.Class_ID,false));
5 dataAdapter.Order.Add(new ClassNoteListDataAdapter.OrderAttribute(ClassNoteListDataAdapter.Attribute.Note_UpdateTime,true));
6 dataAdapter.Fill(..);
7
8
2 ClassNoteListDataAdapter dataAdapter=new ClassNoteListDataAdapter();
3 dataAdapter.Order.Clear();//此处很重要
4 dataAdapter.Order.Add(new ClassNoteListDataAdapter.OrderAttribute(ClassNoteListDataAdapter.Attribute.Class_ID,false));
5 dataAdapter.Order.Add(new ClassNoteListDataAdapter.OrderAttribute(ClassNoteListDataAdapter.Attribute.Note_UpdateTime,true));
6 dataAdapter.Fill(..);
7
8
DeKlarit排序介绍完毕,DeKlarit使用的排序方式值得借鉴,属于强类型的排序处理。