我已在其他地方发布了此内容,但我对要求的理解对帮助我的主持人@ShaiCohen 不正确,他建议我重新发布它。
我需要编辑输入的数据,该数据DataTable
可以具有不同的列和行,但前两列是不变的。数据如下所示(行首的数字不是数据的一部分):
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 行只有“Critical Down Time”子类别,而其余的则是这三者的组合。
要求是在它们不存在的地方插入“修复代码条目”子类别。它们不存在的原因是因为数据库中没有数据,但是即使没有相应的数据,客户端也希望看到显示的丢失的措辞,并插入一个空行来分隔组,如下所示:
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
但是,当前存在的代码假定数据始终具有三个子类别的分组,因此如果不是,则上一行的子类别将覆盖当前行中的子类别:
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。