我已經在其他地方發佈了這個記錄,但是我對該要求的理解對於主持人@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行只有「臨界停機時間」子類別,而其餘的則是三者的組合。
要求是將「修理代碼條目」子類別插入不存在的地方。他們不存在的原因是因爲數據庫中沒有數據,但客戶希望看到即使沒有相應的數據也顯示缺少的字母,並插入空行來分隔這些組:
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
方法沒有得到rese當新行被處理時,t爲零。
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.
@ShaiCohen,根據您的要求我重新張貼了這個。 – Risho 2013-03-07 17:30:59
如果你的表格被標準化了,這會容易得多。 – 2013-03-07 19:35:46
@JohnSaunders,這不是標準化的問題,查詢使用了一個數據透視表,這樣通常在單個列中的日期現在顯示爲列,而底層數據由查詢計算。 – Risho 2013-03-07 20:00:54