Pages

3 Aug 2013

Sorting in Gridview Asp.net C#

protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {    
        DataTable dtSortTable = GridView1.DataSource as DataTable;

        if (dtSortTable != null)
        {
            DataView dvSortedView = new DataView(dtSortTable);

            dvSortedView.Sort = e.SortExpression + "" + getSortDirectionString(e.SortDirection);
       
            GridView1.DataSource = dvSortedView;
            GridView1.DataBind();
        }
    }

private string getSortDirectionString(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;
        if(sortDirection== SortDirection.Ascending)
        {
            newSortDirection = "ASC";
        }
        else
        {
            newSortDirection = "DESC";
        }
        return newSortDirection;
}

No comments:

Post a Comment