1.
如果在网上搜一下.Net把List转换为DataTable,基本上都搜出类似如下一段代码:
DataTable dt = new DataTable();
2 if (_list != null)
3 {
//通过反射获取list中的字段
4 System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties();
5 foreach (System.Reflection.PropertyInfo pi in p)
6 {
7 dt.Columns.Add(pi.Name, System.Type.GetType(pi.PropertyType.ToString()));
8 }
9 for (int i = 0; i < _list.Count; i++)
10 {
11 IList TempList = new ArrayList();
12 //将IList中的一条记录写入ArrayList
13 foreach (System.Reflection.PropertyInfo pi in p)
14 {
15 object oo = pi.GetValue(_list[i], null);
16 TempList.Add(oo);
17 }
18 object[] itm = new object[p.Length];
19 for (int j = 0; j < TempList.Count; j++)
20 {
21 itm.SetValue(TempList[j], j);
22 }
23 dt.LoadDataRow(itm, true);
}
24 }
这段代码的思路是没有问题;但是实际运行会有问题;因为;
System.Reflection.PropertyInfo[] p = _list[0].GetType().GetProperties();
对于System.Int32类型,不会返回内容;p为空;不会添加列;
对于System.String类型,返回的值是2个;
2 做一个小程序来说明;
全部代码;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace GetTypeGetPropertys
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
PropertyInfo[] myPropertyInfo;
// Get the properties of ‘Type’ class object.
myPropertyInfo = Type.GetType(“System.Type”).GetProperties();
for (int i = 0; i < myPropertyInfo.Length; i++)
{
//Console.WriteLine(myPropertyInfo[i].ToString());
listBox1.Items.Add(myPropertyInfo[i].ToString());
}
PropertyInfo[] props = Type.GetType(“System.Int32”).GetProperties();
foreach (PropertyInfo prop in props)
{
listBox2.Items.Add(prop.ToString());
}
PropertyInfo[] props2 = Type.GetType(“System.String”).GetProperties();
for (int i = 0; i < props2.Length; i++)
{
listBox3.Items.Add(props2[i].ToString());
}
}
}
}
界面和运行效果:
上述三个列表框返回的分别是System.Type、System.Int32、System.String调用GetProperties()返回的结果;微软对于GetProperties()方法的返回值有如下说明:
返回值
类型:
System.Reflection.PropertyInfo
[]
表示当前
Type
的所有公共属性的
PropertyInfo
对象数组。
– 或 –
如果当前
Type
没有公共属性,则为
PropertyInfo
类型的空数组。
实现
3 所以现在要绑定一个List到DataGridView,代码只能如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace dataGridDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<int> li = new List<int>();
//List<string> li = new List<string>();
for (int i = 0; i < 32; i++)
{
//li.Add(i.ToString());
li.Add(i);
}
DataTable tb = new DataTable();
tb = ToDataTable(li);
dataGridView1.DataSource = tb;
}
private DataTable ToDataTable<T>(List<T> items)
{
var tb = new DataTable(typeof(T).Name);
//PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
//PropertyInfo[] props = typeof(T).GetProperties();
PropertyInfo[] props = items[0].GetType().GetProperties();
//PropertyInfo[] props = Type.GetType(“System.Int32”).GetProperties();
if (props.Count() == 0)
{
tb.Columns.Add(typeof(T).Name, typeof(T));
}
else
{
foreach (PropertyInfo prop in props)
{
Type t = GetCoreType(prop.PropertyType);
tb.Columns.Add(prop.Name, t);
}
}
foreach (T item in items)
{
if (props.Count() == 0)
{
var values = new object();
for (int i = 0; i < 32; i++)
{
//values = item.GetValue();
values=i;
tb.Rows.Add(values);
}
}
else
{
var values = new object[props.Length];
for (int i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}
}
return tb;
}
/// <summary>
/// Determine of specified type is nullable
/// </summary>
public static bool IsNullable(Type t)
{
return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));
}
/// <summary>
/// Return underlying type if type is Nullable otherwise return the type
/// </summary>
public static Type GetCoreType(Type t)
{
if (t != null && IsNullable(t))
{
if (!t.IsValueType)
{
return t;
}
else
{
return Nullable.GetUnderlyingType(t);
}
}
else
{
return t;
}
}
}
}
运行结果;