1
private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
2
{
3
int sortColumn = e.Column;
4
ListView listView = (ListView)sender;
5
6
if (listView.Name == "listView2")
7
{
8
if (listView2.Sorting == SortOrder.Ascending)
9
listView2.Sorting = SortOrder.Descending;
10
else
11
listView2.Sorting = SortOrder.Ascending;
12
13
this.listView2.ListViewItemSorter = new ListViewItemComparer(e.Column, listView2.Sorting);
14
}
15
else if (listView.Name == "listView3")
16
{
17
if (listView3.Sorting == SortOrder.Ascending)
18
listView3.Sorting = SortOrder.Descending;
19
else
20
listView3.Sorting = SortOrder.Ascending;
21
22
this.listView3.ListViewItemSorter = new ListViewItemComparer(e.Column, listView3.Sorting);
23
}
24
else if (listView.Name == "listView4")
25
{
26
if (listView4.Sorting == SortOrder.Ascending)
27
listView4.Sorting = SortOrder.Descending;
28
else
29
listView4.Sorting = SortOrder.Ascending;
30
31
this.listView4.ListViewItemSorter = new ListViewItemComparer(e.Column, listView4.Sorting);
32
}
33
else if (listView.Name == "listView5")
34
{
35
if (listView5.Sorting == SortOrder.Ascending)
36
listView5.Sorting = SortOrder.Descending;
37
else
38
listView5.Sorting = SortOrder.Ascending;
39
40
this.listView5.ListViewItemSorter = new ListViewItemComparer(e.Column, listView5.Sorting);
41
}
42
}
43
44
class ListViewItemComparer : System.Collections.IComparer
45
{
46
private int col;
47
private SortOrder order;
48
public ListViewItemComparer()
49
{
50
col = 0;
51
order = SortOrder.Ascending;
52
}
53
public ListViewItemComparer(int column, SortOrder order)
54
{
55
col = column;
56
this.order = order;
57
}
58
public int Compare(object x, object y)
59
{
60
int returnVal;
61
decimal w;
62
DateTime r;
63
bool k = Decimal.TryParse(((ListViewItem)x).SubItems[col].Text, out w);
64
bool s = DateTime.TryParse(((ListViewItem)x).SubItems[col].Text, out r);
65
if (k == true && order != SortOrder.Descending)//数字正排序
66
{
67
Decimal k1 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
68
Decimal k2 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
69
if (k2 > k1)
70
returnVal = -1;
71
if (k2 < k1)
72
returnVal = 1;
73
else
74
returnVal = 0;
75
}
76
else
77
{
78
if (s == true)//时间排序
79
{
80
System.DateTime firstDate = DateTime.Parse(((ListViewItem)x).SubItems[col].Text);
81
System.DateTime secondDate = DateTime.Parse(((ListViewItem)y).SubItems[col].Text);
82
returnVal = DateTime.Compare(firstDate, secondDate);
83
}
84
else//字符排序
85
{
86
returnVal = String.Compare(((ListViewItem)x).SubItems[col].Text,
87
((ListViewItem)y).SubItems[col].Text);
88
}
89
}
90
91
if (order == SortOrder.Descending)
92
{
93
if (k == true)//数字倒排序
94
{
95
System.Int32 k3 = Int32.Parse(((ListViewItem)x).SubItems[col].Text);
96
System.Int32 k4 = Int32.Parse(((ListViewItem)y).SubItems[col].Text);
97
if (k3 > k4)
98
returnVal = 1;
99
if (k3 < k4)
100
returnVal = -1;
101
else
102
returnVal = 0;
103
}
104
returnVal *= -1;
105
}
106
return returnVal;
107
}
108
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108
