I would like to sum up the total amount for each columns based on the module. The number of the column is not fixed because it displayed based on user selected, for example if user select three qty, then only Qty_1, Qty_2 and Qty_3 is display, else if user
select five qty, then will display Qty_1 until Qty_5.
Now my problem is that i only can insert the total qty of Qty_1 column and for Qty_2 & 3 I cannot insert. Ihave no idea why.
Below here is the result BEFORE add-in TOTAL:
Module Type Qty_1 Qty_2 Qty_3
M1 A 10 10 15
M1 B 10 5 10
M2 A 7 9 15
M2 B 14 5 9
M3 A 0 10 10
M3 B 15 18 10
Below here is the sample result AFTER add-in TOTAL:
Module Type Qty_1 Qty_2 Qty_3
M1 A 10 10 15
M1 B 10 5 10
TOTAL 20 15 25
M2 A 7 9 15
M2 B 14 5 9
TOTAL 21 14 24
M3 A 0 10 10
M3 B 15 18 10
TOTAL 15 28 20
This is the coding I used to sum up the amount.
But I only can insert total qty for Qty_1 column and the rest I cannot insert the total qty.
int w = 0, v = 3;
for (int col = 2; col < dTable.Columns.Count; col++)
{
for (int x = 0; x < dSingleModule.Rows.Count; x++)
{
DataRow insertRow = dTable.NewRow();
insertRow[1] = "TOTAL";
string str = a.Rows[x][0].ToString();
for (int t = 1; t < dTable.Rows.Count; t++)
{
string _mod = dTable.Rows[t][0].ToString();
if (_mod == str)
{
int num = int.Parse(dTable.Rows[t][col].ToString());
w += num;
}
}
insertRow[col] = w;
dTable.Rows.InsertAt(insertRow, v);
w = 0;
v += 3;
}
}
Can you please explain waht will you do with this datatable later on ... because I am almost sure you can done this in more fancy way than you are doing it :)
Considering your problem ... I see you have 2 different datatables in for loops (dTable and dSingleModule) - why? And at the end you are inserting a new row into table which you are enumerating in for loop. You can not do that, because
when you insert a new row, table length is changed and also row indexes.
For datatable, dTable is contain the original data before I insert the TotalQty and dSingleModule is use to get the distincted module like M1, M2 and M3. I use it to make sure the Module at dTable and dSingleModule is match before i do the calculation and
insert the value.
I coding, i add new row for insert the total qty, but i only can insert to one column the rest cannot.
Just try to insert all rows in new (3rd) table instead in dTable. In for statement copy rows from dTable until Module is changed, and when it's changed insert total amount row and so on.
ying87
Member
37 Points
61 Posts
Datatable - add total amount of qty for each columns
Dec 12, 2012 12:41 PM|LINK
Hi,
I would like to sum up the total amount for each columns based on the module. The number of the column is not fixed because it displayed based on user selected, for example if user select three qty, then only Qty_1, Qty_2 and Qty_3 is display, else if user select five qty, then will display Qty_1 until Qty_5.
Now my problem is that i only can insert the total qty of Qty_1 column and for Qty_2 & 3 I cannot insert. Ihave no idea why.
Below here is the result BEFORE add-in TOTAL: Module Type Qty_1 Qty_2 Qty_3 M1 A 10 10 15 M1 B 10 5 10 M2 A 7 9 15 M2 B 14 5 9 M3 A 0 10 10 M3 B 15 18 10 Below here is the sample result AFTER add-in TOTAL: Module Type Qty_1 Qty_2 Qty_3 M1 A 10 10 15 M1 B 10 5 10 TOTAL 20 15 25 M2 A 7 9 15 M2 B 14 5 9 TOTAL 21 14 24 M3 A 0 10 10 M3 B 15 18 10 TOTAL 15 28 20This is the coding I used to sum up the amount. But I only can insert total qty for Qty_1 column and the rest I cannot insert the total qty. int w = 0, v = 3; for (int col = 2; col < dTable.Columns.Count; col++) { for (int x = 0; x < dSingleModule.Rows.Count; x++) { DataRow insertRow = dTable.NewRow(); insertRow[1] = "TOTAL"; string str = a.Rows[x][0].ToString(); for (int t = 1; t < dTable.Rows.Count; t++) { string _mod = dTable.Rows[t][0].ToString(); if (_mod == str) { int num = int.Parse(dTable.Rows[t][col].ToString()); w += num; } } insertRow[col] = w; dTable.Rows.InsertAt(insertRow, v); w = 0; v += 3; } }gregaDro
Member
318 Points
65 Posts
Re: Datatable - add total amount of qty for each columns
Dec 12, 2012 01:19 PM|LINK
Can you please explain waht will you do with this datatable later on ... because I am almost sure you can done this in more fancy way than you are doing it :)
Considering your problem ... I see you have 2 different datatables in for loops (dTable and dSingleModule) - why? And at the end you are inserting a new row into table which you are enumerating in for loop. You can not do that, because when you insert a new row, table length is changed and also row indexes.
ying87
Member
37 Points
61 Posts
Re: Datatable - add total amount of qty for each columns
Dec 12, 2012 11:58 PM|LINK
hi,
For datatable, dTable is contain the original data before I insert the TotalQty and dSingleModule is use to get the distincted module like M1, M2 and M3. I use it to make sure the Module at dTable and dSingleModule is match before i do the calculation and insert the value.
I coding, i add new row for insert the total qty, but i only can insert to one column the rest cannot.
gregaDro
Member
318 Points
65 Posts
Re: Datatable - add total amount of qty for each columns
Dec 13, 2012 09:17 AM|LINK
Just try to insert all rows in new (3rd) table instead in dTable. In for statement copy rows from dTable until Module is changed, and when it's changed insert total amount row and so on.