Reputation: 87
I want to covert a treeview from a model in navisworks and create a datatable. I tried to use foreach but no luck.
any help would be Appreciated
Upvotes: 1
Views: 247
Reputation: 87
I resolved my problem by getting the Descendants and passed this into an IEnumerable
var Descendants = PL.oDoc.Models.First.RootItem.Descendants.Select(x => x);
and then with this method I convert that into an datatable:
public static DataTable DataTable<T>(this IEnumerable<T> items)
{
var tb = new DataTable(typeof(T).Name);
PropertyInfo[] props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var prop in props)
{
tb.Columns.Add(prop.Name, prop.PropertyType);
}
foreach (var item in items)
{
var values = new object[props.Length];
for (var i = 0; i < props.Length; i++)
{
values[i] = props[i].GetValue(item, null);
}
tb.Rows.Add(values);
}
return tb;
}
Upvotes: 1