int rows = dtDetail.Rows.Count; int i = 0; foreach (DataRow row in dtDetail.Rows) { str.AppendFormat(@" <tr > <td align= 'center' > {0} </td> <td align= 'center' > {1} </td> <td align= 'center' > {2} </td> <td align= 'center' > {3} </td> <td align= 'center' > {4} </td> <td align= 'center' > {5} </td>", row[i+1], row["Name"], row["strCustomerPO"], row["DESCRIPTION"], row["strHSCode"], row["numQty"]); if (i == 0) { str.AppendFormat(@" <td align= 'center' rowspan='{2}' > {0} </td> <td align= 'center' rowspan='{2}'> {1} </td>", row["numLotQty"], row["numRoughWeight"], rows); } str.AppendFormat(@"<td align= 'center' > {0} </td>", row["numNetWeight"]); if (i == 0) { str.AppendFormat(@"<td align= 'center' rowspan='{0}'> {1} </td> </tr> ", rows, Measurement); } i = 1; }优化
时间: 2024-03-28 10:42:06 浏览: 44
DataSet.Tables[].Rows[][]的用法详细解析
可以考虑使用StringBuilder的Append方法来代替Format方法构建字符串,这样可以提高性能。同时,可以将循环中的判断移至循环外,避免重复判断。代码如下:
```
int rows = dtDetail.Rows.Count;
int i = 0;
var str = new StringBuilder();
bool isFirst = true;
foreach (DataRow row in dtDetail.Rows)
{
str.Append(" <tr >");
str.AppendFormat(" <td align= 'center' > {0} </td>", row[i + 1]);
str.AppendFormat(" <td align= 'center' > {0} </td>", row["Name"]);
str.AppendFormat(" <td align= 'center' > {0} </td>", row["strCustomerPO"]);
str.AppendFormat(" <td align= 'center' > {0} </td>", row["DESCRIPTION"]);
str.AppendFormat(" <td align= 'center' > {0} </td>", row["strHSCode"]);
str.AppendFormat(" <td align= 'center' > {0} </td>", row["numQty"]);
if (isFirst)
{
str.AppendFormat(" <td align= 'center' rowspan='{0}' > {1} </td>", rows, row["numLotQty"]);
str.AppendFormat(" <td align= 'center' rowspan='{0}'> {1} </td>", rows, row["numRoughWeight"]);
isFirst = false;
}
str.AppendFormat(" <td align= 'center' > {0} </td>", row["numNetWeight"]);
if (isFirst)
{
str.AppendFormat(" <td align= 'center' rowspan='{0}'> {1} </td>", rows, Measurement);
isFirst = false;
}
str.Append("</tr>");
i = 1;
}
string result = str.ToString();
```
阅读全文