List<int[]>怎么转化为DataTable型
现在List<int[]> list=new List<int[]>且list有值,为了说的比较明白,现把每条值以下列方式显示出来:
list[0][0]='a0'
list[0][1]='a1'
list[0][2]='a2'
list[1][0]='b0'
list[1][1]='b1'
list[1][2]='b2'
现要求转为DataTable型,显示如下:
a0,a1,a2
b0,b1,b2
请各位大神指教! --------------------编程问答-------------------- 双层for循环 --------------------编程问答--------------------
public class ListToDataTable--------------------编程问答--------------------
{
public static DataTable convert2Table<Object>(List<Object> list)
{
DataTable table = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] properties = list[0].GetType().GetProperties();
List<string> columns = new List<string>();
foreach (PropertyInfo pi in properties)
{
table.Columns.Add(pi.Name);
columns.Add(pi.Name);
}
foreach (Object item in list)
{
object[] cells = getValues(columns, item);
table.Rows.Add(cells);
}
}
return table;
}
private static object[] getValues(List<string> columns, object instance)
{
object[] ret = new object[columns.Count];
for (int n = 0; n < ret.Length; n++)
{
PropertyInfo pi = instance.GetType().GetProperty(columns[n]);
object value = pi.GetValue(instance, null);
ret[n] = value;
}
return ret;
}
}
/// <summary>--------------------编程问答-------------------- --------------------编程问答--------------------
/// 将集合类转换成DataTable
/// </summary>
/// <param name="list">集合</param>
/// <returns></returns>
public static DataTable ToDataTable(IList list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
/// <summary>
/// 将泛型集合类转换成DataTable
/// </summary>
/// <typeparam name="T">集合项类型</typeparam>
/// <param name="list">集合</param>
/// <returns>数据集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list)
{
return ConvertX.ToDataTable<T>(list, null);
}
/// <summary>
/// 将泛型集合类转换成DataTable
/// </summary>
/// <typeparam name="T">集合项类型</typeparam>
/// <param name="list">集合</param>
/// <param name="propertyName">需要返回的列的列名</param>
/// <returns>数据集(表)</returns>
public static DataTable ToDataTable<T>(IList<T> list, params string[] propertyName)
{
List<string> propertyNameList = new List<string>();
if (propertyName != null)
propertyNameList.AddRange(propertyName);
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == 0)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
else
{
if (propertyNameList.Contains(pi.Name))
result.Columns.Add(pi.Name, pi.PropertyType);
}
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
if (propertyNameList.Count == 0)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
else
{
if (propertyNameList.Contains(pi.Name))
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
}
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
给力 --------------------编程问答-------------------- 各位都亲自测试过吗?
补充:.NET技术 , C#