public static DataTable ListToDataTable<T>(List<T> list)
{
DataTable dt = new DataTable();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
Type pt = info.PropertyType;
if (pt.IsGenericType && pt.GetGenericTypeDefinition() == typeof(Nullable<>))
{
pt = Nullable.GetUnderlyingType(pt);
dt.Columns.Add(info.Name, pt);
}
else
dt.Columns.Add(new DataColumn(info.Name, info.PropertyType));
}
foreach (T t in list)
{
DataRow row = dt.NewRow();
foreach (PropertyInfo info in typeof(T).GetProperties())
{
row[info.Name] = info.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
I have worked on no of technologies including C# .Net, VB .Net, ASP.Net, LINQ, WCF, X++, and SharePoint,Oracle,Crystal Reports.
Showing posts with label Dynamic DataTable. Show all posts
Showing posts with label Dynamic DataTable. Show all posts
Sunday, November 28, 2010
Dynamic Create DataTable from Generic List using Reflection
I have few methods that returns different Generic Lists.But main thing here I am using Reflection to convert Generic List into Datatable.
Subscribe to:
Posts (Atom)
PDF Arabic watermark using MVC and iTextSharp
PDF full page Arabic watermark using MVC and iTextSharp Download : Source Code Most of the time we have requirement to gen...
-
Technorati Tags: Create Dynamic Connection with Crystal Report using Asp.net and Oracle Stored Procedure Dynamic crystal report connection...
-
LiveJournal Tags: Encryption Decryption Web.config file , Encryption , Decryption , Web.config The most sensitive information stored in web...
-
PDF full page Arabic watermark using MVC and iTextSharp Download : Source Code Most of the time we have requirement to gen...