Friday, April 27, 2012

We have a requirement to sort datagrid columns such a way that blank spaces should always stay to the bottom. I have tried many solutions but in vain. Finally, an extra property in Business Entity and small LINQ statement solved the problem. Here is how I solved it:

1. Declare a new property in Business entity and call it PropertySortOrder assuming the name of column you are intended to sort is Property.

2. If Property has a value set the value of PropertySortOrder to 1, else if the property has blank value set it to Zero (0).

3. Then use LINQ query with two "order by" clauses.

4. If you want ascending sort of rows with values leaving blank values to the end, the LINQ query with order by clause would be PropertySortOrder ascending, Property ascending

5. If you want descending sort of rows with values leaving blank values to the end, the LINQ query with order by clause would be PropertySortOrder ascending, Property descending.

Here is the sample code:

No comments:

Post a Comment

Combinable Enumerations         [Flags]         public enum SomeFlags         {             NoColor = 0,             Red = 1,        ...