override public void SaveConfig() { try { string filename = GetStateConfigFileName(DlgMode); OLVStateAdv state = new OLVStateAdv(); SaveUserSettings(state.UserSettings); if (listView != null) { OLVHelper.SaveStateAdvanced(listView, state); } state.HotItemStyle = HotItemStyle; //use save with defaults (sort column = 0); string newConfigStr = JsonSerializeHelper.SaveToStringWithNullAndDefaultValues(state); if (newConfigStr.Equals(configFileStr) == false) { File.WriteAllText(filename, newConfigStr, Encoding.UTF8); configFileStr = newConfigStr; //Log.ProcessDebug("@@@@@ Saved config for OLV list " + SourceObjectType); } } catch (Exception ex) { Log.LogError("Can not write config for " + GetType().ToString(), ex); } }
private void LoadConfig() { try { string filename = null; if (DlgMode) { filename = GetStateConfigFileName(true); if (!File.Exists(filename)) { filename = GetStateConfigFileName(false); } } else { filename = GetStateConfigFileName(false); } if (File.Exists(filename)) { configFileStr = File.ReadAllText(filename, Encoding.UTF8); OLVStateAdv state = JsonSerializeHelper.LoadFromString <OLVStateAdv>(configFileStr); if (listView != null) { OLVHelper.RestoreStateAdvanced(listView, state); } HotItemStyle = state.HotItemStyle; OLVHelper.SetHotItemStyle(listView, HotItemStyle); LoadUserSettings(state.UserSettings); } } catch (Exception ex) { Log.LogError("Can not read config for " + GetType().ToString(), ex); } }
/// <summary> /// Restore the state of the control from the given string, which must have been /// produced by SaveState() /// </summary> /// <param name="state">A byte array returned from SaveState()</param> /// <returns>Returns true if the state was restored</returns> public static void RestoreStateAdvanced(ObjectListView olv, OLVStateAdv olvState) { // The number of columns has changed. We have no way to match old // columns to the new ones, so we just give up. if (olvState == null) { //|| olvState.NumberOfColumns != ovl.AllColumns.Count)// return; } if (olvState.SortColumn == -1) { olv.PrimarySortColumn = null; olv.PrimarySortOrder = SortOrder.None; } else { if (olvState.SortColumn < olv.AllColumns.Count)// { olv.PrimarySortColumn = olv.AllColumns[olvState.SortColumn]; olv.PrimarySortOrder = olvState.LastSortOrder; } } // olv.RowHeight = olvState.RowHeight; olv.CellEditActivation = olvState.CellEditActivation; // foreach (var columnState in olvState.Сolumns) { foreach (var column in olv.AllColumns) { if (column.Name.Equals(columnState.Name)) { column.IsVisible = columnState.IsVisible; column.LastDisplayIndex = columnState.LastDisplayIndex; column.Width = columnState.Width; break; } } } // ReSharper disable RedundantCheckBeforeAssignment if (olvState.IsShowingGroups != olv.ShowGroups) { // ReSharper restore RedundantCheckBeforeAssignment olv.ShowGroups = olvState.IsShowingGroups; } if (olv.View == olvState.CurrentView) { olv.RebuildColumns(); } else { olv.View = olvState.CurrentView; } }
// overrides the OLV save state // uses JSON instead of a byte array // adds some parameters // also works with a different number of columns in the saved and current list public static void SaveStateAdvanced(ObjectListView olv, OLVStateAdv olvState) { olvState.VersionNumber = 1; olvState.NumberOfColumns = olv.AllColumns.Count; olvState.CurrentView = olv.View; // If we have a sort column, it is possible that it is not currently being shown, in which // case, it's Index will be -1. So we calculate its index directly. Technically, the sort // column does not even have to a member of AllColumns, in which case IndexOf will return -1, // which is works fine since we have no way of restoring such a column anyway. if (olv.PrimarySortColumn != null) { olvState.SortColumn = olv.AllColumns.IndexOf(olv.PrimarySortColumn); } olvState.LastSortOrder = olv.PrimarySortOrder; olvState.IsShowingGroups = olv.ShowGroups; // olvState.RowHeight = olv.RowHeight; olvState.CellEditActivation = olv.CellEditActivation; // if (olv.AllColumns.Count > 0 && olv.AllColumns[0].LastDisplayIndex == -1) { RememberDisplayIndicies(olv); } foreach (OLVColumn column in olv.AllColumns) { OLVColumnStateAdv columnState = new OLVColumnStateAdv(); columnState.Name = column.Name; columnState.IsVisible = column.IsVisible; columnState.LastDisplayIndex = column.LastDisplayIndex; columnState.Width = column.Width; olvState.Сolumns.Add(columnState); } }