2012-01-11 17 views
2

GetEmployeeDetails()メソッドは、Employee型の値を返します。その値をTelerik RadGridに表示する必要があります。従業員の行をクリックすると、Employee行の下にある関連するAddressクラスのフィールド行が展開されます。以下はEmployeeクラスの構造です。Telerik RadGridに階層クラスフィールドを表示するには?

class Employee 
    { 
     string EmpId; 
     string Name; 
     int Age; 
     List<Address> address; 
    } 

    class Address 
    { 
     string Street; 
     string City; 
     int Zip; 
    } 

Telerik RadGridを使用してASPページで実行時に従業員の詳細を表示するコードを以下に書いています。しかし、最初のレベルのEmployeeクラスのフィールドと空白のAddressフィールドのみを表示しています。あなたはこの問題から出てくるのを助けてくれますか? Telerikサイト毎

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

あなたは階層構造を持っているしたい場合、あなたはあなたのバインディングを行うには、「RadGrid1_NeedDataSource」イベントを使用する必要があります。

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:

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

関連する問題