c# - 如何在 Telerik RadGrid 中显示分层类字段?

标签 c# asp.net telerik telerik-grid radgrid

GetEmployeeDetails() 方法返回 Employee 类型的值。我需要在 Telerik RadGrid 中显示该值,这样当我们单击“员工”行时,它应该展开“员工”行下的相关地址类字段行。下面是Employee类的结构。

    class Employee
    {
        string EmpId;
        string Name;
        int Age;
        List<Address> address;
    }

    class Address
    {
        string Street;
        string City;
        int Zip;
    }

我编写了以下代码,使用 Telerik RadGrid 在 ASP 页面中运行时显示员工详细信息。但它仅显示第一级 - 员工类字段和地址字段为空。您能帮我解决这个问题吗...?

    protected void Page_Load(object sender, EventArgs e)
    {
        List<Employee> empList = GetEmployeeDetails();

        DataSet dataset = new DataSet("DataSet");

        System.Data.DataTable dt1 = new System.Data.DataTable();
        dt1.TableName = "Employee";
        dt1.Columns.Add("EmpId");
        dt1.Columns.Add("Name");
        dt1.Columns.Add("Age");
        dataset.Tables.Add(dt1);

        System.Data.DataTable dt2 = new System.Data.DataTable();
        dt2.TableName = "Address";
        dt2.Columns.Add("EmpId");
        dt2.Columns.Add("Street");
        dt2.Columns.Add("City");
        dt2.Columns.Add("Zip");
        dataset.Tables.Add(dt2);

        foreach (Employee emp in empList)
        {
            dt1.Rows.Add(new object[] { emp.empId, emp.name, emp.age });
            foreach (Address add in emp.address)
            {
                dt2.Rows.Add(new object[] {emp.empId, add.street, add.city, add.zip });
            }
        }

        DataRelation rel = new DataRelation("rel", dataset.Tables["Employee"].Columns["EmpId"], dataset.Tables["Address"].Columns["EmpId"]);
        dataset.Relations.Add(rel);
        RadGrid1.DataSource = dataSet;
        RadGrid1.DataBind();

    }

最佳答案

根据 Telerik 站点,如果您希望拥有层次结构,则必须使用“RadGrid1_NeedDataSource”事件进行绑定(bind)。

enter image description here

我通过执行以下操作使其正常工作。 (顺便说一句,我使用了你的类和代码,所以它应该很容易模仿,因为我将发布所有代码。你还遗漏了方法 GetEmployeeDetails(),所以我做了自己的:

   private List<Employee> GetEmployeeDetails()
   {
        List<Employee> myEmployees = new List<Employee>();

        Employee Steve = new Employee()
            {
                Address = new List<Address>() { new Address { City = "op", Street = "thatstreet", Zip = 23312 } },
                Age = 23,
                EmpId = "Emp1",
                Name = "SteveIsTheName"
            };


        Employee Carol = new Employee()
            {
                Address = new List<Address>() {
                    new Address { City = "op2", Street = "thatstreet2", Zip = 23313 },
                    new Address { City = "op3", Street = "thatstreet3", Zip = 23314 }},
                Age = 24,
                EmpId = "Emp2",
                Name = "CarolIsTheName"
            };

        myEmployees.Add(Steve);
        myEmployees.Add(Carol);

        return myEmployees;
    }

第 1 步:定义网格的层次结构 View :

protected void RadGrid1_Init(object sender, EventArgs e)
{
    DefineGridStructure();
}

private void DefineGridStructure()
{
    RadGrid1.MasterTableView.DataKeyNames = new string[] { "EmpId" };
    RadGrid1.Width = Unit.Percentage(98);
    RadGrid1.PageSize = 3;
    RadGrid1.AllowPaging = true;
    RadGrid1.AllowSorting = true;
    RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.ShowStatusBar = true;

    RadGrid1.MasterTableView.PageSize = 3;

    //Add columns
    GridBoundColumn boundColumn;
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "EmpId";
    boundColumn.HeaderText = "EmpId";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Name";
    boundColumn.HeaderText = "Name";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Age";
    boundColumn.HeaderText = "Age";
    RadGrid1.MasterTableView.Columns.Add(boundColumn);

    //Detail table - Orders (II in hierarchy level)
    GridTableView tableViewOrders = new GridTableView(RadGrid1);
    tableViewOrders.Width = Unit.Percentage(100);
    tableViewOrders.DataKeyNames = new string[] { "EmpId" };

    GridRelationFields relationFields = new GridRelationFields();
    relationFields.MasterKeyField = "EmpId";
    relationFields.DetailKeyField = "EmpId";
    tableViewOrders.ParentTableRelation.Add(relationFields);
    RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);

    //Add columns
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Street";
    boundColumn.HeaderText = "Street";
    tableViewOrders.Columns.Add(boundColumn);

    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "City";
    boundColumn.HeaderText = "City";
    tableViewOrders.Columns.Add(boundColumn);
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "Zip";
    boundColumn.HeaderText = "Zip";
    tableViewOrders.Columns.Add(boundColumn);
}

