如何在 Telerik RadGrid 中单击一次显示两个表行

发布于 2024-12-27 19:42:33 字数 171 浏览 1 评论 0原文

我有一个类,其中包含另外两个类,例如层次结构。当我在 Telerik RadGrid 中显示时,它应该显示如下,当我们单击第一类行时,它应该显示两行相关类,如下图所示。可以帮我做这个吗..?

在此处输入图像描述

I have one class which contains two more classes like hierarchy. When I'm displaying in Telerik RadGrid it should display like, when we click on first class row then it should display two rows of related classes as in below figure. Could help me to do this..?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久夏青 2025-01-03 19:42:33

当我回答您之前的帖子时,我只是采用了我的其他答案并对其进行了修改以实现您正在寻找的内容。下面是添加一个存储爱好信息的类。然后,我添加了所需的结构和关系(不同颜色的方块表示不同的类):

在此处输入图像描述

注意:此处的更改很小,并且更改被注释为 //New Something// 因此它们应该很容易看到。

protected void Page_Load(object sender, EventArgs e)
{

}

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 = 5;
    RadGrid1.AllowPaging = true;
    RadGrid1.AllowSorting = true;
    RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.ShowStatusBar = true;

    RadGrid1.MasterTableView.PageSize = 5;

    //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);


    //New Detail Table #2 - adds in a another class that stores data


    GridTableView tableViewOrders2 = new GridTableView(RadGrid1);
    tableViewOrders2.Width = Unit.Percentage(100);

    tableViewOrders2.DataKeyNames = new string[] { "EmpId" };

    GridRelationFields relationFields2 = new GridRelationFields();
    relationFields2.MasterKeyField = "EmpId";
    relationFields2.DetailKeyField = "EmpId";
    tableViewOrders2.ParentTableRelation.Add(relationFields2);

    RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders2);

    //Add columns
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "HobbyName";
    boundColumn.HeaderText = "HobbyName";
    tableViewOrders2.Columns.Add(boundColumn);

}

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);


    //New datatable that stores the new classes' data
    DataTable dt3 = new DataTable();
    dt3.TableName = "Hobby";
    dt3.Columns.Add("EmpId");
    dt3.Columns.Add("HobbyName");
    dataset.Tables.Add(dt3);


    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 });
        }
        //New data add loop
        foreach (Hobby hob in emp.Hobby)
        {
            dt3.Rows.Add(new object[] { emp.EmpId, hob.HobbyName });
        }


    }

    RadGrid1.MasterTableView.DataSource = dataset.Tables["Employee"];
    RadGrid1.MasterTableView.DetailTables[0].DataSource = dataset.Tables["Address"];
    //Add the new table to the grid
    RadGrid1.MasterTableView.DetailTables[1].DataSource = dataset.Tables["Hobby"];
}

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 } },
        Hobby = new List<Hobby>() { new Hobby() { HobbyName = "Skating" } },
        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 }},
        Hobby = new List<Hobby>() { new Hobby() { HobbyName = "Fishing" } },
        Age = 24,
        EmpId = "Emp2",
        Name = "CarolIsTheName"
    };

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

    return myEmployees;
}
}

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

    public List<Hobby> Hobby { 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; }
}

class Hobby
{
    public string HobbyName { get; set; }
}

As I answered your previous post, I just took my other answer and modified it to do what you are looking for. The following is the addition of a class that stores hobby info. I then added the needed structure and relations (the different colored squares denote the different classes):

enter image description here

Note: The changes here are minimal and the changes are commented as //New something// so they should be easy to see.

protected void Page_Load(object sender, EventArgs e)
{

}

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 = 5;
    RadGrid1.AllowPaging = true;
    RadGrid1.AllowSorting = true;
    RadGrid1.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
    RadGrid1.AutoGenerateColumns = false;
    RadGrid1.ShowStatusBar = true;

    RadGrid1.MasterTableView.PageSize = 5;

    //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);


    //New Detail Table #2 - adds in a another class that stores data


    GridTableView tableViewOrders2 = new GridTableView(RadGrid1);
    tableViewOrders2.Width = Unit.Percentage(100);

    tableViewOrders2.DataKeyNames = new string[] { "EmpId" };

    GridRelationFields relationFields2 = new GridRelationFields();
    relationFields2.MasterKeyField = "EmpId";
    relationFields2.DetailKeyField = "EmpId";
    tableViewOrders2.ParentTableRelation.Add(relationFields2);

    RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders2);

    //Add columns
    boundColumn = new GridBoundColumn();
    boundColumn.DataField = "HobbyName";
    boundColumn.HeaderText = "HobbyName";
    tableViewOrders2.Columns.Add(boundColumn);

}

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);


    //New datatable that stores the new classes' data
    DataTable dt3 = new DataTable();
    dt3.TableName = "Hobby";
    dt3.Columns.Add("EmpId");
    dt3.Columns.Add("HobbyName");
    dataset.Tables.Add(dt3);


    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 });
        }
        //New data add loop
        foreach (Hobby hob in emp.Hobby)
        {
            dt3.Rows.Add(new object[] { emp.EmpId, hob.HobbyName });
        }


    }

    RadGrid1.MasterTableView.DataSource = dataset.Tables["Employee"];
    RadGrid1.MasterTableView.DetailTables[0].DataSource = dataset.Tables["Address"];
    //Add the new table to the grid
    RadGrid1.MasterTableView.DetailTables[1].DataSource = dataset.Tables["Hobby"];
}

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 } },
        Hobby = new List<Hobby>() { new Hobby() { HobbyName = "Skating" } },
        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 }},
        Hobby = new List<Hobby>() { new Hobby() { HobbyName = "Fishing" } },
        Age = 24,
        EmpId = "Emp2",
        Name = "CarolIsTheName"
    };

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

    return myEmployees;
}
}

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

    public List<Hobby> Hobby { 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; }
}

class Hobby
{
    public string HobbyName { get; set; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文