Skip to content

Instantly share code, notes, and snippets.

Created August 10, 2009 13:03
Show Gist options
  • Select an option

  • Save anonymous/165179 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/165179 to your computer and use it in GitHub Desktop.
try
{
DataTable dt = new DataTable();
if (d.ItemsSource is IEnumerable)
{
IEnumerable items = d.ItemsSource as IEnumerable;
List<string> propertyNames = new List<string>();
foreach (object o in items)
{
if (dt.Columns.Count == 0)
foreach (PropertyInfo p in o.GetType().GetProperties())
{
dt.Columns.Add(p.Name);
propertyNames.Add(p.Name);
}
DataRow dr = dt.NewRow();
foreach (string p in propertyNames)
dr[p] = o.GetType().GetProperty(p).GetValue(o, null);
dt.Rows.Add(dr);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment