GetEmployeeDetails()方法返回Employee类型的值。我需要在Telerik RadGrid中显示该值,这样当我们单击Employee行时,它应该扩展Employee行下的相关Address类字段行。以下是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();
}
答案 0 :(得分:4)
根据Telerik网站,如果您希望拥有层次结构,则必须使用“RadGrid1_NeedDataSource”事件进行绑定。
我通过执行以下操作让它工作。 (顺便说一句,我使用了你的类和代码,所以它很容易模仿,因为我会发布所有的代码。你也省略了方法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:定义网格的层次结构视图:
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