2014-09-23 36 views
1

我認爲我在這裏做了非常錯誤的事情。我有一個名爲oCultivationPlan的導入內部的對象。它明顯包含數據。我想創建一個表格來顯示它裏面的數據。但是,我只想從該對象中選擇一些而不是所有數據。有沒有辦法讓這個更短?我想過使用foreach或for,但是這會循環對象中的所有數據:/雖然我只想要一些選定的數據。如何縮短這張桌子?

  TableRow tblRow = new TableRow(); 

      TableCell tblc = new TableCell(); 
      tblc.Controls.Add(new LiteralControl("ID")); 
      TableCell tblc2 = new TableCell(); 
      tblc2.Controls.Add(new LiteralControl(import.oCultivationPlan.iID.ToString())); 

      tblRow.Controls.Add(tblc); 
      tblRow.Controls.Add(tblc2); 

      tblImportPreview.Controls.Add(tblRow); 

      TableCell tblc3 = new TableCell(); 
      TableCell tblc4 = new TableCell(); 
      tblc3.Controls.Add(new LiteralControl("Description")); 
      tblc4.Controls.Add(new LiteralControl(import.oCultivationPlan.sDescription.ToString())); 

      TableRow tblRow2 = new TableRow(); 
      tblRow2.Controls.Add(tblc3); 
      tblRow2.Controls.Add(tblc4); 

      tblImportPreview.Controls.Add(tblRow2); 

      TableCell tblc5 = new TableCell(); 
      TableCell tblc6 = new TableCell(); 
      tblc5.Controls.Add(new LiteralControl("DateCreated")); 
      tblc6.Controls.Add(new LiteralControl(import.oCultivationPlan.dDateCreated.ToString())); 

      TableRow tblRow3 = new TableRow(); 
      tblRow3.Controls.Add(tblc5); 
      tblRow3.Controls.Add(tblc6); 

      tblImportPreview.Controls.Add(tblRow3); 
+1

[CodeReview.SE]會更好嗎? – 2014-09-23 07:57:20

+0

爲什麼不在你建立你的表之前先篩選列表,如果你想選擇幾行。例如,使用一些簡單的linq返回你的過濾列表,或者爲什麼不在你開始建表過程之前過濾列表。 (假設你在表創建之前知道你正在過濾什麼) – 2014-09-23 07:59:34

+0

事情就是這樣。 我有一個包含數據列表的導出文件。而ALLLL的數據正在被稱爲「進口商」的類中拋出。該導入器類包含列表和對象。 oCultiationPlan是其中一個對象。 我可能不會過濾此對象,因爲它仍然需要導入並保存到數據庫中。 但我可以創建一個新的類與我想要的信息,並從它創建一個表。這將是可能的,但沒有更好的選擇嗎? – 2014-09-23 08:21:45

回答

0

不是foreach :)但你可以使用for循環來獲取它。你應該能夠使用下面的代碼作爲你的問題的解決方案:) 它的小循環cuz,但它做的和你做的完全一樣。我使用字符串數組來保存想要在表內獲取的所有信息,以便它可以在出現之後出現。 對於每一行你有2個新的細胞,因此,爲什麼我們有行* 2,因此細胞可以填滿:)

希望它適合你,並且你可以使用解決方案:)

 int _row = 1; 
     int _cell = 0; 
     string[] arr = new string[6] { "ID", import.oCultivationPlan.iID.ToString(), "Description", import.oCultivationPlan.sDescription.ToString(), "DateCreated", import.oCultivationPlan.dDateCreated.ToString() }; 
     for (; _row <= 3; _row++) 
     { 
      TableRow tblRow = new TableRow(); 
      for (; _cell < _row * 2; _cell++) 
      { 
       TableCell tblc = new TableCell(); 
       tblc.Controls.Add(new LiteralControl(arr[_cell])); 
       tblRow.Controls.Add(tblc); 
      } 

      tblImportPreview.Controls.Add(tblRow); 
     } 
+0

耶斯它的作品。非常感謝這是我一直在尋找的。 :DU是生命的救星; D – 2014-09-23 09:51:34

+0

沒問題:)我剛剛讀了你寫的內容,儘管這不應該很難:D – BlackStarHH 2014-09-23 09:56:08

+0

不要忘了標記問題,因爲選擇答案回答:) – BlackStarHH 2014-09-23 10:43:54

0

我想創建一個強類型類

public Class ImportDto 
{ 
    public string RowIdentifier {get; set;} 
    public string RowValue {get; set; 
} 

然後當大衛說,寫過濾功能,以從導入類過濾數據,並將其映射到ImportValues

public List<ImportDto> FilterImportedData(Import import) 
{ 
    var importDto = new List<ImportDto> 
    { 
     new ImportDto { RowIdentifier ="ID", RowValue = import.oCultivationPlan.iID.ToString()}, 
     new ImportDto { RowIdentifier ="Description", RowValue = import.oCultivationPlan.sDescription.ToString()} 
    }; 
} 

然後在aspx.cs類,通過List<ImportDto>只是循環,創造LiteralControls

foreach(var dto in importDtos) 
{ 
var row = new TableRow(); 

var identifierCell = new TableCell(); 
var valueCell = new TableCell(); 

identifierCell.Controls.Add(new LiteralControl(dto.RowIdentifier)); 
valueCell.Controls.Add(new LiteralControl(dto.RowValue)); 

row.Add(identifierCell); 
row.Add(valueCell); 

tblImportPreview.Controls.Add(row); 
} 

這樣,你需要在未來增加新的過濾後的數據做的,是要修改您的映射功能,並添加新的ImportDto,它會自動顯示在前端。