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

C#DataTableRow列值互转的更多相关文章

  1. SQLServer 自增主键创建, 指定自增主键列值插入数据,插入主键

    http://blog.csdn.net/zh2qiang/article/details/5323981 SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入. 1 ...

  2. excel转化为table(去掉所有列值都为空的值一行,即返回有效值的DataTable)

    /// <summary> /// 去掉所有列值都为空的值一行,即返回有效值的DataTable /// </summary> /// <param name=" ...

  3. Mysql 如何实现列值的合并

    Mysql 如何实现列值的合并 SELECT  GROUP_CONCAT(name SEPARATOR ' ') AS name FROM A

  4. foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值

    http://zhangxiong0301.iteye.com/blog/2242723 最近有个需求,就是使用mybatis时,向mysql中插入数据,其参数为map类型,map里面的key为列名, ...

  5. PIVOT 用于将列值旋转为列名

    PIVOT 用于将列值旋转为列名(即行转列),在 SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT 的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )A ...

  6. jQuery动态对表格Table进行添加或删除行以及修改列值操作

    jQuery,不仅可以以少量的代码做很多操作,而且兼容性好(各种浏览器,各种版本). 下面用jQuery动态对表格Table进行添加或删除行以及修改列值操作 1.jQuery代码 <script ...

  7. GRID用法(取行、列值;定位选中某行等等)

    Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值 cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号 cxGrid1DB ...

  8. BLOB或TEXT字段使用散列值和前缀索引优化提高查询速度

    1.创建表,存储引擎为myisam,对大文本字段blob使用MD5函数建立一个散列值 create table t2(id varchar(60), content blob, hash_value ...

  9. 解决无法获取 GridView 隐藏列值问题

    今天遇到了一个要获取GridView隐藏列值的问题,试了好几种方法,要么获取不到,要么获取到了类列的值也隐藏了,但在样式中这一列会多出一块,,但最后找到了一个功能实现而且实现了列完美隐藏的方法和大家分 ...

  10. 【SQL】Update中使用表别名、如何用表中一列值替换另一列的所有值

    Update中使用表别名 select中的表别名: select * from TableA as ta update中的表别名: update ta from TableA as ta 如何用表中一 ...

随机推荐

  1. JS数据结构与算法-队列结构

    队列结构 一.认识队列 受限的线性结构: 我们已经学习了一种受限的线性结构:栈结构. 并且已经知道这种受限的数据结构对于解决某些特定问题,会有特别的 效果. 下面,我们再来学习另外一个受限的数据结构: ...

  2. 8.drf-序列化器

    在序列化类中,如果想使用request,则可以通过self.context['request']获取 序列化器的主要由两大功能 - 对请求的数据进行校验(底层调用的是Django的Form和Model ...

  3. 这次,听人大教授讲讲分布式数据库的多级一致性|TDSQL 关键技术突破

    近年来,凭借高可扩展.高可用等技术特性,分布式数据库正在成为金融行业数字化转型的重要支撑.分布式数据库如何在不同的金融级应用场景下,在确保数据一致性的前提下,同时保障系统的高性能和高可扩展性,是分布式 ...

  4. 深入理解Golang 闭包,直通面试

    大家好 今天为大家讲解的面试专题是: 闭包. 定义 闭包在计算机科学中的定义是:在函数内部引用了函数内部变量的函数. 看完定义后,我陷入了沉思...确实,如果之前没有接触过闭包或者对闭包不理解的话,这 ...

  5. 自学 TypeScript 第四天,手把手项目搭建

    前言: 学了三天,我们学习了 TS 的基本类型声明,TS 的编译,webpack 打包,其实也就差不多了,剩下的也就一些 类,继承,构造函数,抽象类,泛型一些的,如果都细致的讲可能写好久,感兴趣的可以 ...

  6. OSI传输层TCP与UDP协议、应用层简介、socket模块介绍及代码优化、半连接池的概念

    目录 传输层之TCP与UDP协议 应用层 socket模块 socket基本使用 代码优化 半连接池的概念 传输层之TCP与UDP协议 TCP与UDP都是用来规定通信方式的 通信的时候可以随心所欲的聊 ...

  7. 动态规划篇——DP问题

    动态规划篇--DP问题 本次我们介绍动态规划篇的DP问题,我们会从下面几个角度来介绍: 区间DP 计数DP 树状DP 记忆化搜索 区间DP 我们通过一个案例来讲解区间DP: /*题目展示*/ 题目名: ...

  8. Windows及eclipse常用快捷键-小彤在努力

    Windows快捷键 Ctrl+A:全选 Ctrl+S:保存 Ctrl+Z:撤回 Ctrl+X:剪切 Ctrl+C:复制 Ctrl+V:粘贴 Ctrl+F:查找 Windows+E:打开我的电脑 Al ...

  9. 【Shell案例】【打印指定行用sed、for循环、head和tail配合使用】4、输出第5行的内容

    描述写一个 bash脚本以输出一个文本文件 nowcoder.txt 中第5行的内容. 示例:假设 nowcoder.txt 内容如下:welcometonowcoderthisisshellcode ...

  10. ChatGPT 可以联网了!浏览器插件下载

    Twitter 用户 An Qu 开发了一款新的 Chrome 插件帮助 ChatGPT 上网,安装插件以后 ChatGPT 就可以联!网!了! 简单来说开启插件后,他可以从网上搜索信息,并且根据用户 ...