2014-02-26 50 views
0

我目前正在嘗試從DataTable中檢索列,該列中包含某些列內的對象列。如何從DataSource中檢索對象列RowFilter

讓我們看到:

(this.CohortGrid.DataSource as DataTable).DefaultView.RowFilter = string.Format("CohortFormation.Name LIKE '*{0}*'", ddlFormation.SelectedItem.Text); 

我走進去這些列我的數據表:

[0]:身份證

[1]:名稱

[2]:狀態

[3]:羣組形式

[4]:RoomCol

[5]:InstructorCol

[6]:EmployeeCol

列CohortFormation是包含Id和名稱的對象。

所以,我試圖檢索CohortFormation的名稱這樣的CohortFormation.Name LIKE 但它返回我:

無法找到列

在網格視圖模板列我可以做<%# Eval("CohortFormation.Name") %>它工作得很好。但在後面的代碼中,我該如何做到這一點?

回答

0

我剛找到一個解決方案,它的工作非常好!

我循環訪問DataTable並刪除與DropDownList - > ddlFormation中所選值不相等的行。

有代碼:

CohortCollection cohortCol = (CohortCollection)Session["cohortCol"]; 

     foreach(Cohort cohort in cohortCol.ToList()) 
     { 
      if(cohort.Status.Id != int.Parse(ddlFormation.SelectedItem.Value)) 
      { 
       cohortCol.Remove(cohort); 
      } 
     } 

     this.CohortGrid.DataSource = cohortCol; 
     this.CohortGrid.DataBind(); 

因此,關鍵是要列表,而不是數據表上的循環,如果它不等於我們刪除的對象。最後,我們綁定數據。

它完美的作品!