1
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218 |
using
System; using
System.Collections.Generic; using
System.Linq; using
System.Text; using
System.Windows.Forms; using
System.Drawing; using
System.Collections; namespace
MyCommanHelper { public
class ControlStyleHelper { #region 字段变量 // Fields public
static int currentCol = -1; public
static bool sort; #endregion #region 网格视图 /// <summary> /// 初始化网格视图样式 /// </summary> /// <param name="dataGridView1">网格控件</param> /// <param name="isReadOnly">是否只读</param> /// <param name="isForbidSortColumn">是否禁止排序</param> public
static void InitDataGridView(DataGridView dataGridView1, bool
isReadOnly, bool
isForbidSortColumn) { dataGridView1.RowHeadersDefaultCellStyle.BackColor = SystemColors.GradientActiveCaption; dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = SystemColors.GradientActiveCaption; dataGridView1.BackgroundColor = Color.White; DataGridViewCellStyle style = new
DataGridViewCellStyle(); style.SelectionBackColor = Color.FromArgb(0x80, 0x80, 0xff); style.Alignment = DataGridViewContentAlignment.MiddleCenter; dataGridView1.DefaultCellStyle = style; dataGridView1.ClearSelection(); dataGridView1.AllowUserToAddRows = false ; dataGridView1.ReadOnly = isReadOnly; if
(isForbidSortColumn) { for
( int
i = 0; i < dataGridView1.Columns.Count; i++) { dataGridView1.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable; } } } #endregion #region 对话框 /// <summary> /// 初始化对话框模式样式 /// </summary> /// <param name="frm"></param> public
static void InitFormShowDialogStyle(Form frm) { frm.ShowInTaskbar = false ; frm.MaximizeBox = false ; frm.MinimizeBox = false ; frm.ShowIcon = false ; frm.StartPosition = FormStartPosition.CenterScreen; frm.FormBorderStyle = FormBorderStyle.FixedDialog; frm.ShowDialog(); } /// <summary> /// 初始化对话框非模式样式 /// </summary> /// <param name="frm"></param> public
static void InitFormShowStyle(Form frm) { frm.ShowInTaskbar = true ; frm.MaximizeBox = false ; frm.MinimizeBox = false ; frm.ShowIcon = false ; frm.StartPosition = FormStartPosition.CenterScreen; frm.FormBorderStyle = FormBorderStyle.FixedDialog; frm.Show(); } #endregion #region 列表视图 /// <summary> /// 初始化列表视图样式 /// </summary> /// <param name="listView1">列表视图控件</param> /// <param name="isCheckBox">是否显示复选框</param> public
static void InitListViewStyle(ListView listView1, bool
isCheckBox) { listView1.View = View.Details; listView1.ShowItemToolTips = true ; listView1.GridLines = true ; listView1.FullRowSelect = true ; listView1.CheckBoxes = isCheckBox; } /// <summary> /// 对列排序并标示标识 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public
static void SortListViewColumn( object
sender, ColumnClickEventArgs e) { string
str3; char
ch = ‘▼‘ ; string
str = ch.ToString().PadLeft(4, ‘ ‘ ); ch = ‘▲‘ ; string
str2 = ch.ToString().PadLeft(4, ‘ ‘ ); ListView view = sender as
ListView; if
(!sort) { sort = true ; str3 = view.Columns[e.Column].Text.TrimEnd( new
char [] { ‘▼‘ , ‘▲‘ , ‘ ‘
}); view.Columns[e.Column].Text = str3 + str2; } else
if (sort) { sort = false ; str3 = view.Columns[e.Column].Text.TrimEnd( new
char [] { ‘▼‘ , ‘▲‘ , ‘ ‘
}); view.Columns[e.Column].Text = str3 + str; } if
(e.Column == (view.ListViewItemSorter as
ListViewColumnSorter).SortColumn) { if
((view.ListViewItemSorter as
ListViewColumnSorter).Order == SortOrder.Ascending) { (view.ListViewItemSorter as
ListViewColumnSorter).Order = SortOrder.Descending; } else { (view.ListViewItemSorter as
ListViewColumnSorter).Order = SortOrder.Ascending; } } else { (view.ListViewItemSorter as
ListViewColumnSorter).SortColumn = e.Column; (view.ListViewItemSorter as
ListViewColumnSorter).Order = SortOrder.Ascending; } ((ListView)sender).Sort(); } /// <summary> /// 内部类 /// Nested Types /// </summary> public
class ListViewColumnSorter : IComparer { #region 变量 // Fields private
int ColumnToSort = 0; private
CaseInsensitiveComparer ObjectCompare = new
CaseInsensitiveComparer(); private
SortOrder OrderOfSort = SortOrder.None; #endregion #region 属性 // Properties public
SortOrder Order { get { return
this .OrderOfSort; } set { this .OrderOfSort = value; } } public
int SortColumn { get { return
this .ColumnToSort; } set { this .ColumnToSort = value; } } #endregion #region 方法 // Methods /// <summary> /// 依据double类型列的值比较 /// </summary> /// <param name="x">ListViewItem</param> /// <param name="y">ListViewItem</param> /// <returns></returns> public
int Compare( object
x, object
y) { ListViewItem item = (ListViewItem)x; ListViewItem item2 = (ListViewItem)y; int
num = this .ObjectCompare.Compare(ConvertHelper.ObjectToDouble(item.SubItems[ this .ColumnToSort].Text), ConvertHelper.ObjectToDouble(item2.SubItems[ this .ColumnToSort].Text)); if
( this .OrderOfSort == SortOrder.Ascending) { return
num; } if
( this .OrderOfSort == SortOrder.Descending) { return
-num; } return
0; } #endregion } #endregion } } |
原文:http://www.cnblogs.com/shenchao/p/3673218.html