以下のサンプルデータを含むC#データテーブルがあります。
現在、3つの異なるレベルの「Category」(「Grand_Total」、「Overall_Total」、「PROJ_TOTAL」)の行番号(降順でゼロから始まる)としてさらに3つの列を追加しようとしています。これが達成しようとしています。 、
データテーブルロジックの合計行数で1つの列「G_T_I」のみを追加できますが、他の2つの列「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;
}
}