1 ///
2 /// 把DataRow中的某一列值转换为CheckState类型
3 ///
4 /// 数据行
5 /// 列名
6 ///
7 public static CheckState DataRowToCheckState(this DataRow row, string columnName)
8 {
9 if (!row.Table.Columns.Contains(columnName))
10 {
11 return CheckState.Indeterminate;
12 }
13
14 if (row.IsNull(columnName))
15 {
16 return CheckState.Indeterminate;
17 }
18
19 bool result;
20 if (bool.TryParse(row[columnName].ToString(), out result))
21 {
22 return result ? CheckState.Checked : CheckState.Unchecked;
23 }
24 else
25 {
26 return CheckState.Indeterminate;
27 }
28 }
29
30 ///
31 /// 把DataRow中的某一列值转换为十进制数
32 ///
33 /// 数据行
34 /// 列名
35 ///
36 public static decimal DataRowToDecimal(this DataRow row, string columnName)
37 {
38 if (!row.Table.Columns.Contains(columnName))
39 {
40 return 0M;
41 }
42
43 if (row.IsNull(columnName))
44 {
45 return 0M;
46 }
47
48 decimal result;
49 if (decimal.TryParse(row[columnName].ToString(), out result))
50 {
51 return result;
52 }
53 else
54 {
55 return 0M;
56 }
57 }
58
59 ///
60 /// 把DataRow中的某一列值转换为十进制数
61 ///
62 /// 数据行
63 /// 列名
64 /// 可能为空
65 public static decimal? DataRowToDecimalNull(this DataRow row, string columnName)
66 {
67 if (!row.Table.Columns.Contains(columnName))
68 {
69 return null;
70 }
71
72 if (row.IsNull(columnName))
73 {
74 return null;
75 }
76
77 decimal result;
78 if (decimal.TryParse(row[columnName].ToString(), out result))
79 {
80 return result;
81 }
82 else
83 {
84 return null;
85 }
86 }
87
88 ///
89 /// 把DataRow中的某一列值转换为字符串
90 ///
91 /// 数据行
92 /// 列名
93 ///
94 public static string DataRowToString(this DataRow row, string columnName)
95 {
96 if (!row.Table.Columns.Contains(columnName))
97 {
98 return string.Empty;
99 }
100
101 if (row.IsNull(columnName))
102 {
103 return string.Empty;
104 }
105
106 return row[columnName].ToString();
107 }
108
109 ///
110 /// 把DataRow中的某一列值转换为日期
111 ///
112 /// 数据行
113 /// 列名
114 ///
115 public static DateTime DataRowToDateTime(this DataRow row, string columnName)
116 {
117 if (!row.Table.Columns.Contains(columnName))
118 {
119 return DateTime.Now;
120 }
121
122 if (row.IsNull(columnName))
123 {
124 return DateTime.Now;
125 }
126
127 DateTime result;
128 if (DateTime.TryParse(row[columnName].ToString(), out result))
129 {
130 return result;
131 }
132 else
133 {
134 return DateTime.Now;
135 }
136 }
137
138 ///
139 /// 把DataRow中的某一列值转换为日期
140 ///
141 /// 数据行
142 /// 列名
143 ///
144 public static DateTime? DataRowToDateTimeNull(this DataRow row, string columnName)
145 {
146 if (!row.Table.Columns.Contains(columnName))
147 {
148 return null;
149 }
150
151 if (row.IsNull(columnName))
152 {
153 return null;
154 }
155
156 DateTime result;
157 if (DateTime.TryParse(row[columnName].ToString(), out result))
158 {
159 return result;
160 }
161 else
162 {
163 return null;
164 }
165 }
166
167 ///
168 /// 把DataRow转换为数据字典
169 ///
170 ///
171 ///
172 public static Dictionary<string, object> DataRowToDictionary(this DataRow row)
173 {
174 if (row.Table.Columns.Count > 0)
175 {
176 Dictionary<string, object> dic = new Dictionary<string, object>();
177 for (int i = 0; i < row.Table.Columns.Count; i++)
178 {
179 var columnName = row.Table.Columns[i].ColumnName;
180 dic.Add(columnName, row[columnName]);
181 }
182
183 return dic;
184 }
185
186 return null;
187 }
188
189 ///
190 /// 把DataRow中的某一列值转换为布尔类型
191 ///
192 /// 数据行
193 /// 列名
194 ///
195 public static bool DataRowToBool(this DataRow row, string columnName)
196 {
197 if (!row.Table.Columns.Contains(columnName))
198 {
199 return false;
200 }
201
202 if (row.IsNull(columnName))
203 {
204 return false;
205 }
206
207 bool result;
208 if (bool.TryParse(row[columnName].ToString(), out result))
209 {
210 return result;
211 }
212 else
213 {
214 return false;
215 }
216 }
217 }
218 #endregion
219
220 #region Dictionary的扩展方法
221 ///
222 /// Dictionary的扩展方法
223 ///
224 public static class DictionaryExtensionMethods
225 {
226 ///
227 /// 把Dictionary中的某一值转换为布尔类型
228 ///
229 /// 数据字典
230 /// 列名
231 ///
232 public static CheckState DictionaryToCheckState(this Dictionary<string, object> dic, string key)
233 {
234 if (!dic.ContainsKey(key))
235 {
236 return CheckState.Indeterminate;
237 }
238
239 if (dic[key] == null)
240 {
241 return CheckState.Indeterminate;
242 }
243
244 bool result;
245 if (bool.TryParse(dic[key].ToString(), out result))
246 {
247 return result ? CheckState.Checked : CheckState.Unchecked;
248 }
249 else
250 {
251 return CheckState.Indeterminate;
252 }
253 }
254
255 ///
256 /// 把Dictionary中的某一值转换为十进制数
257 ///
258 /// 数据字典
259 /// 列名
260 ///
261 public static decimal DictionaryToDecimal(this Dictionary<string, object> dic, string key)
262 {
263 if (!dic.ContainsKey(key))
264 {
265 return 0M;
266 }
267
268 if (dic[key] == null)
269 {
270 return 0M;
271 }
272
273 decimal result;
274 if (decimal.TryParse(dic[key].ToString(), out result))
275 {
276 return result;
277 }
278 else
279 {
280 return 0M;
281 }
282 }
283
284 ///
285 /// 把Dictionary中的某一值转换为字符串
286 ///
287 /// 数据字典
288 /// 列名
289 ///
290 public static string DictionaryToString(this Dictionary<string, object> dic, string key)
291 {
292 if (!dic.ContainsKey(key))
293 {
294 return string.Empty;
295 }
296
297 if (dic[key] == null)
298 {
299 return string.Empty;
300 }
301
302 return dic[key].ToString();
303 }
304
305 ///
306 /// 把Dictionary中的某一值转换为日期
307 ///
308 /// 数据字典
309 /// 列名
310 ///
311 public static DateTime DictionaryToDateTime(this Dictionary<string, object> dic, string key)
312 {
313 if (!dic.ContainsKey(key))
314 {
315 return DateTime.Now;
316 }
317
318 if (dic[key] == null)
319 {
320 return DateTime.Now;
321 }
322
323 DateTime result;
324 if (DateTime.TryParse(dic[key].ToString(), out result))
325 {
326 return result;
327 }
328 else
329 {
330 return DateTime.Now;
331 }
332 }
333 }
334 #endregion
335
336 #region 表格GridView的扩展方法
337 ///
338 /// 表格GridView的扩展方法
339 ///
340 public static class GridViewExtensionMethods
341 {
342 ///
343 /// 导出DevExpress表格
344 ///
345 /// 文件名
346 public static void ExportToExcel(this DevExpress.XtraGrid.Views.Grid.GridView view, string fileName)
347 {
348 SaveFileDialog saveDlg = new SaveFileDialog();
349 saveDlg.Filter = "Excel 2007文件|*.xlsx|Excel 99-03|*.xls";
350 saveDlg.FileName = fileName;
351 if (saveDlg.ShowDialog() == DialogResult.OK)
352 {
353 if (saveDlg.FilterIndex == 1)
354 {
355 view.ExportToXlsx(saveDlg.FileName);
356 }
357 else if (saveDlg.FilterIndex == 2)
358 {
359 view.ExportToXls(saveDlg.FileName);
360 }
361 }
362 }
363 }
364 #endregion