using System.Collections.Generic;
using System.Web.UI.WebControls;
public static void Main()
List<Medication> meds = new List<Medication>();
var nameArray = new string[]{"Jeff", "Nate", "Anyssa", "Logan", "Dan", "Ryan", "Daniel", "Carolina", "Doug", "Dana"};
for(int i = 0; i < 10; i++)
meds.Add(new Medication(){SourceInstanceId = "UniqueMedId_" + i, SourceVisitId = "VisitId_" + i, MedicationName = "TestMedName", PatientName = nameArray[i] });
Console.Write(CreateTextNarrative<Medication>(meds));
public static string CreateTextNarrative<TItem>(List<TItem> tableRowData) where TItem : new()
var tableHeaderRow = new TableHeaderRow();
var itemProperties = typeof(TItem).GetProperties(BindingFlags.Public | BindingFlags.Instance).OrderBy(x => x.Name).ToArray();
foreach (PropertyInfo propertyInfo in itemProperties)
tableHeaderRow.Cells.Add(new TableHeaderCell { Text = propertyInfo.Name });
table.Rows.Add(tableHeaderRow);
foreach (var record in tableRowData)
var tableRow = new TableRow();
foreach (PropertyInfo propertyInfo in itemProperties)
var cell = new TableCell();
cell.Text = (string)propertyInfo.GetValue(record, null);
tableRow.Cells.Add(cell);
table.Rows.Add(tableRow);
using (var sw = new StringWriter())
table.RenderControl(new HtmlTextWriter(sw));
public string SourceInstanceId{get;set;}
public string SourceVisitId{get;set;}
public string MedicationName{get;set;}
public string PatientName{get;set;}