BrightIdeasSoftware.ObjectListView.SaveState C# (CSharp) Method

SaveState() public method

Return a byte array that represents the current state of the ObjectListView, such that the state can be restored by RestoreState()

The state of an ObjectListView includes the attributes that the user can modify: current view (i.e. Details, Tile, Large Icon...) sort column and direction column order column widths column visibility

It does not include selection or the scroll position.

public SaveState ( ) : byte[]
return byte[]
        public virtual byte[] SaveState()
        {
            ObjectListViewState olvState = new ObjectListViewState();
            olvState.VersionNumber = 1;
            olvState.NumberOfColumns = this.AllColumns.Count;
            olvState.CurrentView = this.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 (this.LastSortColumn != null)
                olvState.SortColumn = this.AllColumns.IndexOf(this.LastSortColumn);
            olvState.LastSortOrder = this.LastSortOrder;
            olvState.IsShowingGroups = this.ShowGroups;

            if (this.AllColumns.Count > 0 && this.AllColumns[0].LastDisplayIndex == -1)
                this.RememberDisplayIndicies();

            foreach (OLVColumn column in this.AllColumns) {
                olvState.ColumnIsVisible.Add(column.IsVisible);
                olvState.ColumnDisplayIndicies.Add(column.LastDisplayIndex);
                olvState.ColumnWidths.Add(column.Width);
            }

            // Now that we have stored our state, convert it to a byte array
            using (MemoryStream ms = new MemoryStream()) {
                BinaryFormatter serializer = new BinaryFormatter();
                serializer.AssemblyFormat = FormatterAssemblyStyle.Simple;
                serializer.Serialize(ms, olvState);
                return ms.ToArray();
            }
        }
ObjectListView