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; }怎么使{0}为1,2依次}
时间: 2024-03-28 18:42:07 浏览: 32
DataSet.Tables[].Rows[][]的用法详细解析
可以使用一个变量来记录序号,每次循环时让该变量加1,并将其作为格式化字符串的参数,代码如下:
```
int rows = dtDetail.Rows.Count;
int i = 0;
int index = 1;
var str = new StringBuilder();
bool isFirst = true;
foreach (DataRow row in dtDetail.Rows)
{
str.Append(" <tr >");
str.AppendFormat(" <td align= 'center' > {0} </td>", index++);
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();
```
阅读全文