This is a migrated thread and some comments may be shown as answers.

Sorting and Dynamic Columns

1 Answer 74 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 19 Nov 2012, 08:22 PM
I am running into issues when my dynamically bound grid has been sorted by Column A and on a post back, Column A is no longer present due to a user-preference change.  I would like to clear the sorting if the column no longer exists so that I don't get an error on the page.  Is there a way to do this?

Thanks!
<telerik:RadGrid ID="rg" runat="server" Skin="Office2010Silver" OnNeedDataSource="rg_NeedDataSource"
 
OnItemCommand="rgScores_ItemCommand">
 
<ExportSettings HideStructureColumns="true" />
 
<MasterTableView AllowPaging="true" AllowSorting="true" Width="100%" CommandItemDisplay="Top">
 
<CommandItemSettings ShowExportToExcelButton="true" ShowRefreshButton="false" ShowAddNewRecordButton="false" />
 
</MasterTableView>
 
</telerik:RadGrid>


1 Answer, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 1
answered on 19 Nov 2012, 09:11 PM
We were able to come up with a solution:
protected void rg_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
   {
       rg.DataSource = null;
 
       DataTable dt;
 
       using (MyDB db = new MyDB())
           dt = db.GetData();
 
       for (int i = 0; i < rg.MasterTableView.SortExpressions.Count; i++)
       {
           if (!dt.Columns.Contains(rg.MasterTableView.SortExpressions[i].FieldName))
               rg.MasterTableView.SortExpressions.RemoveSortExpression(rg.MasterTableView.SortExpressions[i]);
       }
 
       rg.DataSource = dt;
   }

Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Daniel
Top achievements
Rank 1
Share this question
or