asp.netsortingdatagridviewpaginationdatagrid

How to keep column sorting when clicking on new page index for datagrid


I have a datagrid that has 3 pages of data. I have allowed paging and sorting. They both work ok except with one issue with the sorting.

The sorting is applied correctly when I click a column header but then if I then go and click on the page index the sorting is reset.

Any ideas on how to preseve the sorting while swiching pages on a datagrid?

 protected void dgCompInfo_OnPageIndexChanging(object sender, DataGridPageChangedEventArgs e)
    {      
        dgCompInfo.CurrentPageIndex = e.NewPageIndex;          
        dgCompInfo.DataSource = GetDataTableForGrid();
        dgCompInfo.DataBind();        
    }

Solution

  • What I ended up doing is adding the DataTable used for the grid to the Session and used that on the binding of the OnPagingIndexChanging.

    Works like a champ now!!