5

I'm inserting a special (summary) row into a DataTable, and I want it to appear last in a sorted DataView. I know the DataView is being sorted (ascending) by a particular string column, and for display purposes it doesn't matter what value appears in that column of the summary row.

What string can I place in that field to make sure my summary row gets sorted to the very end? For columns of nearly any other datatype, I could use T.MaxValue, but there unfortunately is no System.String.MaxValue constant.

I already tried

footerRow[colGrouping] = "\uFFFF";

but that sorted to the top! (DataView sorting is lexicographical, not based on numeric value of the codepoint)

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • The lexicographical ordering will be culture-dependent, so this might be difficult to do in the general case. However, [someone reckons you can use `U+E83A`](http://stackoverflow.com/a/9492832/106159) – Matthew Watson Nov 03 '16 at 16:35
  • Maybe you know what culture\language other strings are? – Evk Nov 03 '16 at 16:38
  • Why not put it as last column instead of last row ? – mybirthname Nov 03 '16 at 16:39
  • @MatthewWatson: Can I determine what culture `DataView` uses for sorting (or better yet, force it to `InvariantCulture`)? – Ben Voigt Nov 03 '16 at 16:39
  • As per that answer I linked, you could try using `""` (sic). (I don't think you could change the locale used by `DataView` without changing the thread locale) – Matthew Watson Nov 03 '16 at 16:40
  • @mybirthname: It just wouldn't make sense that way. The summary is done across rows. – Ben Voigt Nov 03 '16 at 16:41
  • 1
    @BenVoigt okay you have a point, my suggestion was stupid. Are you giving this dataview to grid or something like this? Why not just add new grid/table and give the summary row to it. Sorting by some _magic string_ sounds really hacky. – mybirthname Nov 03 '16 at 16:46
  • 2
    This sounds like a bad idea from the start. I expect the data in a table to be data in the table, not data in the table plus some other special thing. I would be more inclined to solve the problem by separating the data and the special thing, sort the data, and concatenate the special thing to the end. – Eric Lippert Nov 03 '16 at 16:52
  • @Eric: I would love to avoid this hack, either by having the sort algorithm check for the special row and sort it to the end regardless of data, or by keeping it separate in the object model and adding it at the UI. Unfortunately, the Microsoft-provided components seem to have code that tests for and prevents that approach. `DataGridView` allows you to add rows manually... unless you are using data-binding. There are hooks allowing custom sorting logic... unless your `DataGridView` is using data-binding. And `DataView` doesn't have such a hook. – Ben Voigt Nov 03 '16 at 17:36
  • @mybirthname: Yes, the `DataView` is a data source for a `DataGridView`. By putting the summary rows inside the `DataTable`, the `DataGridView` takes care of scrolling. If I make separate UI widgets I would need to write some complex scrolling logic to get scrolling to first move through the table data and then when the end is reached, reduce the vertical space given to table data and show the summary block. As ugly as this hack is, reusing the known-good scrolling logic in a `DataGridView` with variable row sizes is much better than reinventing it all introducing some bugs in the process. – Ben Voigt Nov 03 '16 at 17:42
  • Is there anything that prevents you from adding a column ("IsLastRow") to the `DataTable ` and sorting on that column first (`Sort="IsLastRow Asc, SortColumn Asc").? – TnTinMn Nov 03 '16 at 17:47
  • @TnTinMn: I know that does also work, because I'm doing it in order to get my subgroup summary rows sorted in the right place. However, a warning to anyone considering the "extra, invisible column" approach -- `DataGridView` columns with `SortMode = Automatic` don't generate any event when the sort order changes such that you could stick grouping columns in front of the user-selected one (you get the `DataBindingCompleted` after all the sorting work has been done wrong). So you have to use `SortMode = Programmatic` and handle the `ColumnHeaderCellClicked` event, handle asc/desc, update glyph – Ben Voigt Nov 03 '16 at 17:55
  • Another case where a simple extension point (event) between "DataGridView needs to change the sort" and "set the `Sort` property on the `DataView`" would have made life much much easier. The whole `IsHandled`-property on an XyzEventArgs design pattern makes it really easy to provide default behavior while allowing the developer to override it when necessary. – Ben Voigt Nov 03 '16 at 17:57
  • Possible duplicate of [What character to use to put an item at the end of an alphabetic list?](http://stackoverflow.com/questions/8086375/what-character-to-use-to-put-an-item-at-the-end-of-an-alphabetic-list) – Brian Nov 03 '16 at 20:23
  • @Brian: It's similar but different, sorting rules in .NET `DataView` are not the same as sorting rules for filenames (and even those may vary from application to application) nor restricted to characters allowed in filenames. – Ben Voigt Nov 03 '16 at 20:34

1 Answers1

1

If I were to wing it without understanding lexicographical ordering I would say \uFF70

var table = new DataTable("table");
table.Columns.Add("column");

for (int i = 0; i < 65536; i++)
{
    var row = table.NewRow();
    row["column"] = (char)i;
    table.Rows.Add(row);
}

var view = new DataView(table);
view.Sort = "column";
var last = ((string)view[65535]["column"])[0];
//last is 0xff70 'ー'    
Weyland Yutani
  • 4,682
  • 1
  • 22
  • 28