Hi,
Here
Data in the list is converted into Datatable buy using Generic DataType as
<T>, in Some cases list can’t perform all the operations like grid in
Such Cases we need to transfer data from List to Datatable with the code below.
public DataTable ListToDataTable<T>(List<T>
items)
{
DataTable dataTable =
new DataTable(typeof(T).Name);
PropertyInfo[] Properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (PropertyInfo propInfo in Properties)
{
dataTable.Columns.Add(propInfo.Name);
}
foreach (T item in items)
{
var values = new object[Properties.Length];
for (int i = 0; i < Properties.Length; i++)
{
values[i] = Properties[i].GetValue(item, null);
}
dataTable.Rows.Add(values);
}
return dataTable;
}private void Testing()
{
List<Model> listTest = data.getData(2002);//this getData() will fill my list
DataTable table = ListToDataTable(listTest);
}