1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
23 // Pedro MartÃnez Juliá <pedromj@gmail.com>
30 using System.ComponentModel;
32 namespace System.Windows.Forms {
34 [Designer ("System.Windows.Forms.Design.DataGridViewColumnDesigner, " + Consts.AssemblySystem_Design,
35 "System.ComponentModel.Design.IDesigner")]
36 [TypeConverter (typeof (DataGridViewColumnConverter))]
38 [DesignTimeVisible (false)]
39 public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
40 private bool auto_generated;
41 private DataGridViewAutoSizeColumnMode autoSizeMode;
42 private DataGridViewCell cellTemplate;
43 private ContextMenuStrip contextMenuStrip;
44 private string dataPropertyName;
45 private int displayIndex;
46 private int dividerWidth;
47 private float fillWeight;
49 private DataGridViewColumnHeaderCell headerCell;
50 private bool isDataBound;
51 private int minimumWidth = 5;
52 private string name = "";
53 private bool readOnly;
55 private DataGridViewColumnSortMode sortMode;
56 private string toolTipText;
57 private Type valueType;
58 private bool visible = true;
59 private int width = 100;
61 private bool headerTextSet = false;
63 public DataGridViewColumn () {
65 base.DefaultCellStyle = new DataGridViewCellStyle();
67 headerCell = new DataGridViewColumnHeaderCell();
68 headerCell.SetColumnIndex(Index);
69 headerCell.Value = string.Empty;
71 dataPropertyName = string.Empty;
73 sortMode = DataGridViewColumnSortMode.NotSortable;
74 SetState (DataGridViewElementStates.Visible);
77 public DataGridViewColumn (DataGridViewCell cellTemplate) : this () {
78 this.cellTemplate = (DataGridViewCell) cellTemplate.Clone();
81 [DefaultValue (DataGridViewAutoSizeColumnMode.NotSet)]
82 [RefreshProperties (RefreshProperties.Repaint)]
83 public DataGridViewAutoSizeColumnMode AutoSizeMode {
84 get { return autoSizeMode; }
86 if (autoSizeMode != value) {
87 DataGridViewAutoSizeColumnMode old_value = autoSizeMode;
90 if (DataGridView != null) {
91 DataGridView.OnAutoSizeColumnModeChanged (new DataGridViewAutoSizeColumnModeEventArgs (this, old_value));
92 DataGridView.AutoResizeColumnsInternal ();
99 [EditorBrowsable (EditorBrowsableState.Advanced)]
100 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
101 public virtual DataGridViewCell CellTemplate {
102 get { return cellTemplate; }
104 cellTemplate = value;
105 if (DataGridView != null) {
106 cellTemplate.SetDataGridView(DataGridView);
112 [EditorBrowsable (EditorBrowsableState.Advanced)]
113 public Type CellType {
115 if (cellTemplate == null) {
118 return cellTemplate.GetType();
122 [DefaultValue (null)]
123 public override ContextMenuStrip ContextMenuStrip {
124 get { return contextMenuStrip; }
126 if (contextMenuStrip != value) {
127 contextMenuStrip = value;
128 if (DataGridView != null) {
129 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
137 [Editor ("System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, " + Consts.AssemblySystem_Design,
138 typeof (System.Drawing.Design.UITypeEditor))]
139 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
140 public string DataPropertyName {
141 get { return dataPropertyName; }
143 if (dataPropertyName != value) {
144 dataPropertyName = value;
145 if (DataGridView != null) {
146 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
153 public override DataGridViewCellStyle DefaultCellStyle {
155 return base.DefaultCellStyle;
158 if (DefaultCellStyle != value) {
159 base.DefaultCellStyle = value;
160 if (DataGridView != null) {
161 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
168 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
169 public int DisplayIndex {
171 if (displayIndex < 0) {
177 if (displayIndex != value) {
178 if (value < 0 || value > Int32.MaxValue) {
179 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
181 displayIndex = value;
182 if (DataGridView != null) {
183 DataGridView.Columns.RegenerateSortedList ();
184 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
191 public int DividerWidth {
192 get { return dividerWidth; }
194 if (dividerWidth != value) {
195 dividerWidth = value;
196 if (DataGridView != null) {
197 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
204 public float FillWeight {
205 get { return fillWeight; }
208 /* When the System.Windows.Forms.DataGridViewColumn.InheritedAutoSizeMode property value is System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill, the column is resized along with other columns in that mode so that all visible columns in the control exactly fill the horizontal width of the available display area. All fill-mode columns in the control divide the available space in proportions determined by their System.Windows.Forms.DataGridViewColumn.FillWeight property values. For more information about column fill mode, see Column Fill Mode in the Windows Forms DataGridView Control.
210 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
215 [DefaultValue (false)]
216 [RefreshProperties (RefreshProperties.All)]
217 public override bool Frozen {
218 get { return frozen; }
219 set { frozen = value; }
221 /* When a column is frozen, all the columns to its left (or to its right in right-to-left languages) are frozen as well. The frozen and unfrozen columns form two groups. If column repositioning is enabled by setting the System.Windows.Forms.DataGridView.AllowUserToOrderColumns property to true, the user cannot drag a column from one group to the other.
225 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
226 public DataGridViewColumnHeaderCell HeaderCell {
231 if (headerCell != value) {
233 if (DataGridView != null) {
234 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
241 public string HeaderText {
243 if (headerCell.Value == null) {
246 return (string) headerCell.Value;
249 headerCell.Value = value;
250 headerTextSet = true;
254 internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
255 internal bool HeaderTextSet { get { return headerTextSet; } }
258 [EditorBrowsable (EditorBrowsableState.Advanced)]
259 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
260 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
262 if (this.DataGridView == null)
263 return this.autoSizeMode;
265 if (this.autoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
266 return this.autoSizeMode;
268 switch (this.DataGridView.AutoSizeColumnsMode) {
269 case DataGridViewAutoSizeColumnsMode.AllCells:
270 return DataGridViewAutoSizeColumnMode.AllCells;
271 case DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader:
272 return DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
273 case DataGridViewAutoSizeColumnsMode.ColumnHeader:
274 return DataGridViewAutoSizeColumnMode.ColumnHeader;
275 case DataGridViewAutoSizeColumnsMode.DisplayedCells:
276 return DataGridViewAutoSizeColumnMode.DisplayedCells;
277 case DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader:
278 return DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
279 case DataGridViewAutoSizeColumnsMode.Fill:
280 return DataGridViewAutoSizeColumnMode.Fill;
282 return DataGridViewAutoSizeColumnMode.None;
288 public override DataGridViewCellStyle InheritedStyle {
290 if (DataGridView == null) {
291 return base.DefaultCellStyle;
294 if (base.DefaultCellStyle == null) {
295 return DataGridView.DefaultCellStyle;
298 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
299 /////// Combination with dataGridView.DefaultCellStyle
307 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
308 public bool IsDataBound {
309 get { return isDataBound; }
313 [RefreshProperties (RefreshProperties.Repaint)]
315 public int MinimumWidth {
316 get { return minimumWidth; }
318 if (minimumWidth != value) {
319 if (value < 2 || value > Int32.MaxValue) {
320 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
322 minimumWidth = value;
323 if (DataGridView != null) {
324 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
339 if (!headerTextSet) {
340 headerCell.Value = name;
342 if (DataGridView != null) {
343 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
349 public override bool ReadOnly {
352 if (DataGridView != null && DataGridView.ReadOnly)
357 set { readOnly = value; }
360 public override DataGridViewTriState Resizable {
361 get { return base.Resizable; }
362 set { base.Resizable = value; }
366 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
369 set { site = value; }
372 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
373 public DataGridViewColumnSortMode SortMode {
374 get { return sortMode; }
376 if (value == DataGridViewColumnSortMode.Automatic && DataGridView != null && DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect)
377 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
379 if (sortMode != value) {
381 if (DataGridView != null) {
382 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
390 public string ToolTipText {
392 if (toolTipText == null)
394 return toolTipText; }
396 if (toolTipText != value) {
398 if (DataGridView != null) {
399 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
406 [DefaultValue (null)]
407 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
408 public Type ValueType {
409 get { return valueType; }
410 set { valueType = value; }
413 [DefaultValue (true)]
415 public override bool Visible {
416 get { return visible; }
417 set { visible = value; }
421 [RefreshProperties (RefreshProperties.Repaint)]
423 get { return width; }
425 if (width != value) {
426 if (value < minimumWidth) {
427 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
430 if (DataGridView != null) {
431 DataGridView.Invalidate ();
432 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
439 // XXX should we do something like Component.Events?
441 [EditorBrowsable (EditorBrowsableState.Advanced)]
442 public event EventHandler Disposed;
444 public override object Clone () {
445 return this.MemberwiseClone();
447 DataGridViewColumn result = new DataGridViewColumn();
452 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
453 switch (autoSizeColumnMode) {
454 case DataGridViewAutoSizeColumnMode.NotSet:
455 case DataGridViewAutoSizeColumnMode.None:
456 case DataGridViewAutoSizeColumnMode.Fill:
457 throw new ArgumentException("AutoSizeColumnMode is invalid");
467 public override string ToString () {
468 return Name + ", Index: " + base.Index.ToString() + ".";
471 protected override void Dispose (bool disposing) {
476 internal override void SetDataGridView (DataGridView dataGridView) {
477 if (sortMode == DataGridViewColumnSortMode.Automatic && dataGridView != null && dataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) {
478 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
481 base.SetDataGridView (dataGridView);
482 if (cellTemplate != null) {
483 cellTemplate.SetDataGridView(dataGridView);
485 headerCell.SetDataGridView(dataGridView);
488 internal override void SetIndex (int index) {
489 base.SetIndex(index);
490 headerCell.SetColumnIndex(Index);
493 internal void SetIsDataBound (bool value)
498 internal override void SetState (DataGridViewElementStates state) {
499 if (State != state) {
500 base.SetState(state);
501 if (DataGridView != null) {
502 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));
509 internal class DataGridViewColumnConverter : TypeConverter