2

If I have a GridView like below. How can I in the simplest way sort by ID in (always) DESC order? Do I need to have a SortExpression? I'm really new to this, so asking to learn.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowSorting="true" OnSorting="gridView_Sorting">
            <Columns>
                <asp:HyperLinkField DataTextField="ID" DataNavigateUrlFields="ID"
                    DataNavigateUrlFormatString="CrimeCoordinator.aspx?ID={0}" Text="Lead ID"
                    HeaderText="Ärendenummer" />
                <asp:BoundField DataField="Employee" HeaderText="Handläggare" />
            </Columns>
</asp:GridView>
guitarzero
  • 545
  • 9
  • 18
  • Normally you would sort the datasource before you assign the datasource to your gridview. What kind of datasource are you using? – Kim K. Nov 17 '13 at 15:39
  • For now just hardcoded objects. – guitarzero Nov 17 '13 at 15:44
  • if your hardcoded objects is a list of objects, I recommend to sort the list of objects first and then assign the list of objects to the gridview's datasource. You can sort the list using Linq [StackOwerflow How to sort a list](http://stackoverflow.com/questions/3309188/c-net-how-to-sort-a-list-t-by-a-property-in-the-object) – Kim K. Nov 17 '13 at 15:51

2 Answers2

1

With a list of objects you can sort the list by a property of the object. you should sort the list in your codebehind before assigning the list as datasource to the gridview.

Here is an example of how to sort your list of employees Descending by ID. The sorting is executed by linq so remember to add linq as reference in your codebehind.

using System.Linq;

...

/* your list of hardcoded employees */
list<object> listEmployees = your_list;

/* Sort the list by using linq and save it as sortedEmployees
   The Sorting is done based on the property ID */
list<object> sortedEmployees = listEmployees.OrderByDescending(t => t.ID);

/* set the datasource of your gridview */
GridView1.DataSource = sortedEmployees;

...
Kim K.
  • 263
  • 2
  • 10
0

Since you are using a list of objects as your data source, then you need to implement your own sorting logic, like this:

public sealed class GenericComparer<T> : IComparer<T>
{
    public enum SortOrder
    {
        Ascending = 0,
        Descending = 1
    }

    private string sortColumn;
    private SortOrder sortingOrder;

    public string SortColumn
    {
        get
        {
            return this.sortColumn;
        }
    }

    public SortOrder SortingOrder
    {
        get
        {
            return this.sortingOrder;
        }
    }

    public GenericComparer(string theSortColumn, SortOrder theSortingOrder)
    {
        this.sortColumn = theSortColumn;
        this.sortingOrder = theSortingOrder;
    }

    public int Compare(T x, T y)
    {
        PropertyInfo thePropertyInfo = typeof(T).GetProperty(this.sortColumn);
        IComparable object1 = (IComparable)thePropertyInfo.GetValue(x, null);
        IComparable object2 = (IComparable)thePropertyInfo.GetValue(y, null);
        if (this.sortingOrder == SortOrder.Ascending)
        {
            return object1.CompareTo(object2);
        }
        else
        {
            return object2.CompareTo(object1);
        }
    }
}

Now in your call to .Sort() method on your list of objects, you pass a new instance of this helper class (passing it the property of your list you want to sort by and the direction you want to sort - ascending or descending).

Since the comparer logic above uses generics, you can pass whatever type you want to sort by (i.e. int, DateTime, or even entire domain objects).

Karl Anderson
  • 34,606
  • 12
  • 65
  • 80