Hide certain columns in Grid

Sep 2, 2008 at 11:36 AM
Hi all,

What is the C# code to hide certain columns and to enable those hidden columns again?

Thanks in advance
Sep 2, 2008 at 1:46 PM
This is a feature that will be part of the final release of the WPF DataGrid.  It is not currently in the CTP.
Sep 2, 2008 at 3:37 PM
Thanks for the Info!
Sep 4, 2008 at 6:55 PM
What I did was subclass DataGrid, and add a List of Columns. Remove the columns you want to add from the Grid's columns, and add them to the list. Then to reshow the columns, remove them from the list and reinsert them into .Columns collections given the display index. Clear your "removed columns" collection when done.

It works for now.
Sep 10, 2008 at 3:48 PM
Hi,$0$0$0$0 I've solved this problem by updating the DataGrid class.$0$0$0$0$0$0$0$0 Here is the code in DataGrid.cs$0$0$0#region Visibility$0$0$0$0$0        /// <summary>$0$0        /// Method for hidding or showing columns$0$0        /// </summary>$0$0        /// <param name="column"></param>$0$0        internal void PerformHideOrShow(DataGridColumn column)$0$0        {$0$0            Debug.Assert(column != null, "Column should not be null");$0$0$0$0$0            // fire visibility changed event$0$0            DataGridColumnVisibilityChangedEventArgs eventArgs = new DataGridColumnVisibilityChangedEventArgs(column);$0$0            OnColumnVisibilityChanged(this, eventArgs);$0$0$0$0$0            if (Items.NeedsRefresh)$0$0            {$0$0                try$0$0                {$0$0                    Items.Refresh();$0$0                }$0$0                catch (InvalidOperationException invalidOperationException)$0$0                {$0$0                    throw new InvalidOperationException(SR.Get(SRID.DataGrid_ProbableInvalidGroupDescription), invalidOperationException);$0$0                }$0$0            }$0$0        }$0$0$0$0$0        public event DataGridColumnVisibilityChangedHandler ColumnVisibilityChanged;$0$0$0$0$0        public Collection<DataGridColumn> HiddenColumns$0$0        {$0$0            get { return _hiddenColumns; }$0$0            set { _hiddenColumns = value; }$0$0        }$0$0$0$0$0        /// <summary>$0$0        /// Protected method that raises the ColumnVisibilityChanged event$0$0        /// </summary>$0$0        /// <param name="dataGrid"></param>$0$0        /// <param name="eventArgs"></param>$0$0        protected void OnColumnVisibilityChanged(DataGrid dataGrid, DataGridColumnVisibilityChangedEventArgs eventArgs)$0$0        {$0$0            eventArgs.Handled = false;$0$0            if (ColumnVisibilityChanged != null)$0$0            {$0$0                ColumnVisibilityChanged(this, eventArgs);$0$0            }$0$0$0$0$0            if (!eventArgs.Handled)$0$0            {$0$0                DefaultColumnVisibilityChanged(eventArgs.Column);$0$0            }$0$0        }$0$0$0$0$0        /// <summary>$0$0        /// Perfom default visibility or hidden$0$0        /// </summary>$0$0        /// <param name="dataGridColumn"></param>$0$0        private void DefaultColumnVisibilityChanged(DataGridColumn dataGridColumn)$0$0        {$0$0            if (dataGridColumn.Visible == false)$0$0            {$0$0                // remove the column and add it to removed columns$0$0                HiddenColumns.Add(dataGridColumn);$0$0                this.Columns.Remove(dataGridColumn);$0$0                dataGridColumn.DataGridOwner = this;$0$0            }$0$0            else$0$0            {$0$0                HiddenColumns.Remove(dataGridColumn);$0$0                this.Columns.Add(dataGridColumn);$0$0            }$0$0        }$0$0$0$0$0        #endregion$0$0$0$0$0$0Add there is some more code in DataGridColumn, defining a dependecy property VisibleProperty$0$0$0$0$0Regards,$0$0Calin$0$0 $0