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;
60 private int dataColumnIndex;
62 private bool headerTextSet = false;
64 public DataGridViewColumn () {
66 base.DefaultCellStyle = new DataGridViewCellStyle();
68 headerCell = new DataGridViewColumnHeaderCell();
69 headerCell.SetColumnIndex(Index);
70 headerCell.Value = string.Empty;
73 dataPropertyName = string.Empty;
75 sortMode = DataGridViewColumnSortMode.NotSortable;
76 SetState (DataGridViewElementStates.Visible);
79 public DataGridViewColumn (DataGridViewCell cellTemplate) : this () {
80 this.cellTemplate = (DataGridViewCell) cellTemplate.Clone();
83 [DefaultValue (DataGridViewAutoSizeColumnMode.NotSet)]
84 [RefreshProperties (RefreshProperties.Repaint)]
85 public DataGridViewAutoSizeColumnMode AutoSizeMode {
86 get { return autoSizeMode; }
88 if (autoSizeMode != value) {
89 DataGridViewAutoSizeColumnMode old_value = autoSizeMode;
92 if (DataGridView != null) {
93 DataGridView.OnAutoSizeColumnModeChanged (new DataGridViewAutoSizeColumnModeEventArgs (this, old_value));
94 DataGridView.AutoResizeColumnsInternal ();
101 [EditorBrowsable (EditorBrowsableState.Advanced)]
102 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
103 public virtual DataGridViewCell CellTemplate {
104 get { return cellTemplate; }
105 set { cellTemplate = value; }
109 [EditorBrowsable (EditorBrowsableState.Advanced)]
110 public Type CellType {
112 if (cellTemplate == null) {
115 return cellTemplate.GetType();
119 [DefaultValue (null)]
120 public override ContextMenuStrip ContextMenuStrip {
121 get { return contextMenuStrip; }
123 if (contextMenuStrip != value) {
124 contextMenuStrip = value;
125 if (DataGridView != null) {
126 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
134 [Editor ("System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, " + Consts.AssemblySystem_Design,
135 typeof (System.Drawing.Design.UITypeEditor))]
136 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
137 public string DataPropertyName {
138 get { return dataPropertyName; }
140 if (dataPropertyName != value) {
141 dataPropertyName = value;
142 if (DataGridView != null) {
143 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
150 public override DataGridViewCellStyle DefaultCellStyle {
152 return base.DefaultCellStyle;
155 if (DefaultCellStyle != value) {
156 base.DefaultCellStyle = value;
157 if (DataGridView != null) {
158 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
165 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
166 public int DisplayIndex {
168 if (displayIndex < 0) {
174 if (displayIndex != value) {
175 if (value < 0 || value > Int32.MaxValue) {
176 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
178 displayIndex = value;
179 if (DataGridView != null) {
180 DataGridView.Columns.RegenerateSortedList ();
181 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
187 internal int DisplayIndexInternal {
188 get { return DisplayIndex; }
189 set { displayIndex = value; }
192 internal int DataColumnIndex {
193 get { return dataColumnIndex; }
194 set { dataColumnIndex = value; }
198 public int DividerWidth {
199 get { return dividerWidth; }
201 if (dividerWidth != value) {
202 dividerWidth = value;
203 if (DataGridView != null) {
204 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
211 public float FillWeight {
212 get { return fillWeight; }
215 /* 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.
217 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
222 [DefaultValue (false)]
223 [RefreshProperties (RefreshProperties.All)]
224 public override bool Frozen {
225 get { return frozen; }
226 set { frozen = value; }
228 /* 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.
232 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
233 public DataGridViewColumnHeaderCell HeaderCell {
238 if (headerCell != value) {
240 if (DataGridView != null) {
241 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
248 public string HeaderText {
250 if (headerCell.Value == null) {
253 return (string) headerCell.Value;
256 headerCell.Value = value;
257 headerTextSet = true;
261 internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
262 internal bool HeaderTextSet { get { return headerTextSet; } }
265 [EditorBrowsable (EditorBrowsableState.Advanced)]
266 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
267 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
269 if (this.DataGridView == null)
270 return this.autoSizeMode;
272 if (this.autoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
273 return this.autoSizeMode;
275 switch (this.DataGridView.AutoSizeColumnsMode) {
276 case DataGridViewAutoSizeColumnsMode.AllCells:
277 return DataGridViewAutoSizeColumnMode.AllCells;
278 case DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader:
279 return DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
280 case DataGridViewAutoSizeColumnsMode.ColumnHeader:
281 return DataGridViewAutoSizeColumnMode.ColumnHeader;
282 case DataGridViewAutoSizeColumnsMode.DisplayedCells:
283 return DataGridViewAutoSizeColumnMode.DisplayedCells;
284 case DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader:
285 return DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
286 case DataGridViewAutoSizeColumnsMode.Fill:
287 return DataGridViewAutoSizeColumnMode.Fill;
289 return DataGridViewAutoSizeColumnMode.None;
295 public override DataGridViewCellStyle InheritedStyle {
297 if (DataGridView == null) {
298 return base.DefaultCellStyle;
301 if (base.DefaultCellStyle == null) {
302 return DataGridView.DefaultCellStyle;
305 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
306 /////// Combination with dataGridView.DefaultCellStyle
314 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
315 public bool IsDataBound {
316 get { return isDataBound; }
320 [RefreshProperties (RefreshProperties.Repaint)]
322 public int MinimumWidth {
323 get { return minimumWidth; }
325 if (minimumWidth != value) {
326 if (value < 2 || value > Int32.MaxValue) {
327 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
329 minimumWidth = value;
330 if (DataGridView != null) {
331 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
346 if (!headerTextSet) {
347 headerCell.Value = name;
349 if (DataGridView != null) {
350 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
356 public override bool ReadOnly {
359 if (DataGridView != null && DataGridView.ReadOnly)
364 set { readOnly = value; }
367 public override DataGridViewTriState Resizable {
368 get { return base.Resizable; }
369 set { base.Resizable = value; }
373 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
376 set { site = value; }
379 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
380 public DataGridViewColumnSortMode SortMode {
381 get { return sortMode; }
383 if (DataGridView != null && value == DataGridViewColumnSortMode.Automatic) {
384 if (DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect ||
385 DataGridView.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect)
386 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic "+
387 "while the DataGridView control's SelectionMode "+
388 "is set to FullColumnSelect or ColumnHeaderSelect.");
391 if (sortMode != value) {
393 if (DataGridView != null) {
394 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
402 public string ToolTipText {
404 if (toolTipText == null)
406 return toolTipText; }
408 if (toolTipText != value) {
410 if (DataGridView != null) {
411 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
418 [DefaultValue (null)]
419 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
420 public Type ValueType {
421 get { return valueType; }
422 set { valueType = value; }
425 [DefaultValue (true)]
427 public override bool Visible {
428 get { return visible; }
431 if (DataGridView != null)
432 DataGridView.Invalidate ();
437 [RefreshProperties (RefreshProperties.Repaint)]
439 get { return width; }
441 if (width != value) {
442 if (value < minimumWidth) {
443 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
446 if (DataGridView != null) {
447 DataGridView.Invalidate ();
448 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
455 // XXX should we do something like Component.Events?
457 [EditorBrowsable (EditorBrowsableState.Advanced)]
458 public event EventHandler Disposed;
460 public override object Clone () {
461 return this.MemberwiseClone();
463 DataGridViewColumn result = new DataGridViewColumn();
468 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
469 switch (autoSizeColumnMode) {
470 case DataGridViewAutoSizeColumnMode.NotSet:
471 case DataGridViewAutoSizeColumnMode.None:
472 case DataGridViewAutoSizeColumnMode.Fill:
473 throw new ArgumentException("AutoSizeColumnMode is invalid");
483 public override string ToString () {
484 return Name + ", Index: " + base.Index.ToString() + ".";
487 protected override void Dispose (bool disposing) {
492 internal override void SetDataGridView (DataGridView dataGridView) {
493 if (sortMode == DataGridViewColumnSortMode.Automatic && dataGridView != null && dataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) {
494 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
497 base.SetDataGridView (dataGridView);
498 headerCell.SetDataGridView(dataGridView);
501 internal override void SetIndex (int index) {
502 base.SetIndex(index);
503 headerCell.SetColumnIndex(Index);
506 internal void SetIsDataBound (bool value)
511 internal override void SetState (DataGridViewElementStates state) {
512 if (State != state) {
513 base.SetState(state);
514 if (DataGridView != null) {
515 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));
522 internal class DataGridViewColumnConverter : TypeConverter