[ASP.NET][Telerik] Export and Import RadGrid Setting

[ASP.NET][Telerik] Export and Import RadGrid Setting

我需要將 RadGrid 的狀態記下來,比如說,目前在第幾頁,過濾條件等等..

花了一點時間把所需要的資料倒出來,然後在 NeedDataSource 事件裡面調用 ExportGridSetting


{
    var gridTableView = grid.MasterTableView;

    var setting = new GridSetting();
    setting.Filter = gridTableView.FilterExpression;
    setting.OrderBy = gridTableView.SortExpressions.GetSortString();
    setting.Skip = gridTableView.CurrentPageIndex * gridTableView.PageSize;
    setting.Take = gridTableView.PageSize;
    setting.PageIndex = gridTableView.CurrentPageIndex;

    var sortCommands = this.ExportSortCommands(grid);
    var filterCommands = ExportFilterCommands(grid);

    setting.SortCommands = sortCommands;
    setting.FilterCommands = filterCommands;
    return setting;
}

 

匯出 FilterCommand


{
    var filterCommands = new List<FilterCommand>();
    foreach (GridColumn column in grid.MasterTableView.Columns)
    {
        if (column is GridBoundColumn == false)
        {
            continue;
        }

        filterCommands.Add(new FilterCommand()
        {
            PropertyName = column.UniqueName,
            FunctionName = column.CurrentFilterFunction.ToString(),
            FilterValue = column.CurrentFilterValue
        });
    }
    return filterCommands;
}

 

對照畫面取出所需的資料

SNAGHTML5931dc

 

匯出 SortCommand


{
    var sortCommands = new List<SortCommand>();
    foreach (var sort in grid.MasterTableView.SortExpressions.Cast<GridSortExpression>())
    {
        EnumOrder order;
        if (!EnumOrder.TryParse(sort.SortOrder.ToString(), out order))
        {
            continue;
        }
        sortCommands.Add(new SortCommand()
        {
            PropertyName = sort.FieldName,
            Order = order
        });
    }
    return sortCommands;
}

 

有匯出當然就會有匯入囉,可以在 Page_Load 事件裡調用 ImportGridSetting 方法


{
    var gridTableView = radGrid.MasterTableView;
    gridTableView.FilterExpression = setting.Filter;
    gridTableView.PageSize = setting.Take;
    gridTableView.CurrentPageIndex = setting.PageIndex;

    this.ImportFilterCommands(radGrid, setting.FilterCommands);
    this.ImportSortCommands(radGrid, setting.SortCommands);
}

 


{
    var gridTableView = grid.MasterTableView;
    foreach (var command in filterCommands)
    {
        GridColumn column = gridTableView.GetColumnSafe(command.PropertyName);
        GridKnownFunction function;
        if (!GridKnownFunction.TryParse(command.FunctionName, out function))
        {
            continue;
        }
        column.CurrentFilterFunction = function;
        column.CurrentFilterValue = command.FilterValue;
    }
}



public void ImportSortCommands(RadGrid grid, IEnumerable<SortCommand> sortCommands)
{
    grid.MasterTableView.SortExpressions.Clear();
    foreach (var element in sortCommands)
    {
        GridSortOrder order;
        if (!GridSortOrder.TryParse(element.Order.ToString(), out order))
        {
            continue;
        }

        var expression = new GridSortExpression()
        {
            FieldName = element.PropertyName,
            SortOrder = order
        };
        grid.MasterTableView.SortExpressions.Add(expression);
    }
}

 

其餘的 DTO


{
    public string PropertyName { get; set; }

    public string FunctionName { get; set; }

    public string FilterValue { get; set; }
}

public class SortCommand
{
    public string PropertyName { get; set; }
    public EnumOrder Order { get; set; }
}

public enum EnumOrder
{
    None,
    Ascending,
    Descending,
}

public class GridSetting
{
    public GridSetting()
    {
        this.Take = 10;
        this.FilterCommands = new List<FilterCommand>();
        this.SortCommands = new List<SortCommand>();
    }

    public string Filter { get; set; }

    public string OrderBy { get; set; }

    public int Skip { get; set; }//CurrentPageIndex*PageSize

    public int Take { get; set; }//PageSize

    public int PageIndex { get; set; }

    public int RowIndex { get; set; }

    public IEnumerable<FilterCommand> FilterCommands { get; set; }

    public IEnumerable<SortCommand> SortCommands { get; set; }
}


文章出自:http://www.dotblogs.com.tw/yc421206/archive/2015/08/31/153260.aspx

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo