What would be the best way to physically sort the contents of a DataTable?

  • Thread starter Thread starter Maxwell2006
  • Start date Start date
M

Maxwell2006

Hi,



What would be the most efficient way to physically sort the contents of a
DataTable?



Of course dsCashReceipts.CashReceiptOutBalList.DefaultView.Sort doesn't
work, because I need to phusically sort the DataTable.



Thank you,

Max
 
Either hit the DB with a query that has an order by statement, or manually
loop through and create a new value sorted like you want.
 
Hi Max,

Looks like the easiest way is the Select() method which returns a DataRow
array, in the order specified - use the (filter, sort) overload, with a
bogus filter expression (like "a=a", or perhaps it will accept String.Empty)

If you are in VS 2005 and you absolutely must have a data table with its
rows in the order, use the view to sort, and then use the new ToTable method
to create a new data table that matches the view. I am not aware of any way
to change the order of rows in a data table.

If not in VS 2005, you can always do it the hard way: (air code alert)

Dim oTbl as DataTable = dsCashReceipts.CashReceiptOutBalList.Clone()
For Each oRow As DataRowView in
dsCashReceipts.CashReceiptOutBalList.DefaultView
oTbl.ImportRow(oRow.Row)
Next
dsCashReceipts.CashReceiptOutBalList = oTbl

Cheers
 
Hi Max,

Neither the DataTable nor the database table can be sort phisically. If you
really need to do this, you have to

1. Logically sort the DataTable using DataView.
2. Create a new DataTable and put all the contents in the DataView into it.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top