[英]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.