简体   繁体   English

我如何使这张桌子更短?

[英]How do I make this table shorter?

I think I'm doing something terribly wrong here. 我想我在这里做错了什么。 I have a object inside import called oCultivationPlan. 我在导入中有一个名为oCultivationPlan的对象。 It contains data obviously. 它显然包含数据。 And I want to create a table which shows the data inside it. 我想创建一个表来显示其中的数据。 However I only want a selected few from that object and not all the data in it. 但是,我只希望从该对象中选择一些,而不是其中的所有数据。 Is there a way to make this shorter? 有什么办法可以缩短时间? I thought about using foreach or for, but that would loop through all the data inside the object :/ while I only want a selected few. 我考虑过使用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);

not a foreach :) but you can use a for loop to get trough it. 不是foreach :),但是您可以使用for循环来获取它。 you should be able to use the code below as a solution for your question :) its smaller cuz of the loop but it does the exact same thing as what you did. 您应该可以使用下面的代码作为问题的解决方案:)循环的较小cuz,但其功能与您所做的完全相同。 I use the string array to keep all the info you want to get inside the table so that it will be having something to go out after. 我使用字符串数组将要获取的所有信息保留在表中,以便以后有东西可用。 For each row you have you got 2 new cells in it and thats why we have the row*2 so the cells can get filled up :) 对于每一行,您都有2个新单元格,这就是为什么我们拥有该行* 2以便单元格可以被填满的原因:)

Hope it works for you and that you can use the solution :) 希望它对您有用,并且您可以使用该解决方案:)

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

I would create a strong typed Class 我会创建一个强类型的类

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

Then as David said, write a filter function to filter data from Import class and map it to ImportValues 然后如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()}
    };
}

Then in the aspx.cs class, just loop through List<ImportDto> and create LiteralControls 然后在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);
}

That way all you need to do in future to add new filtered data, is to modify your mapping function and add a new ImportDto, and the it will be displayed in the frontend automatically. 这样,将来添加新的过滤数据所需要做的就是修改映射功能并添加新的ImportDto,它将自动显示在前端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM