0

So I have response data from various API calls. Each of these calls returns a list of objects, and each object could also contain a list of objects. As there are many different object types the method will have to be generic. What I am aiming for is to have a data grid that contains the objects properties, and if the property is another list add a data grid control instead.

public static DataTable GetDataGrid(this IList list)
    {
        var dt = new DataTable();
        foreach (var o in (IEnumerable)list)
        {
            var r = dt.NewRow();
            foreach (var f in o.GetType().GetProperties())
            {
                if (!dt.Columns.Contains(f.Name))
                {
                    dt.Columns.Add(f.Name);
                }
                var value = f.GetValue(o);
                Type t = value.GetType();
                // if it is an integer list just concat the values
                if (value is IList<int?>)
                {
                    r[f.Name] = (String.Join<int?>(",", (IEnumerable<int?>)value));
                }// if it is another object list create a datagrid
                else if (value is IList)
                {
                    // CREATE A DATA GRID
                }// if it is not a list just add the value
                else
                {
                    r[f.Name] = value;
                }

            }
            dt.Rows.Add(r);
        }
        return dt;

    }

This was how far I got, but this is also just a DataTable

EDIT: Example

So I would like the raw data underneath shown in the table. System Memory, Process Memory and GPU are all objects of differing types, but I would like them to be their own tables within the respective cells. If that is not possible could I store a trigger of some kind to open a table.

JBAkroyd
  • 33
  • 6
  • to create a datagrid from a list, are you telling that your grids are 1 column grid, your question does not clarify the structure that you are building with your data. Can you put in some examples – peeyush singh Nov 27 '18 at 00:38
  • Nesting datagrids in cells would be a terrible UI/UX, everyone else uses Treeviews for this "recursive" loading. – Jeremy Thompson Nov 27 '18 at 01:23
  • Is it even a datatable you start with? Why a datagrid? Are your really expecting to edit dynamic types in a dynamic structure? I think building a treeview dynamically might be your best bet here. – Andy Nov 27 '18 at 08:55
  • It start with a list of objects that contain properties and other list of objects. So a tree view would be a better way? – JBAkroyd Nov 27 '18 at 18:37

1 Answers1

1

I decided to use a Web Browser control and convert the datatable to an html table.

Datatable to html Table

 public static string ConvertDataTableToHTML(this DataTable dt)
    {
        string html = "<table>";
        //add header row
        html += "<tr>";
        for (int i = 0; i < dt.Columns.Count; i++)
            html += "<td>" + dt.Columns[i].ColumnName + "</td>";
        html += "</tr>";
        //add rows
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            html += "<tr>";
            for (int j = 0; j < dt.Columns.Count; j++)
                html += "<td>" + dt.Rows[i][j].ToString() + "</td>";
            html += "</tr>";
        }
        html += "</table>";
        return html;
    }
JBAkroyd
  • 33
  • 6