How do you dynamically add columns to a RadGrid?

Will picture Will · Nov 4, 2013 · Viewed 9.3k times · Source

I have a RadGrid with an unknown number of columns I would like to create. Actually I know the first column, which has a DataField of PermissionName. I have a CSLA data source which returns a list of PermissionInfo objects, each of which contains a list of RoleInfo objects. How can I dynamically create a column in the RadGrid for each RoleInfo object when the PermissionInfo objects have varying numbers of RoleInfo objects?

If any PermissionInfo object contains a specific RoleInfo object, I want to create a column with RoleInfo.RoleName as the header and True as the DataValue. If the RoleInfo object is not present, then I would like to have the DataValue = false for that row and column.

Here is my RadGrid:

    <telerik:RadGrid ID="rgPermissions" AllowPaging="false" AllowSorting="true" AutoGenerateColumns="false"
    DataSourceID="dsPermissions" runat="server">
    <MasterTableView DataKeyNames="PermissionId" DataSourceID="dsPermissions" EditMode="InPlace">
        <Columns>
            <telerik:GridBoundColumn DataField="PermissionName" HeaderText="Permission" ></telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
    </telerik:RadGrid>
<csla:CslaDataSource ID="dsPermissions" runat="server" OnSelectObject="dsPermissions_SelectObject">
</csla:CslaDataSource>

Here are the properties in PermissionInfo

    public int PermissionId { get; set; }

    public string PermissionName { get; set; }

    public RoleInfoList Roles { get; set; }

Here are the properties in RoleInfo:

    public int RoleId { get; set; }

    public string RoleName { get; set; }

    public string Title { get; set; }

In my page_load method, I have also written a Factory Method to retrieve all Roles:

RoleInfoList roles = RoleInfoList.GetRoleList();

Answer

Brian Mains picture Brian Mains · Nov 4, 2013

There are several ways; first, you can use the hierarchical grid approach, which Telerik natively supports (see this topic and subtopics). Alternatively, you could "flatten" the results you are trying to bind by doing a LINQ statement, and then binding the anonymous result.

var p in permissions
select new
{
    p.PermissionId,
    p.PermissionName,
    RolesList = String.Join(", ", p.Roles.Select(i => i.RoleName))
}

Note this approach is not LINQ friendly, as Join isn't translated to LINQ.