簡體   English   中英

我如何使這張桌子更短?

[英]How do I make this table shorter?

我想我在這里做錯了什么。 我在導入中有一個名為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);

不是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);
        }

我會創建一個強類型的類

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

然后如David所說,編寫一個過濾器函數以過濾Import類中的數據並將其映射到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()}
    };
}

然后在List<ImportDto>類中,循環遍歷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,它將自動顯示在前端。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM