c# Nested grid loads multiple times

121 views Asked by At

I am using telerik radgrid. I am binding the grid programatically. The grid loads correctly but the Nested grid loads multiple times.(the number of times the number of columns in nested grid). Below is my code

GridTableView tableViewOrders = new GridTableView(grid);
foreach (Application app in isParentApp)
{
    DataTable tbl = new DataTable("nestedTable_" + app.AppId);
    objGridList = GetGridList(app.AppId);
    foreach (var nestedRow in objGridList)
    {
       GridRelationFields relationFields = new GridRelationFields();
            relationFields.MasterKeyField = "AppId";
            relationFields.DetailKeyField = "AppId";
            tableViewOrders.ParentTableRelation.Add(relationFields);
            GridBoundColumn boundColumn = new GridBoundColumn();
            boundColumn.DataField = nestedRow.ColName;
            boundColumn.HeaderText = nestedRow.ColName;
            tableViewOrders.Columns.Add(boundColumn);
            grid.MasterTableView.DetailTables.Add(tableViewOrders);
            tbl.Columns.Add(nestedRow.ColName);
    }
foreach(var rows in totalRows)
 {
        DataRow nestedDtRow = tbl.NewRow();
        nestedDtRow["AppId"] = app.AppId;
    foreach (var nestedRecord in nestedRecords)
    {
        nestedDtRow[nestedRecord.colName] = nestedRecord.Data;
    }
      tbl.Rows.Add(nestedDtRow);
 }
   tableViewOrders.DataSource = tbl;

}

While debugging "tbl" had only one table but the output displayed multiple tables.

        `
1

There are 1 answers

6
CodingYoshi On BEST ANSWER

You are Adding the columns over and over for each row in this loop:

foreach (var nestedRow in objGridList)
{
} 

You only need to do this once outside the loop. See this answer on how to do add the columns outside the loop.