Skip to content

Instantly share code, notes, and snippets.

@fujin
Forked from anonymous/gist:165179
Created August 10, 2009 13:07
Show Gist options
  • Select an option

  • Save fujin/165181 to your computer and use it in GitHub Desktop.

Select an option

Save fujin/165181 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);
}
}
}
begin
dt = Datatable.new
propertyNames = Array.new
d.ItemsSource.each do |o|
o.GetType.GetProperties.each do |p|
dt.Columns.Add p.Name
propertyNames << p.Name
end unless dt.Columns.Count == 0
dr = dt.NewRow
propertyNames.each { |p| dr[p] = o.GetType.GetProperty(p).GetValue(o, null) }
dt.Rows.Add dr
end if d.ItemsSource.is_a? Enumerable
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment