c# - How to calculate a formula and add a row in data table -
i have data database in datatable object , performing row calculation based on formula defined in list of string unfortunately ending wrong calculation because sequence of rows value not match given formula calculations going wrong e.x ( income before pipeline, depr/amort , msr )= net production income - total personal cost - other cost + service fee income
for in loop first value coming datatable total personal cost , second other cost third service fee income , in last net production income. form formula
income before pipeline, depr/amort , msr = total personal cost - other cost + service fee income + net production income.
please give me solution , in particular below code code performing row calculation given formula
for (int p = 0; p < dt.rows.count; p++) { rowname = dt.rows[p]["gldescription"].tostring(); addcolume[0] = ""; if (addcolume.contains(rowname)) { int l = 1; var drvalue = dt.rows[p][j]; if (!(drvalue == null || drvalue == dbnull.value)) { if (expression[index] == "+") { totalrow += convert.todecimal(drvalue); } else if (expression[index] == "-") { totalrow -= convert.todecimal(drvalue); } index++; } } else if (addcolume.contains(rowname) && addcolume.contains("self")) { var drvalue = dt.rows[p][j]; if (!(drvalue == null || drvalue == dbnull.value)) { decimal currentvalue = convert.todecimal(drvalue); dt.rows[p][j] = currentvalue * -1; } } } for support have code
list<string> lstgrouping = new list<string>(); lstgrouping.add("volume↔volume↔volume↔sigular"); lstgrouping.add("units↔units↔units↔sigular"); lstgrouping.add("blank↔blank↔sigular"); lstgrouping.add("total production revenue↔secondary-net (sell - buy +/- hedge)↔branch brokered loan income↔add"); lstgrouping.add("total direct costs↔deed recording↔provisions loan losses↔add"); lstgrouping.add("net production income↔total production revenue↔total direct costs↔+$-"); lstgrouping.add("total personnel costs↔accounting dept↔employer payroll tax exp↔add"); lstgrouping.add("total g&a costs , other↔bank fees↔provisions branch losses↔add"); lstgrouping.add("servicing fee income↔servicing fee income , net of expenses↔servicing fee income , net of expenses↔sigular"); lstgrouping.add("income before pipeline, depr/amort , msr↔net production income↔total personnel costs↔total g&a costs , other↔servicing fee income↔+$+$-$-"); lstgrouping.add("other items total↔servicing fee income , net of expenses↔depreciation expense↔add"); lstgrouping.add("net income↔income before pipeline, depr/amort , msr↔other items total↔+$+"); (int = 0; < lstgrouping.count; i++) { var addcolume = lstgrouping[i].split('↔'); var expression = addcolume[(addcolume.length) - 1].split('$'); int startindex = 0; int endindex = 0; foreach (datarow dr in dt.rows) { string gldescription = dr["gldescription"].tostring(); if (gldescription.trim() == addcolume[1].trim()) startindex = dt.rows.indexof(dr); else if (gldescription.trim() == addcolume[2].trim()) endindex = dt.rows.indexof(dr); } datarow newdr = dt.newrow(); if (expression[0] == "add") dt.rows.insertat(newdr, endindex + 1); else if (expression[0] == "sigular") { endindex = 1; } else dt.rows.add(newdr); datarow level1row = dtlevel1.newrow(); dtlevel1.rows.add(level1row); if (startindex != null && endindex != null) { (int j = 0; j < dt.columns.count; j++) { string colname = dt.columns[j].columnname; if (colname != "gldescription" && colname != "gldescriptionid") { decimal totalrow = 0; if (expression[0] == "add") { (int k = startindex; k <= endindex; k++) { var drvalue = dt.rows[k][j]; if (drvalue == null || drvalue == dbnull.value) drvalue = 0; if (expression[0].contains("add")) totalrow += convert.todecimal(drvalue); } } else if (expression[0] == "sigular") { int keyvalue = startindex; var drvalue = dt.rows[keyvalue][j]; if (drvalue == null || drvalue == dbnull.value) drvalue = 0; totalrow += convert.todecimal(drvalue); } else { int index = 0; int paraindex = 0; string rowname; (int p = 0; p < dt.rows.count; p++) { rowname = dt.rows[p]["gldescription"].tostring(); addcolume[0] = ""; if (addcolume.contains(rowname)) { int l = 1; var drvalue = dt.rows[p][j]; if (!(drvalue == null || drvalue == dbnull.value)) { if (expression[index] == "+") { totalrow += convert.todecimal(drvalue); } else if (expression[index] == "-") { totalrow -= convert.todecimal(drvalue); } index++; } } else if (addcolume.contains(rowname) && addcolume.contains("self")) { var drvalue = dt.rows[p][j]; if (!(drvalue == null || drvalue == dbnull.value)) { decimal currentvalue = convert.todecimal(drvalue); dt.rows[p][j] = currentvalue * -1; } } } } newdr[colname] = totalrow; level1row[colname] = totalrow; } else if (colname == "gldescription") { newdr[colname] = addcolume[0]; level1row[colname] = addcolume[0]; } } } } dtlevel2 = dt; if (level == "lev1") return dtlevel1; else if (level == "lev2") return dt; else return null;
Comments
Post a Comment