Unable to sort in ASP.net C#
<p>I've looked on this site, and many others and found many threads about this. However, I'm still unable to get sorting to work on this gridview. I have tried several things and nothing seems to work. What happens with the code I currently have is that it just refreshes the page but does not sort the information. I am using RowCommands to do the CRUD functions. Since I am using the RowCommands I do have a DataAccessLayer class. From what I have found, no one is doing it this way I guess? This is what I have so far for sorting.. I'm not sure what to do to get this to work properly. Any help is greatly appreciated! Thanks!</p>

<pre><code>    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            BindGridViewData();
        }
    }

    protected void BindGridViewData()
    {
        gvVehicle.DataSource = VehicleDataAccessLayer.GetAllVehicles();
        gvVehicle.DataBind();
    }

    protected void gvVehicle_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvVehicle.DataSource = VehicleDataAccessLayer.GetAllVehicles();
        gvVehicle.PageIndex = e.NewPageIndex;
        gvVehicle.DataBind();
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSort = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSort = "ASC";
                break;

            case SortDirection.Descending:
                newSort = "DESC";
                break;
        }

        return newSort;
    }

    private string GetSortDirection(string column)
    {
        string sortDirection = "DESC";
        string sortExpression = ViewState["SortExpression"] as string;

        if (sortExpression != null)
        {
            if (sortExpression == column)
            {
                string lastDirection = ViewState["SortDirection"] as string;
                if ((lastDirection != null) &amp;&amp; (lastDirection == "DESC"))
                {
                    sortDirection = "ASC";
                }
            }
        }

        ViewState["SortDirection"] = sortDirection;
        ViewState["SortExpression"] = column;

        return sortDirection;
    }

    //Used for sorting
    protected void gvVehicle_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = gvVehicle.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            gvVehicle.DataSource = dataView;
            gvVehicle.DataBind();          
        }
    }
</code></pre>
