sorting - Sort all DataTable in Dataset for vb.net -


i need sort datatables in mt dataset. have tried using defaultview, it's sorting datatable after loop datatable looks same without sorting.

this tried:

 each dt datatable in albumlistds.tables    dt.defaultview.sort = "imagedata asc"    dt = datatable.defaultview.totable    dt.acceptchanges()    albumlistds.acceptchanges()  next 

please correct me if did wrong.

the changes made datatable when inside loop local element returned iterator of each.

msdn says

modifying collection elements. current property of enumerator object readonly (visual basic), , returns local copy of each collection element. means cannot modify elements in each...next loop. modification make affects local copy current , not reflected underlying collection.

so, when recreate datatable

dt = datatable.defaultview.totable 

the new dt instance not same instance contained in dataset. , changes lost @ same moment when loop on datatable element.
in striking contrast on can in c# attempt change iterator instance caught compiler , signaled error @ compile time

perhaps change defaultview sort expression , leave datatable in original order (surely better memory usage). when need loop in ordered way, use dataview

for each drv datarowview in datatable.defaultview    console.writeline(drv("yourfield").tostring()) next 

or use normal for...loop (but in backward direction)

for x integer = albumlistds.tables.count - 1 0 step -1    dim dt = albumlistds.tables(x)    dt.defaultview.sort = "imagedata asc"    albumlistds.tables.removeat(x)    albumlistds.tables.add(dt.defaultview.totable) next albumlistds.acceptchanges 

notice need remove previous table collection (tables readonly) , add new one. safer if loop backward end of collection first element avoid possible indexing errors


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -