Use the order attribute of dataprovider in deklarit for dynamic sorting

Source: Internet
Author: User

1. view the class code of dataadapter generated by dataprovider. The following three private members and one public attribute are sorted.

Private string orderstring; // The positive order, which is sorted by the user.
Private string reverseorderstring; // reverse order, opposite to the sorting order set by the user
Private arraylist orderarray; // stores ordered attributes.
Public arraylist order
{
Get
{
Return orderarray;
}
Set
{
Orderarray = value;
}
}

2. the following lists the enumerated types and names of Data columns that can be sorted Based on the attribute columns set in 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

3. Define the sorting attribute class. This class contains three members: orderattributename: Sorting enumeration, orderattributeascending: sorting order, orderattributestring: Sorting Field 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; // sorting Enumeration
19 public bool orderattributeascending; // sorting order, true: ascending, false: Descending
20 Public String orderattributestring; // name of the sorted data column
21}

4. Order by clause that generates the SELECT command: 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 ";
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. when querying data, obtain the order by clause statement. Pay attention to the code lines marked as red. The order by clause is saved in orderstring and reverseorderstring members, finally, when executing the executeprivate method, use the clauses saved in orderstring to form the final SELECT query statement: 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 ();
8Orderstring = getorderstring(This. Order, true); // gets the order by clause in the forward order.
9Reverseorderstring = getorderstring(This. Order, false); // gets the order by clause in reverse order.
10 try
11 {
12 executeprivate (startrow, maxrows );
13}
14 finally
15 {
16 this. Cleanup ();
17}
18 return 0;
19}

6. Use Dynamic sorting: 1
2 classnotelistdataadapter dataadapter = new classnotelistdataadapter ();
3 dataadapter. Order. Clear (); // important
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

After deklarit sorting is introduced, the sorting method used by deklarit is worth learning. It is a type of strong sorting processing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.