c# - Dynamic data table creation from a dymanic data source -
i need create datatable on fly datable such need iterate through rows of table a, calculations on column values , write results table b. illustration table has 5 columns (but in reality number of columns varies) starting sec column (a), each column has numeric value. need add together them , write total table b.
witht munber of clumns in table b alter in compared table not rows. info not going displayed on page in gridview or similar form can't utilize onevent method, please don't suggest other alternate ways, because next process requires datatable produce desired outcome.
| | b | c | d | e | (table a) ------------------------------- entries | 2 | 6 | 100 | 0 | 5 | | totals | (table b) entries | 113 |
i've seen several examples suggesting progamatic info creation type of code:
datatable dt; protected void page_load(object sender, eventargs e) { dt = new datatable("tbltest"); datacolumn dc1 = new datacolumn(); dc1.datatype = typeof(string); dc1.columnname = "name"; datacolumn dc2 = new datacolumn(); dc2.datatype = typeof(string); dc2.columnname = "add1"; datacolumn dc3 = new datacolumn(); dc3.datatype = typeof(string); dc3.columnname = "add2"; dt.columns.add(dc1); dt.columns.add(dc2); dt.columns.add(dc3); } protected void button1_click(object sender, eventargs e) { session["name"] += txtname.text + "|"; session["add1"] += txtadd1.text + "|"; session["add2"] += txtadd2.text + "|"; createtable(); } public void createtable() { string[] sa = session["name"].tostring().split('|'); string[] sb = session["add1"].tostring().split('|'); string[] sc = session["add2"].tostring().split('|'); int recordnum = sa.length; (int j = 0; j < recordnum - 1; j++) { datarow dr = dt.newrow(); dr["name"] = sa[j].tostring(); dr["add1"] = sb[j].tostring(); dr["add2"] = sc[j].tostring(); dt.rows.add(dr); } gridview1.datasource = dt.defaultview; gridview1.databind(); }
but not dynamic , can't wrap hear around this. i've started foreach loop iterate through rows , cells of table not sure how go populating individual rows , cells in table b.
foreach (datarow row in _dt.rows) { (int = 2; < row.itemarray.length; i++ ) { if (row.itemarray[i] != dbnull.value) _total += convert.toint32(row.itemarray[i]); } }
thanks.
risho
latest update: writing post tiny little lighbulbs turning on in head , produced piece of code. not error out doesn't work either. test purposes tried bind gridview see info i'm getting - no data. statis column names visible in debugger , know source info table has data. here code yor inspection:
datatable _newdt = new datatable(); datacolumn _dc1 = new datacolumn(); _dc1.datatype = typeof(string); _dc1.columnname = "wuc"; datacolumn _dc2 = new datacolumn(); _dc2.datatype = typeof(string); _dc2.columnname = "wuc entries"; datacolumn _dc3 = new datacolumn(); _dc2.datatype = typeof(string); _dc3.columnname = "total entries"; _newdt.columns.add(_dc1); _newdt.columns.add(_dc2); int _total = 0; foreach (datarow row in _dt.rows) { datarow _dr = _newdt.newrow(); datacolumn col1 = new datacolumn(); col1.datatype = typeof(string); col1.columnname = row.itemarray[0].tostring(); datacolumn col2 = new datacolumn(); col2.datatype = typeof(string); col2.columnname = row.itemarray[1].tostring(); datacolumn col3 = new datacolumn(); col3.datatype = typeof(int); (int = 2; < row.itemarray.length; i++ ) { if (row.itemarray[i] != dbnull.value) _total += convert.toint32(row.itemarray[i]); } col3.columnname = _total.tostring(); }
update #2:
i'v made next changes , new error popped up:
"input string not in right format.couldn't store in wuc entries column. expected type int32."
the issue i've delcared column string, info parent table in string format, , don't understand why expects int32. (?) here new version:
datatable _newdt = new datatable(); datarow _dr; datacolumn _dc1 = new datacolumn(); _dc1.datatype = typeof(string); _dc1.columnname = "wuc"; datacolumn _dc2 = new datacolumn(); _dc2.datatype = typeof(string); _dc2.columnname = "wuc entries"; datacolumn _dc3 = new datacolumn(); _dc2.datatype = typeof(int32); _dc3.columnname = "total entries"; _newdt.columns.add(_dc1); _newdt.columns.add(_dc2); int _total = 0; foreach (datarow row in _dt.rows) { _dr = _newdt.newrow(); _dr["wuc"] = row.itemarray[0].tostring(); _dr["wuc entries"] = row.itemarray[1].tostring(); (int = 2; < row.itemarray.length; i++ ) { if (row.itemarray[i] != dbnull.value) _total += convert.toint32(row.itemarray[i]); } _dr["total entries"] = _total; _newdt.rows.add(_dr); }
well, after much trial , error able create error go away changing trird column typeof(string), didn't impact outcome of data.
but sense didn't solved issue , bottom of other time.
c# asp.net
No comments:
Post a Comment