C#DataTableRow列值互转
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 (!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 /// <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 (!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 /// <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 (!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 /// <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 (!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 /// <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 (!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 /// <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 (!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 /// <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 (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 /// <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 (!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<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 (!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 /// <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 (!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 /// <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 (!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 /// <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 (!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 /// <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 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
C#DataTableRow列值互转的更多相关文章
- SQLServer 自增主键创建, 指定自增主键列值插入数据,插入主键
http://blog.csdn.net/zh2qiang/article/details/5323981 SQLServer 中含自增主键的表,通常不能直接指定ID值插入,可以采用以下方法插入. 1 ...
- excel转化为table(去掉所有列值都为空的值一行,即返回有效值的DataTable)
/// <summary> /// 去掉所有列值都为空的值一行,即返回有效值的DataTable /// </summary> /// <param name=" ...
- Mysql 如何实现列值的合并
Mysql 如何实现列值的合并 SELECT GROUP_CONCAT(name SEPARATOR ' ') AS name FROM A
- foreach属性-动态-mybatis中使用map类型参数,其中key为列名,value为列值
http://zhangxiong0301.iteye.com/blog/2242723 最近有个需求,就是使用mybatis时,向mysql中插入数据,其参数为map类型,map里面的key为列名, ...
- PIVOT 用于将列值旋转为列名
PIVOT 用于将列值旋转为列名(即行转列),在 SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT 的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )A ...
- jQuery动态对表格Table进行添加或删除行以及修改列值操作
jQuery,不仅可以以少量的代码做很多操作,而且兼容性好(各种浏览器,各种版本). 下面用jQuery动态对表格Table进行添加或删除行以及修改列值操作 1.jQuery代码 <script ...
- GRID用法(取行、列值;定位选中某行等等)
Delphi Cxgrid获取选中行列,排序规则,当前正在编辑的单元格内的值 cxGrid1DBTableView1.Controller.FocusedRowIndex 当前行号 cxGrid1DB ...
- BLOB或TEXT字段使用散列值和前缀索引优化提高查询速度
1.创建表,存储引擎为myisam,对大文本字段blob使用MD5函数建立一个散列值 create table t2(id varchar(60), content blob, hash_value ...
- 解决无法获取 GridView 隐藏列值问题
今天遇到了一个要获取GridView隐藏列值的问题,试了好几种方法,要么获取不到,要么获取到了类列的值也隐藏了,但在样式中这一列会多出一块,,但最后找到了一个功能实现而且实现了列完美隐藏的方法和大家分 ...
- 【SQL】Update中使用表别名、如何用表中一列值替换另一列的所有值
Update中使用表别名 select中的表别名: select * from TableA as ta update中的表别名: update ta from TableA as ta 如何用表中一 ...
随机推荐
- 云实例初始化工具cloud-init源码分析
源码分析 代码结构 cloud-init的代码结构如下: cloud-init ├── bash_completion # bash自动补全文件 │ └── cloud-init ├── Chan ...
- day02-实现01
实现01 1.实现任务阶段1 编写mytomcat,该服务器能给浏览器返回"你好,我是服务器!"的简单信息. 根据之前的tomcat框架整体分析,我们将浏览器发送请求,tomcat ...
- xmind下载安装破解版激活教程思维导图软件获取
1.xmind下载解压压缩包就可以看到里面的文件,然后双击安装文件就可以开始安装了 2.安装Xmind程序双击之后会出现下面的流程,照着截图操作,不要乱点哈 切记切记!!这一步直接点击next,不要修 ...
- java 分别获取当前时间的年月日以及当前时间所在周的周一周末日期
以前也经常用date去截取,但是病史所有场景都适合,或者说效率满足不了,或者说拼接格外麻烦.能用java本省的的方法去实现其实更爽.因为中西方的文化的差异有时候在简单的方法上我们不得不去加一些其他的去 ...
- vue阻止向上和向下冒泡
阻止向下冒泡 <div class="content" @click.self="cancelFunc"></div> 阻止向上冒泡 & ...
- 第2-4-8章 规则引擎Drools实战(1)-个人所得税计算器
目录 9. Drools实战 9.1 个人所得税计算器 9.1.1 名词解释 9.1.2 计算规则 9.1.2.1 新税制主要有哪些变化? 9.1.2.2 资较高人员本次个税较少,可能到年底扣税增加? ...
- 如何通过Java应用程序压缩PDF文档
PDF文档是我们日常办公中使用最频繁的文档格式.但因为大多数PDF文档都包含很多页面图像或大量图片,这就导致PDF文档过大,处理起来较为麻烦.PDF文件过大,就会导致传输或者下载的速度变慢,也会增加传 ...
- 网络爬虫之requests模块,自动办公领域之openpyx模块
一.第三方模块的下载与使用 第三方模块:别人写的模块,一般情况下功能都特别强大 我们如果想使用第三方模块,第一次必须先下载,后面才可以反复使用(等同于内置模块) 下载第三方模块的方式 1. pi ...
- 从面试题入手,畅谈 Vue 3 性能优化
前言 今年又是一个非常寒冷的冬天,很多公司都开始人员精简.市场从来不缺前端,但对高级前端的需求还是特别强烈的.一些大厂的面试官为了区分候选人对前端领域能力的深度,经常会在面试过程中考察一些前端框架的源 ...
- 4、PageHelper分页查询
1.MyBatis的分页方式: 逻辑分页与物理分页 1.逻辑分页:使用MyBatis自带的RowBounds进行分页,是一次性查询很多数据,然后再在结果中检索分页的数据.这样做弊端是需要消耗大量的内存 ...