c# - Unable to Copy datacolumn from one data table to another -
how can copy 1 data column 1 data table new datatable. when try it, error column 'xxx' belongs datatable.?
datacolumn = datatable1.columns[1]; datatable2 = new datatable(); datatable2.columns.add(datacolumn);
thanks in advance
you cannot copy datacolumns. you'll need create new datacolumn in new datatable same data type in old datatable's column, , need run loop bring in data old datatable new datatable.
see following code. assumes datatables have same number of rows.
datatable dt1 = new datatable(); datatable dt2 = new datatable(); dt2.columns.add("columna", dt1.columns["columna"].datatype); (int = 0; < dt1.rows.count; i++) { dt2.rows[i]["columna"] = dt1.rows[i]["columna"]; }
also, if data copying reference types , not value types might want see if .clone() method available type, or make 1 yourself. doing 'this = that' in loop not work on reference types.
Comments
Post a Comment