ibatis-user-cs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Riccardo d'Errico" <riccardo.derr...@gmail.com>
Subject Creating a DataTable from an IList returned from the data mapper
Date Sat, 15 Jul 2006 16:55:23 GMT
It arrived to me to have to convert back to a DataTable the IList result I
get from the DataMapper instance.
This is mainly because some asp.net components accepts binding only to
dataset or datatable
Here is the code I use if somebody has the same problem.
This code only works in .Net 2.0
public static DataTable GetDataTableFromIList<T>(List<T> aIList)
{

DataTable _returnTable = new DataTable();

if (aIList.Count>0)

{

//Creates the table structure looping in the in the first element of the
list

object _baseObj = aIList[0];

Type objectType = _baseObj.GetType();

PropertyInfo[] properties = objectType.GetProperties();

DataColumn _col;

foreach (PropertyInfo property in properties)

{

_col = new DataColumn();

_col.ColumnName = (string)property.Name;

_col.DataType = property.PropertyType;

_returnTable.Columns.Add(_col);

}

//Adds the rows to the table

DataRow _row;

foreach (object objItem in aIList)

{

_row = _returnTable.NewRow();

foreach (PropertyInfo property in properties)

{

_row[property.Name] = property.GetValue(objItem, null);

}

_returnTable.Rows.Add(_row);

}

}

return _returnTable;

}

Mime
View raw message