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

How to persist RadGrid Sorting

2 Answers 167 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Herman Gouw
Top achievements
Rank 2
Herman Gouw asked on 02 Jul 2010, 03:20 PM
Hi,

In my current project, I have to write a web page containing a RadGrid with Persisted Sorting.

I have implement the web page similar to as follows RadGrid with Persisted Sorting

The RadGrid's SortExpressions are persisted in the Settings which are saved (i.e. serialized) on Page_UnLoad and loaded (i.e. deserialized) on Page_Load.

I created the class SortExpressions instead of using Telerik's GridSortExpression because GridSortExpression is not marked as Serializable.

The web page works as expected. However, can you please review my code if I have implemented it correctly?

The code is given as follows:

ASPX
<form id="form1" runat="server"
<div> 
    <telerik:RadScriptManager ID="radScriptManager" runat="server" /> 
    <telerik:RadGrid ID="radGrid" AutoGenerateColumns="false" AllowSorting="true" OnNeedDataSource="radGrid_NeedDataSource" runat="server"
        <ClientSettings> 
            <Scrolling UseStaticHeaders="true" /> 
        </ClientSettings>  
        <MasterTableView AllowMultiColumnSorting="true" TableLayout="Fixed"
            <Columns> 
                <telerik:GridBoundColumn DataField="Name" DataType="System.String" HeaderText="Name" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="Name" AllowSorting="true" UniqueName="Name" /> 
                <telerik:GridBoundColumn DataField="Founder" DataType="System.String" HeaderText="Founder" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" SortExpression="Founder" AllowSorting="true" UniqueName="Founder" /> 
            </Columns> 
        </MasterTableView> 
    </telerik:RadGrid>   
</div> 
</form> 

C#
public partial class RadGridWithSorting : Page 
    private const string SettingsFilename = "Settings.bin"
    private Settings _settings = null
    private List<Company> _companies = null
 
    protected Settings Settings 
    { 
        get 
        { 
            if (this._settings == null
            { 
                this._settings = new Settings(); 
            } 
            return this._settings; 
        } 
        set 
        { 
            this._settings = value; 
        } 
    } 
 
    protected void Page_Load(object sender, EventArgs e) 
    { 
        LoadSettings(); 
        LoadSortExpressions(); 
 
        if (!IsPostBack) 
        { 
            this._companies = Company.Load(); 
            Session["COMPANIES"] = this._companies; 
        } 
        else 
        { 
            this._companies = (List<Company>)Session["COMPANIES"]; 
        } 
    } 
 
    protected void Page_UnLoad(object sender, EventArgs e) 
    { 
        SaveSortExpressions(); 
        SaveSettings(); 
    } 
 
    protected void radGrid_NeedDataSource(object source, GridNeedDataSourceEventArgs e) 
    { 
        this.radGrid.DataSource = this._companies; 
    } 
 
    private void LoadSettings() 
    { 
        Settings = Helper.Load(SettingsFilename); 
    } 
 
    private void LoadSortExpressions() 
    { 
        this.radGrid.MasterTableView.SortExpressions.Clear(); 
        foreach (SortExpression sortExpression in Settings.SortExpressions) 
        { 
            GridSortExpression gridSortExpression = new GridSortExpression(); 
            gridSortExpression.FieldName = sortExpression.FieldName; 
            gridSortExpression.SortOrder = sortExpression.SortOrder; 
            this.radGrid.MasterTableView.SortExpressions.AddSortExpression(gridSortExpression); 
        } 
    } 
 
    private void SaveSettings() 
    { 
        Helper.Save(Settings, SettingsFilename); 
    } 
 
    private void SaveSortExpressions() 
    { 
        Settings.SortExpressions.Clear(); 
        foreach (GridSortExpression sortExpression in this.radGrid.MasterTableView.SortExpressions) 
        { 
            Settings.SortExpressions.Add(new SortExpression(sortExpression.FieldName, sortExpression.SortOrder)); 
        } 
    } 
 
[Serializable] 
public class Settings 
    public List<SortExpression> SortExpressions { getset; } 
 
    public Settings() 
    { 
        SortExpressions = new List<SortExpression> {}; 
    } 
    public Settings(List<SortExpression> sortExpressions) 
    { 
        SortExpressions = sortExpressions; 
    } 
 
[Serializable] 
public class SortExpression 
    public string FieldName { getset; } 
    public GridSortOrder SortOrder { getset; } 
 
    public SortExpression() 
    { 
        FieldName = null
        SortOrder = GridSortOrder.None; 
    } 
    public SortExpression(string fieldName, GridSortOrder sortOrder) 
    { 
        FieldName = fieldName; 
        SortOrder = sortOrder; 
    } 

Thanks,
Herman

2 Answers, 1 is accepted

Sort by
0
Accepted
Pavlina
Telerik team
answered on 02 Jul 2010, 05:00 PM
Hi Herman,

I went through your code and it looks correct. However, I suggest that you review the following online example which demonstrates saving and restoring RadGrid settings by user.
http://demos.telerik.com/aspnet-ajax/grid/examples/programming/savinggridsettingsonperuserbasis/defaultcs.aspx

Greetings,
Pavlina
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Herman Gouw
Top achievements
Rank 2
answered on 06 Jul 2010, 08:15 AM
Thanks Pavlina.
Tags
Grid
Asked by
Herman Gouw
Top achievements
Rank 2
Answers by
Pavlina
Telerik team
Herman Gouw
Top achievements
Rank 2
Share this question
or