Hello guys,
In this article i have posted extension method for conversion generics List to datatable i have used in one of my business logic.
In ths code we PropertyDescriptorCollection class for geberic description.In this article i have posted extension method for conversion generics List to datatable i have used in one of my business logic.
public static class Cls
{
public static DataTable ToDataTable<T>(this IList<T> data)
{
PropertyDescriptorCollection props =
TypeDescriptor.GetProperties(typeof (T));
var table = new DataTable();
for (int i = 0; i < props.Count; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
var values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
we can also this task with the help of reflection but i have work this code only once so i don't bother more.
Thanks
- Rahul
No comments:
Post a Comment