2012-01-11 51 views
2

GetEmployeeDetails()方法返回Employee類型的值。我需要在Telerik RadGrid中顯示這個值,這樣當我們單擊Employee行時,它應該展開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頁中運行時的員工詳細信息。但它只顯示第一級員工類字段和地址字段爲空。你能幫我解決這個問題嗎?

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

您是否設置了嵌套網格視圖來表示地址?爲了調試,我們還需要看到網格標記... – 2012-01-12 18:17:53

回答

4

每Telerik的的網站,如果你希望有一個層次,你必須使用「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步:運行它。

很明顯,您可能需要對套管進行調整,因爲可能存在輕微的大寫差異。我也調整了你的班級,以允許設置可變數據,沒什麼大的:

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