I want to design a table user control and dynamically add the properties of custom objects. I want to populate this table with a collection object with a generic type. How can I do that ? I can not send the properties of the class to PopulateTable with its custom headers.
What I have so far (in a usercontrol) is..
<table class="datatable">
<thead>
<asp:Literal id="ltrlHeaders" runat="server" />
</thead>
<tbody>
<asp:Literal runat="server" ID="ltrlContent" />
</tbody>
</table>
public void PopulateTable(? myCollection)
{
List<string> headers = new List<string>();
FieldInfo[] fields = type.GetFields();
foreach (FieldInfo f in fields)
{
headers.Add(f.Name);
}
// headers
StringBuilder headerString = new StringBuilder();
headerString.Append("<thead>");
headers.ForEach(delegate(string h)
{
headerString.Append(String.Format("<th>{0}</th>", h));
});
headerString.Append("</thead>");
ltrlHeaders.Text = headerString.ToString();
StringBuilder contentString = new StringBuilder();
foreach (var item in list)
{
contentString.Append("<tr>");
foreach (string fieldInfo in headers)
{
contentString.Append(String.Format("<td>{0}</td>", type.GetField(fieldInfo).GetValue(item) as string));
}
contentString.Append("</tr>");
}
ltrlContent.Text = contentString.ToString();
}
Assuming that
Tobjects can be cast tostring. Or you might want:if you wanted to be able to pass a collection and know something about its interface.