Wednesday, 7 October 2009

Sorting a datatable

Here's another of my series of 'How To Do Things In .Net That Took A Bit Of Finding' ....

You have some data in a datatable, and you want to re-sort it.

Usually, of course, you would order your original data extract in SQL using the ORDER BY clause; but there are circumstances in which that either isn't practical or possible.

Not a problem: if dt is your datatable, and Surname, Forename, and RecordID are all columns in your datatable, just go
dt.DefaultView.Sort() = "Surname, Forename, RecordID"
or even specify the sort direction
dt.DefaultView.Sort() = "Surname, Forename, RecordID DESC"
Simples!

0 comments: