/// <summary> /// default ctor /// </summary> /// <param name="columnChanged">The column that has changed</param> /// <param name="action">The action for the event</param> public ColumnStateChangedEventArgs(DataGridViewColumn columnChanged, ColumnStateChangeAction action) { this.action = action; this.columnChanged = columnChanged; }
/// <summary> /// returns a datagrid view column with all the settings applied /// </summary> /// <returns>datagrid view column</returns> /// <exception cref="NotSupportedException">Thrown when the exclude flag has been marked as true</exception> internal DataGridViewColumn GetDataGridViewColumn() { if (exclude) throw new NotSupportedException("Cannot call GetDataGridViewColumn if Exclude has been marked as true"); DataGridViewColumn column = new DataGridViewColumn(DisplayName, SortName); //Collection behaviour IValueConverter converter = null; //TODO IMPLEMENT CUSTOM STRATEGY if (collectionBehaviour != CollectionBehaviour.None) { converter = CollectionNotificationManager.RegisterCollectionNotification(collectionBehaviour); memeberBinding.Converter = converter; column.CanSort = false; } column.DisplayMemberBinding = memeberBinding; column.DefaultPosition = position; return column; }
//generates the column for the view private void GenerateGridColumns(Type type) { //unregister to the old columns notifications if (!UseDefaultView && View != null) { GridView oldView = View as GridView; if (oldView != null) foreach (INotifyPropertyChanged column in oldView.Columns) column.PropertyChanged -= gridViewColumnPropertyChanged; } GridView gridView = new GridView();//grid view to display the columns IList<DataGridViewColumn> columnsToAdd = PropertyInfoEngine.GetProperties<DataGridViewColumn>( type, delegate(PropertyInfo property) { DataGridViewColumn column = null; //get the attribute DataGridViewPropertyDescriptorAttribute[] attributes = (DataGridViewPropertyDescriptorAttribute[]) property.GetCustomAttributes(typeof(DataGridViewPropertyDescriptorAttribute), true); if (attributes != null && attributes.Length > 0) { if (attributes[0].Exclude) return null; //return null so that we exclude this column column = attributes[0].GetDataGridViewColumn(); } else { column = new DataGridViewColumn(property.Name, property.Name); column.DisplayMemberBinding = new Binding(property.Name); } //register to the notifications ((INotifyPropertyChanged)column).PropertyChanged += gridViewColumnPropertyChanged; return column; }); //add all the columns to the new grid view, and remove the excluded for (int i = 0; i < columnsToAdd.Count; i++) { DataGridViewColumn column = columnsToAdd[i]; if (column != null) { gridView.Columns.Add(column); } else { columnsToAdd.RemoveAt(i); i--; } } //position the columns properly for (int i = 0; i < columnsToAdd.Count; i++) { DataGridViewColumn columnToMove = columnsToAdd[i]; if(DataGridViewColumn.IsValidPosition(columnToMove.DefaultPosition)) gridView.Columns.Move(i, (columnToMove.DefaultPosition >= columnsToAdd.Count ? (columnsToAdd.Count-1) : columnToMove.DefaultPosition)); } View = gridView; }
/// <summary> /// Returns a ColumnStateChangeAction that describes the property changed event /// </summary> /// <param name="sender">The DataGridViewColumn that has raised the event</param> /// <param name="e">The event argmunets to parse</param> /// <returns>Returns a ColumnStateChangeAction to represent the change of state</returns> /// <exception cref="ArgumentNullException">Throws ArgumentNullException if the sender or the arguments are not valid</exception> public static ColumnStateChangeAction GetActionFromPropertyChanged(DataGridViewColumn sender, PropertyChangedEventArgs e) { if (sender != null && IsEnabledPropertyChanged(e)) return sender.IsEnabled ? ColumnStateChangeAction.Enabled : ColumnStateChangeAction.Disabled; throw new ArgumentNullException("e", ExceptionStrings.INVALID_PROPETY_CHANGED_EVENT_ARGS); }