Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
400
Hidden prefix for sorting
posted

I have a column of data, consisting of fields like S, M, L, XL (small, medium, large, extra large). I would like these to sort in size order (S,M,L,XL) instead of alphabetical order. Note that, depending on the context of the data, sometimes an M will sort before the L, but in other contexts the L could sort before the M.

But from our database it would be easy for me to prefix these values: 03S, 04M, 05L, 06XL. Then they would sort correctly, based on the context of the data. But I would obviously not like the prefix to show on the display.

Is it possible to set the Format property of the column to strip off the first two characters? Or is there some other easy way to solve this problem?

  • 37774
    Suggested Answer
    posted

    There are a couple options that you have here.  The easiest solution would be to create a class that implements the IComparer interface, then assign an instance of that class as the SortComparer property on the column.  There is an example of this in the Knowledge Base.  You could either have your class hard-code the values so that it knows that S < M and so on, or you could have a separate column that actually has your prefix values and use that column for the values to sort by (hiding this column from view).  Should you decide to take the latter approach, you could have your DB return the prefix values and in the InitializeLayout event of the grid, add an unbound column and hide the DB column; in the InitializeRow event, you would copy over the values, stripping out the prefixes (i.e. via the Substring method).

    -Matt