0

I have datatable which includes these values: enter image description here

Currently I'm sorting this way:

customTable.DefaultView.Sort = "Module asc";

I want to move the Intro be be the first module included and then by number starting from 1 the other modules, how can I do that?

halfer
  • 19,824
  • 17
  • 99
  • 186
Laziale
  • 7,965
  • 46
  • 146
  • 262
  • Do you have any control over the structure of the datatable? If so, you could add an Expression field which is an IF() that looks for Intro and if it finds it, uses 0, otherwise uses the Module number. Then sort by that column. – Forty3 Feb 28 '17 at 16:27
  • I have complete control over the structure, what do you propose, can you please give me a code sample? Thx – Laziale Feb 28 '17 at 16:28
  • Possible duplicate of [DataView.Sort - more than just asc/desc (need custom sort)](http://stackoverflow.com/questions/582374/dataview-sort-more-than-just-asc-desc-need-custom-sort) – Wagner DosAnjos Feb 28 '17 at 16:29
  • Thanks @wdosanjos - in your posted link is a similar answer to mine: http://stackoverflow.com/a/583943/3485669 – Forty3 Feb 28 '17 at 16:41

1 Answers1

0

Using an Expression field on the DataTable, you can contain the sort within the DataTable itself (should you be passing it along to other consumers):

dt.Columns.Add("xxSort", 
               (123).GetType(), 
               "IIF([Module] = 'Intro', 0, Convert([Module], 'System.Int32'))");
dt.DefaultView.Sort = "xxSort ASC";
Forty3
  • 2,199
  • 1
  • 14
  • 19