其他类型转化成DataTable
来源:互联网 发布:js点击按钮退出全屏 编辑:程序博客网 时间:2024/05/22 06:32
class Program{ static void Main(string[] args) { List<Student> list = new List<Student>(); list.Add(new Student() { ID = 1, Name = "Fabre", Sex = true, Age = 27, Birthday = DateTime.Now }); string[] strTitle = new string[] { "ID", "Name", "Sex", "Age", "Birthday" }; DataTable dt = GetDataTable(list, strTitle); } public static DataTable GetDataTable(dynamic objects, string[] strFields) { DataTable dt = new DataTable(); bool isFirst = true; foreach (var obj in objects) { DataRow dr = dt.NewRow(); foreach (string item in strFields) { if (isFirst) { dt.Columns.Add(item);//创建列 } dr[item] = GetPropertyValue(obj, item);//单元格赋值 } isFirst = false;//第一次创建表头,往后都不用创建了,用一个标识处理 dt.Rows.Add(dr); } return dt; } /// <summary> /// 获取一个实例指定的属性值 /// </summary> /// <param name="info">object对象</param> /// <param name="field">属性名称</param> /// <returns></returns> public static object GetPropertyValue(object info, string field) { if (info == null) { return null; } Type t = info.GetType(); IEnumerable<System.Reflection.PropertyInfo> property = from pi in t.GetProperties() where pi.Name.ToLower() == field.ToLower() select pi; if (property.Count() < 1) { return ""; } return property.First().GetValue(info, null); }}public class Student{ public int ID { get; set; } public string Name { get; set; } //true 男 false 女 public bool Sex { get; set; } public int Age { get; set; } public DateTime Birthday { get; set; }}
阅读全文
0 0
- 其他类型转化成DataTable
- CString和其他类型转化
- 其他类型转化为json
- 将DataView转化成DataTable
- 将DataView转化成DataTable
- 接到字符串转化成datatable
- DataRow 数组转化成DataTable
- DataRow 数组转化成DataTable
- QString 到其他类型的转化
- 【字符串到其他类型转化】stringstream用法
- c/c++string 转化其他类型
- Cstring 和其他类型相互转化
- 实体类与DataTable的转化(适用任意类型)
- C#中将DataTable类型转化为泛型集合
- string[] 转化成其他数组
- 将Datatable转化成json发送前台
- 将datatable数据转化成list
- Java中String转化为其他类型方法汇总
- hdu 1063 Exponentiation
- hdu 1002 A+B
- c++设计模式之桥接模式
- Go 编译sqlite3 遇到的问题 + 加快go IDE编译速度
- hdu 1753 大明A+B
- 其他类型转化成DataTable
- TortoiseSVN文件夹及文件图标不显示解决方法(转发)
- zend studio 10破解/汉化(转发)
- hdu 2553 N皇后问题
- PowerDesigner
- hdu1028 Ignatius and the Princess III
- Studio快捷键
- hdu 1426 Sudoku Killer
- shell编程笔记