gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:09:52 25 4
gpt4 key购买 nike

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 页面中显示员工详细信息。但它只显示第一级 - Employee 类字段和地址字段为空。你能帮我解决这个问题吗...?

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com