私はこれを別の場所に投稿しましたが、要件についての私の理解は、私を助けてくれたモデレーター @ShaiCohen にとって間違っていたため、再投稿するよう提案されました。
さまざまな列と行を持つことができる供給されるデータを編集する必要がDataTable
ありますが、最初の 2 つの列は一定です。データは次のようになります (行頭の数字はデータの一部ではありません)。
Repair Repair
Code Code Entries 6/1/2012 7/1/2012 8/1/2012 9/1/2012
------ -------------------- -------- -------- -------- --------
1. 00000A Critical Down Time 1
2. 00000A Critical Outage 1
3. 00000A Total Repair Time 65
4. 00000B Critical Down Time 6
5. 00000B Total Repair Time 90
6. 00000C Critical Down Time 1 5
7. 00000C Critical Outage 1 5
8. 00000C Total Repair Time 30 240
9. 00000D Critical Down Time 2
10. 00000E Critical Down Time 1
11. 00000G Critical Down Time 1
12. 00000M Critical Down Time 1 3
13. 00000M Critical Outage 1 3
14. 00000M Total Repair Time 60 180
行 1 ~ 3、6 ~ 8 は同じ修理コード カテゴリを持っているため、グループと見なされることに注意してください。一方、行 10 ~ 12 には「重大なダウンタイム」サブカテゴリのみがあり、残りは 3 つの組み合わせになっています。
要件は、存在しない場所に「修理コード エントリ」サブカテゴリを挿入することです。それらが存在しない理由は、データベースにデータがないためですが、クライアントは、対応するデータがなくても不足している言葉遣いが表示されることを望んでおり、次のようにグループを区切るために空の行を挿入したいと考えています。
Repair Repair
Code Code Entries 6/1/2012 7/1/2012 8/1/2012 9/1/2012
------ -------------------- -------- -------- -------- --------
1. 00000A Critical Down Time 1
2. 00000A Critical Outage 1
3. 00000A Total Repair Time 65
4. 00000B Critical Down Time 6
00000B Critical Outage
5. 00000B Total Repair Time 90
6. 00000C Critical Down Time 1 5
7. 00000C Critical Outage 1 5
8. 00000C Total Repair Time 30 240
9. 00000D Critical Down Time 2
00000D Critical Outage
00000D Total Repair Time
ただし、現在配置されているコードは、データには常に 3 つのサブカテゴリのグループがあると想定しているため、そうでない場合は、前の行のサブカテゴリが現在の行のサブカテゴリを上書きします。
8. 00000C Total Repair Time 30 240
9. 00000D Total Repair Time (should be Critical Down Time) 2
00000D Critical Outage
コード (以下) では、メソッドsubCategoryOccurences
内のカウンターCheckSubCategoryRequirements
は、新しい行が処理されたときにゼロにリセットされません。
public void PrepareDataTable(DataTable dtResults)
{
if (dtResults == null || dtResults.Rows.Count == 0)
return;
//initialize category
categoryPrevious = dtResults.Rows[0]["Category"].ToString();
do
{
//get the current category
categoryCurrent = dtResults.Rows[rowCount]["Category"].ToString();
//check if this is a new category. this is where all the work is done
if (categoryCurrent != categoryPrevious)
{
//check if we have fulfilled the requirement for number of subcategories
CheckSubCategoryRequirements(dtResults);
//at this point we have fulfilled the requirement for number of subcategories
//add blank (separator) row
dtResults.Rows.InsertAt(dtResults.NewRow(), rowCount);
rowCount++;
//reset the number of subcategories
subCategoryOccurences = 0;
categoryPrevious = categoryCurrent;
}
else
{
rowCount++;
categoryOccurences++;
}
} while (rowCount < dtResults.Rows.Count);
//check sub category requirements for the last category
CheckSubCategoryRequirements(dtResults);
}
private void CheckSubCategoryRequirements(DataTable dtResults)
{
if (subCategoryOccurences< subCategories.Length)
{
//we need to add rows for the missing subcategories
while (subCategoryOccurences< subCategories.Length)
{
//create a new row and populate category and subcategory info
rowFiller = dtResults.NewRow();
rowFiller["Category"] = categoryPrevious;
rowFiller["SubCategory"] = subCategories[subCategoryOccurences];
//insert the new row into the current location of table
dtResults.Rows.InsertAt(rowFiller, rowCount);
subCategoryOccurences++;
rowCount++;
}
}
}
メソッド呼び出しの前にカウンターを移動しようとしましたが、望ましくない結果が発生したため、ここからどこに行くべきかわかりません。建設的なコメントをいただければ幸いです。ありがとう。R.