我有一个带有以下示例数据的 C# 数据表,
现在,我正在尝试在 3 个不同级别的“类别”(“Grand_Total”、“Overall_Total”和“PROJ_TOTAL”)上添加 3 个更多列作为行号(以降序从零开始),这是我想要实现的,
我只能在数据表逻辑中添加一列“G_T_I”和总行数,但无法弄清楚破解其他两列“O_T_I”和“P_T_I”的逻辑。请帮助/建议?
class Program
{
static void Main(string[] args)
{
// Get the DataTable.
DataTable table = GetTable();
//get new data table
DataTable newTable = GetNewTable();
for(int i = table.Rows.Count - 1; i > -1; i--)
{
var gTotalIndex = (table.Rows.Count - i) - 1;
newTable.Rows.Add(i, table.Rows[gTotalIndex]["Category"], table.Rows[gTotalIndex]["Name"], table.Rows[gTotalIndex]["PROJID"], table.Rows[gTotalIndex]["Type"], table.Rows[gTotalIndex]["Amt"]);
}
}
static DataTable GetNewTable()
{
DataTable table = new DataTable();
table.Columns.Add("G_T_I", typeof(int));
table.Columns.Add("Category", typeof(string));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("PROJID", typeof(string));
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Amt", typeof(decimal));
return table;
}
static DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("Category", typeof(string));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("PROJID", typeof(string));
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Amt", typeof(decimal));
table.Rows.Add("NEW_PROJ", "ABC", "100", "Acost", 10);
table.Rows.Add("SAME_PROJ", "", "", "Bcost", 20);
table.Rows.Add("PROJ_TOTAL", "", "100 Total", "", 30);
table.Rows.Add("NEW_PROJ", "", "200", "Acost", 40);
table.Rows.Add("PROJ_TOTAL", "", "200 Total", "", 40);
table.Rows.Add("OVERALL_TOTAL", "ABC Total", "", "", 70);
table.Rows.Add("NEW_PROJ", "PQR", "300", "Acost", 10);
table.Rows.Add("SAME_PROJ", "", "", "Bcost", 10);
table.Rows.Add("PROJ_TOTAL", "", "300 Total", "", 20);
table.Rows.Add("OVERALL_TOTAL", "PQR Total", "", "", 20);
table.Rows.Add("GRAND_TOTAL", "", "", "", 90);
return table;
}
}