private static void FillData(PropertyInfo[] propertyInfo, DataTable dataTable, Object objectSource)
{
DataRow dataRow = dataTable.NewRow();
foreach (PropertyInfo pi in propertyInfo)
dataRow[pi.Name] = pi.GetValue(objectSource, null);
dataTable.Rows.Add(dataRow);
}
private static DataTable CreateDataTable(PropertyInfo[] propertyInfo)
{
DataTable dataTable = new DataTable();
DataColumn dataColumn = null;
foreach (PropertyInfo pi in propertyInfo)
{
dataColumn = new DataColumn();
dataColumn.ColumnName = pi.Name;
dataColumn.DataType = pi.PropertyType;
dataTable.Columns.Add(dataColumn);
}
return dataTable;
}
public static DataTable ConvertToDataTable(Object[] array)
{
PropertyInfo[] propertyInfo = array.GetType().GetElementType().GetProperties();
DataTable dataTable = CreateDataTable(propertyInfo);
if (array.Length != 0)
{
foreach (object o in array)
FillData(propertyInfo, dataTable, o);
}
return dataTable;
}
No comments:
Post a Comment