-
-
Save fujin/165181 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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