あなたは階層構造を持っているしたい場合、あなたはあなたのバインディングを行うには、「RadGrid1_NeedDataSource」イベントを使用する必要があります。
![enter image description here](https://i.stack.imgur.com/bjGdc.png)
私はそれが次のことを実行して動作するようになりました。 。私はすべてのコードをポストするとして、模倣するのは簡単でなければなりませんので、ところで、私はあなたのクラスやコードを使用する(また)(メソッドの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:実行します。
明らかに大文字小文字の違いがあるかもしれないので、ケーシングを調整する必要があるかもしれません。可変データの設定を許可するようにクラスを調整しましたが、大きなものはありません。
希望します。
-JJ
アドレスを表すためにネストされたグリッドビューを設定していますか?デバッグするには、グリッドマークアップも参照する必要があります... –