简体   繁体   中英

How do I make this table shorter?

I think I'm doing something terribly wrong here. I have a object inside import called 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.

            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. 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. 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 :)

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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