我有以下代碼循環通過DataTable
並建立另一個如果滿足某些條件。但是,最初的DataTable中的最後一行將被跳過。爲什麼循環跳過我的DataTable中的最後一行?
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow row = dt.Rows[i];
DataRow nextRow = i < dt.Rows.Count - 1 ? dt.Rows[i + 1] : null;
string account = row[1].ToString();
string nextAccount = "";
if (nextRow != null)
{
nextAccount = nextRow[1].ToString();
}
numberOfItems++;
totalAmount += Convert.ToDecimal(row[2]);
row[4] = "D";
row[5] = c;
row[6] = Sequence;
if (nextRow != null && i < dt.Rows.Count && account != nextAccount)
{
dt2.Rows.Add("N",
c,
row[1],
row[2],
row[3],
numberOfItems,
totalAmount,
Sequence);
numberOfItems = 0;
totalAmount = 0m;
Sequence++;
}
}
在上面的代碼,如果我有一個表,如:
abc, 1, 2, 3 abc, 1, 2, 5 def, 1, 3, 6
它會同時處理ABC的,但不是高清。
DT2應包含:
abc, 1, 2, 8, 2 def, 1, 3, 6, 1
凡8是總的第4列中dt和2爲abc的行數。
我只得到這雖然
abc, 1, 2, 8, 2
定義「process」,因爲在前兩行中只有dt2中有行嗎? –
@JamesMichaelHare - 是的,基本上dt2是dt的總結。我會更新我的帖子 – Xaisoft