C# 如何在Telerik RadGrid中单击显示两个表格行
我有一个类,它包含另外两个类,比如层次结构。当我在Telerik RadGrid中显示时,它应该显示为,当我们单击第一个类行时,它应该显示两行相关类,如下图所示。可以帮助我做到这一点吗C# 如何在Telerik RadGrid中单击显示两个表格行,c#,asp.net,telerik,radgrid,radgridview,C#,Asp.net,Telerik,Radgrid,Radgridview,我有一个类,它包含另外两个类,比如层次结构。当我在Telerik RadGrid中显示时,它应该显示为,当我们单击第一个类行时,它应该显示两行相关类,如下图所示。可以帮助我做到这一点吗 当我回答你之前的帖子时,我只是选择了我的另一个答案,并对其进行了修改,以满足你的要求。以下是添加的一个存储爱好信息的类。然后,我添加了所需的结构和关系(不同颜色的正方形表示不同的类): 注意:此处的更改非常小,并且这些更改被注释为//新内容//因此应该很容易看到。 protected void Page_Loa
当我回答你之前的帖子时,我只是选择了我的另一个答案,并对其进行了修改,以满足你的要求。以下是添加的一个存储爱好信息的类。然后,我添加了所需的结构和关系(不同颜色的正方形表示不同的类): 注意:此处的更改非常小,并且这些更改被注释为//新内容//因此应该很容易看到。
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; }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
}
受保护的void RadGrid1_Init(对象发送方,事件参数e)
{
DefineGridStructure();
}
私有void DefineGridStructure()
{
RadGrid1.MasterTableView.DataKeyNames=新字符串[]{“EmpId”};
RadGrid1.宽度=单位百分比(98);
RadGrid1.PageSize=5;
RadGrid1.AllowPaging=true;
RadGrid1.AllowSorting=true;
RadGrid1.PagerStyle.Mode=GridPagerMode.NextPrevendNumeric;
RadGrid1.AutoGenerateColumns=false;
RadGrid1.ShowStatusBar=true;
RadGrid1.MasterTableView.PageSize=5;
//添加列
GridBoundColumn-boundColumn;
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“EmpId”;
boundColumn.HeaderText=“EmpId”;
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“Name”;
boundColumn.HeaderText=“Name”;
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“年龄”;
boundColumn.HeaderText=“年龄”;
RadGrid1.MasterTableView.Columns.Add(boundColumn);
//明细表-订单(二级层级)
GridTableView tableViewOrders=新的GridTableView(RadGrid1);
tableViewOrders.Width=单位百分比(100);
tableViewOrders.DataKeyNames=新字符串[]{“EmpId”};
GridRelationFields relationFields=新的GridRelationFields();
relationFields.MasterKeyField=“EmpId”;
relationFields.DetailKeyField=“EmpId”;
tableViewOrders.ParentTableRelation.Add(relationFields);
RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders);
//添加列
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“街道”;
boundColumn.HeaderText=“街道”;
tableViewOrders.Columns.Add(boundColumn);
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“城市”;
boundColumn.HeaderText=“城市”;
tableViewOrders.Columns.Add(boundColumn);
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“Zip”;
boundColumn.HeaderText=“Zip”;
tableViewOrders.Columns.Add(boundColumn);
//新的细节表#2-添加另一个存储数据的类
GridTableView tableViewOrders2=新的GridTableView(RadGrid1);
tableViewOrders2.宽度=单位百分比(100);
tableViewOrders2.DataKeyNames=新字符串[]{“EmpId”};
GridRelationFields relationFields2=新的GridRelationFields();
relationFields2.MasterKeyField=“EmpId”;
relationFields2.DetailKeyField=“EmpId”;
tableViewOrders2.ParentTableRelation.Add(relationFields2);
RadGrid1.MasterTableView.DetailTables.Add(tableViewOrders2);
//添加列
boundColumn=新的GridBoundColumn();
boundColumn.DataField=“HobbyName”;
boundColumn.HeaderText=“HobbyName”;
tableViewOrders2.Columns.Add(boundColumn);
}
受保护的void RadGrid1_NeedDataSource(对象发送方,GridNeedDataSourceEventArgs e)
{
List employist=GetEmployeeDetails();
数据集=新数据集(“数据集”);
System.Data.DataTable dt1=新的System.Data.DataTable();
dt1.TableName=“Employee”;
dt1.列。添加(“EmpId”);
dt1.列。添加(“名称”);
dt1.列。添加(“年龄”);
dataset.Tables.Add(dt1);
System.Data.DataTable dt2=新的System.Data.DataTable();
dt2.TableName=“地址”;
dt2.列。添加(“EmpId”);
dt2.列。添加(“街道”);
dt2.列。添加(“城市”);
dt2.列。添加(“Zip”);
dataset.Tables.Add(dt2);
//存储新类数据的新datatable
DataTable dt3=新的DataTable();
dt3.TableName=“爱好”;
dt3.列。添加(“EmpId”);
dt3.Columns.Add(“HobbyName”);
dataset.Tables.Add(dt3);
foreach(Employist中的员工emp)
{
添加(新对象[]{emp.EmpId,emp.Name,emp.Age});
foreach(地址加载项emp.Address)
{
添加(新对象[]{emp.EmpId,Add.Street,Add.City,Add.Zip});
}
//新数据添加循环
foreach(emp.Hobby中的Hobby hob)
{
Add(新对象[]{emp.EmpId,hob.HobbyName});
}
}
RadGrid1.MasterTableView.DataSource=dataset.Tables[“Employee”];
RadGrid1.MasterTableView.DetailTables[0]。数据源=数据集。表格[“地址”];
//将新表添加到网格中
RadGrid1.MasterTableView.DetailTables[1]。DataSource=dataset.Tables[“Hobby”];
}
私有列表GetEmployeeDetails()
{
List myEmployees=新列表();
员工Steve=新员工()
{
地址=新列表(){newaddress{City=“op”,Street=“thatstreet”,Zip=23312},
嗜好=新列表(){new Hobby(){HobbyName=“溜冰”},
年龄=23岁,
EmpId=“Emp1”,
Name=“SteveIsTheName”
};
员工卡罗尔=新员工()
{
地址=新列表(){
新地址{City=“op2”,Street=“thatstreet2”,Zip=23313},
新地址{City=“op3”,Street=“thatstreet3”,Zip=23314},
Hobby=new List(){new Hobby(){HobbyName=“Fishing”},
年龄=24岁,
EmpId=“Emp2”,
Name=“carolithename”
};
myEmployees.Add(史蒂夫);
我的雇员