飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

C#DataTableRow列值互转

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

标签:方法编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。