List to DataTable : C#


private System.Data.DataTable GetResult(List<vwGetFieldInterviewObject> data)
{
DataTable dt = new DataTable();
dt.TableName = “myTable”;
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(vwGetFieldInterviewObject));
foreach (PropertyDescriptor prop in properties)
dt.Columns.Add(prop.Name, Nullable.GetUnderlyingType(prop.PropertyType) ?? prop.PropertyType);
foreach (var item in data)
{
DataRow row = dt.NewRow();
foreach (PropertyDescriptor prop in properties)
row[prop.Name] = prop.GetValue(item) ?? DBNull.Value;
dt.Rows.Add(row);
}
return dt;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s