Showing posts with label Convert To DataTable. Show all posts
Showing posts with label Convert To DataTable. Show all posts

Thursday, April 29, 2010

Object Array To DataTable

#region Object Array to DataTable

/// Method to Convert Datatable from object Array.

public static DataTable ToDatatable(this Object[] array)

{

PropertyInfo[] properties = array.GetType().GetElementType().GetProperties();

DataTable dt = CreateDataTable(properties);

if (array.Length != 0)

{

foreach (object o in array)

FillData(properties, dt, o);

}

return dt;

}




/// Method To Create total column of datatable.

private static DataTable CreateDataTable(PropertyInfo[] properties)

{

DataTable dt = new DataTable();

DataColumn dc = null;

foreach (PropertyInfo pi in properties)

{

dc = new DataColumn();

dc.ColumnName = pi.Name;

//dc.DataType = pi.PropertyType;

dt.Columns.Add(dc);

}

return dt;

}



/// Method for Fill data in DataTable.


private static void FillData(PropertyInfo[] properties, DataTable dt, Object o)

{

DataRow dr = dt.NewRow();

foreach (PropertyInfo pi in properties)

{

dr[pi.Name] = pi.GetValue(o, null);

}

dt.Rows.Add(dr);

}

#endregion

List To DataTable

#region List To DataTable

public static DataTable ToDataTable(this List items)

{

var tb = new DataTable(typeof(T).Name);

PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo prop in props)

{

Type t = GetCoreType(prop.PropertyType);

tb.Columns.Add(prop.Name, t);

}

foreach (T item in items)

{

var values = new object[props.Length];

for (int i = 0; i <>

{

values[i] = props[i].GetValue(item, null);

}

tb.Rows.Add(values);

}

return tb;

}



///

/// Determine of specified type is nullable

///

public static bool IsNullable(Type t)

{

return !t.IsValueType || (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>));

}



///

/// Return underlying type if type is Nullable otherwise return the type

///

public static Type GetCoreType(Type t)

{

if (t != null && IsNullable(t))

{

if (!t.IsValueType)

{

return t;

}

else

{

return Nullable.GetUnderlyingType(t);

}

}

else

{

return t;

}

}

#endregion

IEnumerable to DataTable


public static DataTable ToDataTable(this IEnumerable ien)
{
DataTable dt = new DataTable();
foreach (object obj in ien)
{
Type t = obj.GetType();
PropertyInfo[] pis = t.GetProperties();
if (dt.Columns.Count == 0)
{
foreach (PropertyInfo pi in pis)
{
  
Type propType = pi.PropertyType;

                    // Is it a nullable type? Get the underlying type
                    if (propType.IsGenericType && propType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
                     
   propType = new System.ComponentModel.NullableConverter(propType).UnderlyingType;

                    dt.Columns.Add(pi.Name, propType);

}
}
DataRow dr = dt.NewRow();
foreach (PropertyInfo pi in pis)
{
object value = pi.GetValue(obj, null);
dr[pi.Name] = value;
}
dt.Rows.Add(dr);
}
return dt;
}