第 2 步:设置数据源:(无需调用 DataBind 方法或添加关系,由网格完成)

    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        List<Employee> empList = GetEmployeeDetails();

        DataSet dataset = new DataSet("DataSet");

        System.Data.DataTable dt1 = new System.Data.DataTable();
        dt1.TableName = "Employee";
        dt1.Columns.Add("EmpId");
        dt1.Columns.Add("Name");
        dt1.Columns.Add("Age");
        dataset.Tables.Add(dt1);

        System.Data.DataTable dt2 = new System.Data.DataTable();
        dt2.TableName = "Address";
        dt2.Columns.Add("EmpId");
        dt2.Columns.Add("Street");
        dt2.Columns.Add("City");
        dt2.Columns.Add("Zip");
        dataset.Tables.Add(dt2);

        foreach (Employee emp in empList)
        {
            dt1.Rows.Add(new object[] { emp.EmpId, emp.Name, emp.Age });
            foreach (Address add in emp.Address)
            {
                dt2.Rows.Add(new object[] { emp.EmpId, add.Street, add.City, add.Zip });
            }
        }

        RadGrid1.MasterTableView.DataSource = dataset.Tables["Employee"];
        RadGrid1.MasterTableView.DetailTables[0].DataSource = dataset.Tables["Address"];

    }

第 3 步:运行它。

显然,您可能需要调整大小写,因为可能存在细微的大小写差异。我还调整了您的类以允许设置可变数据,没什么大不了的:

class Employee
{
    public List<Address> Address { get; set; }

    public int Age { get; set; }

    public string Name { get; set; }

    public string EmpId { get; set; }
}

class Address
{
    public string Street { get; set; }

    public string City { get; set; }

    public int Zip { get; set; }
}

希望这有帮助。

-JJ

关于c# - 如何在 Telerik RadGrid 中显示分层类字段?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8817889/

相关文章:

C# MVC 动态创建 View 模型

c# - 如何使用 Gmail 的 SMTP 客户端在 C# 中向自己发送电子邮件,而无需在 Gmail 设置中启用对安全性较低的应用程序的访问?

javascript - 从 RadDateTimePicker 中扣除天数

c# - 我可以使用 gRpc 代替 SignalR 来更新客户端通知吗?

c# - 为什么我的 Razor、ASP.NET、C# HTTPRequests 不返回响应的主体?

javascript - 如何通过 JavaScript 创建 Telerik MVC 组合框

javascript - 使用 Ajax 进行更改事件的 Telerik Kendo 网格验证

c# - WPF Dispatcher 线程卡住主窗口

c# - System.Drawing.Graphics 的默认构造函数是如何移除的?

c# - Microsoft Chart 堆叠列与非堆叠分组