BrightIdeasSoftware.ObjectListView.ChangeToFilteredColumns C# (CSharp) Method

ChangeToFilteredColumns() public method

Rebuild this list for the given view
public ChangeToFilteredColumns ( View view ) : void
view View
return void
        public virtual void ChangeToFilteredColumns(View view)
        {
            // Store the state
            IList previousSelection = this.SelectedObjects;
            int previousTopIndex = this.TopItemIndex;

            this.Freeze();
            this.Clear();
            List<OLVColumn> columns = this.GetFilteredColumns(view);
            if (view == View.Details || this.ShowHeaderInAllViews) {
                // Make sure all columns have a reasonable LastDisplayIndex
                for (int index = 0; index < columns.Count; index++)
                {
                    if (columns[index].LastDisplayIndex == -1)
                        columns[index].LastDisplayIndex = index;
                }
                // ListView will ignore DisplayIndex FOR ALL COLUMNS if there are any errors,
                // e.g. duplicates (two columns with the same DisplayIndex) or gaps.
                // LastDisplayIndex isn't guaranteed to be unique, so we just sort the columns by
                // the last position they were displayed and use that to generate a sequence
                // we can use for the DisplayIndex values.
                List<OLVColumn> columnsInDisplayOrder = new List<OLVColumn>(columns);
                columnsInDisplayOrder.Sort(delegate(OLVColumn x, OLVColumn y) { return (x.LastDisplayIndex - y.LastDisplayIndex); });
                int i = 0;
                foreach (OLVColumn col in columnsInDisplayOrder)
                    col.DisplayIndex = i++;
            }

            this.Columns.AddRange(columns.ToArray());
            if (view == View.Details || this.ShowHeaderInAllViews)
                this.ShowSortIndicator();
            this.BuildList();
            this.Unfreeze();

            // Restore the state
            this.SelectedObjects = previousSelection;
            this.TopItemIndex = previousTopIndex;
        }

Usage Example

        /// <summary>
        /// The user has pressed OK. Do what's requied.
        /// </summary>
        /// <param name="olv"></param>
        /// <param name="view"></param>
        protected void Apply(ObjectListView olv, View view)
        {
            olv.Freeze();

            // Update the column definitions to reflect whether they have been hidden
            if (view == View.Details)
            {
                foreach (OLVColumn col in olv.AllColumns)
                {
                    col.IsVisible = this.MapColumnToVisible[col];
                }
            }
            else
            {
                foreach (OLVColumn col in olv.AllColumns)
                {
                    col.IsTileViewColumn = this.MapColumnToVisible[col];
                }
            }

            // Collect the columns are still visible
            List <OLVColumn> visibleColumns = this.RearrangableColumns.FindAll(
                delegate(OLVColumn x) { return(this.MapColumnToVisible[x]); });

            // Detail view and Tile view have to be handled in different ways.
            if (view == View.Details)
            {
                // Of the still visible columns, change DisplayIndex to reflect their position in the rearranged list
                olv.ChangeToFilteredColumns(view);
                foreach (OLVColumn col in visibleColumns)
                {
                    col.DisplayIndex     = visibleColumns.IndexOf((OLVColumn)col);
                    col.LastDisplayIndex = col.DisplayIndex;
                }
            }
            else
            {
                // In Tile view, DisplayOrder does nothing. So to change the display order, we have to change the
                // order of the columns in the Columns property.
                // Remember, the primary column is special and has to remain first!
                OLVColumn primaryColumn = this.AllColumns[0];
                visibleColumns.Remove(primaryColumn);

                olv.Columns.Clear();
                olv.Columns.Add(primaryColumn);
                olv.Columns.AddRange(visibleColumns.ToArray());
                olv.CalculateReasonableTileSize();
            }

            olv.Unfreeze();
        }
ObjectListView