using System;using System.Collections;
using System.Data;using System.Reflection;
using System.Collections.Generic;
using System.Text;
public static class Util
{
public static DataTable ConvertArrayListToDataTable(ArrayList arrayList)
{
DataTable dt = new DataTable();
if (arrayList.Count != 0)
{
dt = ConvertObjectToDataTableSchema(arrayList[0]);
FillData(arrayList, dt);
}
return dt;
}
public static DataTable ConvertObjectToDataTableSchema(Object o)
{
DataTable dt = new DataTable();
PropertyInfo[] properties = o.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
DataColumn dc = new DataColumn(property.Name);
dc.DataType = property.PropertyType; dt.Columns.Add(dc);
}
return dt;
}
private static void FillData(ArrayList arrayList, DataTable dt)
{
foreach (Object o in arrayList)
{
DataRow dr = dt.NewRow();
PropertyInfo[] properties = o.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
dr[property.Name] = property.GetValue(o, null);
}
dt.Rows.Add(dr);
}
}
}
Thursday, May 15, 2008
Subscribe to:
Post Comments (Atom)
Check This Out!
More Links to Good Information
- December (1)
- October (1)
- January (1)
- September (1)
- February (2)
- January (2)
- May (3)
- February (1)
- May (1)
- October (1)
- January (1)
- August (1)
- March (1)
- May (1)
- March (1)
- January (1)
- March (1)
- December (2)
- September (2)
- June (1)
- February (1)
- January (1)
- October (1)
- December (2)
- November (1)
- August (4)
- July (14)
- June (10)
- May (9)
- April (2)
- February (4)
- January (2)
- December (7)
- October (10)
7 comments:
Thanks a lot. Precisely what i was looking for. Instead of myself creating the code myself I could just 'borrow' your solution.
your code doesnt work for me!
error in converting
Thanks for the code snippet. Very useful indeed.
Thank you for this code.
Thanks for share you code.It helps a lot.
thank a lot
tks
Post a Comment