Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
415
How to add Hierarchical Grid in a Report
posted

I have a generate a PDF report from data I am getting from web service. Based on some certain conditions, the data can be nested where schema is changed in both tables. The problem is when I try to add the nested table, it basically creates 2 set of tables, First it adds the main table and below this table, all child table rows are added together. My code is as:

//add cell for row
for (var i = 0; i < props.Length; i++)
{
if (props[i].Name == "ChildGrid" && gridCols[i].ChildGridColumns.Length > 0)
{
var childGrid = item?.GetType().GetProperty("ChildGrid")?.GetValue(item, null);
if (childGrid != null)
{
IEnumerable lst = ((IEnumerable)childGrid).Cast<object>().ToList();
AddChildGridToReport(lst, gridCols[i].ChildGridColumns);
}
}
else
{
var dt = props[i].GetValue(item, null);

//update class code with new value
if (ReportFor == "Consist/Inventory" && props[i].Name == "ClassCode")
clsCode = dt?.ToString();

AddGridRowCell(tableRow, props[i], dt?.ToString(), gridCols[i], cellPattern);
}
}


private void AddChildGridToReport(object itemList, ColumnProperties[] gc = null)
{
//table
IGrid table = section.AddGrid();
table.Borders = bordersStyle;
table.Width = new RelativeWidth(100);

IGridRow tableRow;
Background cellBgColor = new Background(Colors.White);
Background cellAltBgColor = new Background(new Color(252, 252, 252));
int j = 0;

IGridHeader header = table.Header;
header.Repeat = true;
for (var i = 0; i < gc.Length; i++)
{
IGridColumn gridCol = table.AddColumn();
gridCol.Width = new RelativeWidth(gc[i].Width);

AddGridHeader(header, gc[i]);
}

//define cell pattern/style
GridCellPattern cellPattern = new GridCellPattern()
{
Paddings = cellPaddings,
Borders = bordersStyle,
};
cellPattern.Alignment.Vertical = Alignment.Middle;

//convert object to IEnumerable
var items = (IEnumerable)itemList;
foreach (var item in items)
{

PropertyInfo[] props = item.GetType().GetProperties();

tableRow = table.AddRow();
cellPattern.Background = (j % 2 == 0) ? cellBgColor : cellAltBgColor;

for (var i = 0; i < props.Length; i++)
{
//AddRowCell(tableRow, cellBackgroundColor, gc[i], props[i]);
var dt = props[i].GetValue(item, null);
AddGridRowCell(tableRow, props[i], dt.ToString(), gc[i], cellPattern);
}
j++;
}
}

Parents Reply Children