c# - Sorting a datatable affects reference datatable -
i have method similar 1 written bellow(this pseudo-code working in c#):
function generatechart(datatable dt) { datatable dtcharttable = dt; dtcharttable.defaultview.sort = "somecolumnname"; //remaining functionality }
what above code sorts records in dt. not getting why doing so. note: function called 2 different places. @ 1 place sending datatable object , @ other datatable directly refered 1 stored in session.
that's right. setting dtcharttable variable same memory represented dt variable. so, sorting dtcharttable affects same defaultview property used second .
if don't want behavior create copy of dt using
datatable dtcharttable = dt.copy();
but costly because in way every datarow duplicated.
possibility creating new dataview
dataview view = new dataview(dt); view.sort = "somecolumnname"; ......
this doesn't affect original dt.defaultview , can process datarowview new dataview
Comments
Post a Comment