现在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请各位大神指教!
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请各位大神指教!
解决方案 »
- RadioButtonList 清空和移除怎么用啊 为什么我每加载一下项都累加呢
- 关于WCF的问题
- 任意输入字符串,编程将其反方向输出
- 要文本框中实现类似VS的点出功能.大家有什么好的算法
- .net中的数组类Array有何用途,我们平时用的是针对某种数据类型(如int),用int[]定义一维整型数组或用int[,]定义二维整型数组不就行了,为什么又出现一个莫名其妙的Array类,真把人给搞混了。
- 有关数组的问题
- 弱智编程 要求代码最少
- 请思归和大家来看看 很简单的问题
- 奇怪!!我在c#的应用程序里用了e.Cancel=true,为什么不能关机?
- 用什么方法可以得到一个硬盘的总共空间,已经使用的空间,和剩余空间?
- 关于data.read的问题,谢谢
- winform 多线程如何刷新控件,在线等.....
{
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;
}
}
/// 将集合类转换成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;
}