Exemple #1
0
        public static void LoadSettings(Telerik.Web.UI.RadGrid gridInstance, eGrid grid, out IEnumerable <string> columnsToHide, string userName)
        {
            var settingsRow = DataAccess.SystemSettings.GetGridSettings(userName, (int)grid).Tables[0].AsEnumerable().FirstOrDefault();

            var settings = string.Empty;

            columnsToHide = Enumerable.Empty <string>();

            if (settingsRow != null)
            {
                if (!settingsRow.IsNull("DATA"))
                {
                    settings = settingsRow.Field <string>("DATA");
                }

                if (!settingsRow.IsNull("ColumnsToHide"))
                {
                    columnsToHide = settingsRow.Field <string>("ColumnsToHide").Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
            }

            var masterTable = gridInstance.MasterTableView;
            var allColumns  = masterTable.Columns.Cast <GridColumn>().Concat(masterTable.AutoGeneratedColumns);

            if (!string.IsNullOrWhiteSpace(settings) || columnsToHide.Any())
            {
                var gridState = GridState.Deserialize(settings);

                #region Old Load Method

                // The following 5 lines are only in place to allow people to upgrade from any older version of HE to the latest version without losing their saved grid settings.
                // For any future release of HE these saved settings will have been migrated to the new format so these lines can be removed.
                if (gridState == null)
                {
                    LoadSettings_OldMethod(gridInstance, settings, columnsToHide);
                    SaveGridSettings(gridInstance, grid, userName);
                    return;
                }

                #endregion Old Load Method

                var losFormatter = new LosFormatter();

                var deserializeViewState = new Action <string, IStateManager>((s, sm) =>
                {
                    using (var reader = new StringReader(s))
                    {
                        try
                        {
                            var viewState = losFormatter.Deserialize(reader);
                            sm.LoadViewState(viewState);
                        }
                        catch
                        {
                            // Don't throw an exception if the grid data can't be deserialised... sometimes this can happen if the Telerik controls have been updated to a more recent version.
                        }
                    }
                });

                // Load group by expressions
                var groupByExpressions = masterTable.GroupByExpressions;
                groupByExpressions.Clear();

                foreach (var vs in gridState.GroupByExpressionViewStates)
                {
                    var expression = new GridGroupByExpression();
                    deserializeViewState(vs, expression);
                    groupByExpressions.Add(expression);
                }

                // Load sort expressions
                var sortExpressions = masterTable.SortExpressions;
                sortExpressions.Clear();
                deserializeViewState(gridState.SortExpressionsViewState, sortExpressions);

                // Load filter expression
                masterTable.FilterExpression = gridState.FilterExpression;

                foreach (var column in allColumns)
                {
                    if (gridState.ColumnStates.ContainsKey(column.UniqueName))
                    {
                        var columnState = gridState.ColumnStates[column.UniqueName];
                        column.OrderIndex      = columnState.OrderIndex;
                        column.ItemStyle.Width = column.HeaderStyle.Width = columnState.Width;
                        column.Visible         = columnState.Visible;
                    }
                }
            }

            columnsToHide = from c in allColumns where !c.Visible select c.UniqueName;
        }