简体   繁体   English

如何动态将列添加到RadGrid?

[英]How do you dynamically add columns to a RadGrid?

I have a RadGrid with an unknown number of columns I would like to create. 我有一个RadGrid,但我想创建的列数未知。 Actually I know the first column, which has a DataField of PermissionName . 实际上我知道第一列,它具有PermissionName的DataField。 I have a CSLA data source which returns a list of PermissionInfo objects, each of which contains a list of RoleInfo objects. 我有一个CSLA数据源,该数据源返回PermissionInfo对象的列表,每个对象都包含RoleInfo对象的列表。 How can I dynamically create a column in the RadGrid for each RoleInfo object when the PermissionInfo objects have varying numbers of RoleInfo objects? 当PermissionInfo对象具有不同数量的RoleInfo对象时,如何在RadGrid中为每个RoleInfo对象动态创建一列?

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. 如果任何 PermissionInfo对象包含特定的RoleInfo对象,我想创建一个列,其中RoleInfo.RoleName作为标题,而True作为DataValue。 If the RoleInfo object is not present, then I would like to have the DataValue = false for that row and column. 如果不存在RoleInfo对象,那么我希望该行和列的DataValue = false。

Here is my RadGrid: 这是我的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 这是PermissionInfo中的属性

    public int PermissionId { get; set; }

    public string PermissionName { get; set; }

    public RoleInfoList Roles { get; set; }

Here are the properties in RoleInfo : 这是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: page_load方法中,我还编写了一个Factory方法来检索所有Roles:

RoleInfoList roles = RoleInfoList.GetRoleList();

There are several ways; 有几种方法。 first, you can use the hierarchical grid approach, which Telerik natively supports ( see this topic and subtopics ). 首先,您可以使用Telerik原生支持的分层网格方法( 请参阅本主题和子主题 )。 Alternatively, you could "flatten" the results you are trying to bind by doing a LINQ statement, and then binding the anonymous result. 或者,您可以通过执行LINQ语句,然后绑定匿名结果来“拉平”您要绑定的结果。

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. 请注意,此方法不是LINQ友好的,因为Join不会转换为LINQ。

Here is what I ended up doing: I ended up ditching the CSLA data source and simply binding a DataTable instead. 这就是我最终要做的事情:我最终放弃了CSLA数据源,而只是绑定了一个DataTable。 I removed the static GridBoundColumn from my RadGrid as it was creating an extra column, and calling the OnNeedDataSource event: 我在创建额外列并调用OnNeedDataSource事件时从RadGrid中删除了静态GridBoundColumn:

<telerik:RadGrid ID="rgPermissions" AllowPaging="false" AllowSorting="true" OnNeedDataSource="rgPermissions_NeedDataSource"
    runat="server">
    <MasterTableView DataKeyNames="Permission Name" AutoGenerateColumns="true" EditMode="InPlace">
        <Columns>
            <telerik:GridEditCommandColumn />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

Then I filled out my event handler, creating the DataTable using the CSLA factory methods: 然后填写事件处理程序,使用CSLA工厂方法创建DataTable:

protected void rgPermissions_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    // Data Access
    PermissionInfoList permissions = PermissionInfoList.GetPermissionInfoList();
    RoleInfoList roles = RoleInfoList.GetRoleList();

    // create datatable for permissions
    DataTable permissionTable = CreatePermissionDataTable(roles);
    foreach (PermissionInfo permission in permissions)
    {
        // Add permission name
        DataRow dataRow = permissionTable.NewRow();
        dataRow["Permission Name"] = permission.PermissionName;

        AddRow(permission, permissionTable, dataRow, roles);
    }

    rgPermissions.DataSource = permissionTable;
}

I created a DataTable for Permission data: 我为权限数据创建了一个数据表:

private DataTable CreatePermissionDataTable(RoleInfoList roles)
{
    DataTable permissions = new DataTable();
    permissions.Columns.Add("Permission Name", typeof(string));
    permissions.Columns["Permission Name"].ReadOnly = true;
    foreach (RoleInfo role in roles)
    {
        permissions.Columns.Add(role.Title, typeof(Boolean));
    }
    return permissions;
}

and I did use LINQ to sift out the permissions from the role data: 我确实使用LINQ从角色数据中筛选出权限:

private DataTable AddRow(PermissionInfo permission, DataTable permissions, DataRow dataRow, RoleInfoList roles)
{

    // Add roles
    foreach (RoleInfo role in roles)
    {
        dataRow[role.Title] = permission.Roles.Any(r => r.RoleId == role.RoleId);
    }
    permissions.Rows.Add(dataRow);

    return permissions;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 动态将DropDownList添加到radGrid ItemTemplate - Dynamically Add DropDownList to radGrid ItemTemplate 如何在动态创建的RadGrid中从动态创建的控件中检索和保存数据 - How do I retrieve and save the data out of a dynamically created control in a dynamically created RadGrid 如何在后面动态创建的radgrid代码中添加Cell Formatting事件方法? - How to add Cell Formatting event method to dynamically created radgrid code behind? 如何动态地向DataTables添加列? - How to add columns dynamically to DataTables? 如何使用Telerik RadGrid在Kendo Grid上设置“UniqueName”? - How do I set a “UniqueName” on a Kendo Grid like you can with a Telerik RadGrid? 如何从Telerik RadGrid删除多余的列? - How to remove extra columns from Telerik RadGrid? 单击Telerik radgrid中的“添加新记录”时,如何访问列中的文本框 - how do I access the textbox in a column when I click “Add new record” in the telerik radgrid 如何在“添加新”模式下打开预填默认值的Telerik RadGrid? - How do I open a Telerik RadGrid in “Add New” mode with default values pre-filled? 如何在 WPF 中动态(通过代码)添加在 XAML 中创建的自定义控件? - How do you dynamically (via code) add custom Controls that were made in XAML in WPF? 如何以及何时向WPF中的网格中动态添加列 - How and when to dynamically add columns to a Grid in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM