Created
October 8, 2014 17:43
-
-
Save dshook/860074c93abf4d81214c to your computer and use it in GitHub Desktop.
ListToDataTable
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
| public static class ListToDataTable | |
| { | |
| public static JsDataTable ConvertListToJsDataTable<T>(List<T> items) | |
| { | |
| JsDataTable dataTable = new JsDataTable(); | |
| //Get all the properties for the headers | |
| PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance); | |
| //create empty header | |
| var header = new List<JsDataTableRow>() { new JsDataTableRow(){ cells = new List<JsDataTableCell>() } }; | |
| foreach (PropertyInfo prop in Props) | |
| { | |
| //Setting column names as Property names | |
| header.First().cells.Add(new JsDataTableCell() { value = prop.Name }); | |
| } | |
| //set up the rows | |
| var rows = new List<JsDataTableRow>(); | |
| foreach (T item in items) | |
| { | |
| var row = new JsDataTableRow() { cells = new List<JsDataTableCell>() }; | |
| for (int i = 0; i < Props.Length; i++) | |
| { | |
| //inserting property values to datatable rows | |
| row.cells.Add(new JsDataTableCell(){ value = Props[i].GetValue(item, null)}); | |
| } | |
| rows.Add(row); | |
| } | |
| dataTable.headers = header; | |
| dataTable.rows = rows; | |
| return dataTable; | |
| } | |
| } | |
| public class JsDataTable | |
| { | |
| public List<JsDataTableRow> headers { get; set; } | |
| public List<JsDataTableRow> rows { get; set; } | |
| } | |
| public class JsDataTableRow | |
| { | |
| public List<JsDataTableCell> cells { get; set; } | |
| } | |
| public class JsDataTableCell | |
| { | |
| public object value { get; set; } | |
| public string @class { get; set; } | |
| public string rowspan { get; set; } | |
| public string colspan { get; set; } | |
| public string link { get; set; } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment