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; }
195 dataColumnIndex = value;
196 if (dataColumnIndex >= 0)
202 public int DividerWidth {
203 get { return dividerWidth; }
205 if (dividerWidth != value) {
206 dividerWidth = value;
207 if (DataGridView != null) {
208 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
215 public float FillWeight {
216 get { return fillWeight; }
219 /* 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.
221 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
226 [DefaultValue (false)]
227 [RefreshProperties (RefreshProperties.All)]
228 public override bool Frozen {
229 get { return frozen; }
230 set { frozen = value; }
232 /* 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.
236 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
237 public DataGridViewColumnHeaderCell HeaderCell {
242 if (headerCell != value) {
244 if (DataGridView != null) {
245 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
252 public string HeaderText {
254 if (headerCell.Value == null) {
257 return (string) headerCell.Value;
260 headerCell.Value = value;
261 headerTextSet = true;
265 internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
266 internal bool HeaderTextSet { get { return headerTextSet; } }
269 [EditorBrowsable (EditorBrowsableState.Advanced)]
270 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
271 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
273 if (this.DataGridView == null)
274 return this.autoSizeMode;
276 if (this.autoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
277 return this.autoSizeMode;
279 switch (this.DataGridView.AutoSizeColumnsMode) {
280 case DataGridViewAutoSizeColumnsMode.AllCells:
281 return DataGridViewAutoSizeColumnMode.AllCells;
282 case DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader:
283 return DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
284 case DataGridViewAutoSizeColumnsMode.ColumnHeader:
285 return DataGridViewAutoSizeColumnMode.ColumnHeader;
286 case DataGridViewAutoSizeColumnsMode.DisplayedCells:
287 return DataGridViewAutoSizeColumnMode.DisplayedCells;
288 case DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader:
289 return DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
290 case DataGridViewAutoSizeColumnsMode.Fill:
291 return DataGridViewAutoSizeColumnMode.Fill;
293 return DataGridViewAutoSizeColumnMode.None;
299 public override DataGridViewCellStyle InheritedStyle {
301 if (DataGridView == null) {
302 return base.DefaultCellStyle;
305 if (base.DefaultCellStyle == null) {
306 return DataGridView.DefaultCellStyle;
309 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
310 /////// Combination with dataGridView.DefaultCellStyle
318 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
319 public bool IsDataBound {
320 get { return isDataBound; }
324 [RefreshProperties (RefreshProperties.Repaint)]
326 public int MinimumWidth {
327 get { return minimumWidth; }
329 if (minimumWidth != value) {
330 if (value < 2 || value > Int32.MaxValue) {
331 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
333 minimumWidth = value;
334 if (DataGridView != null) {
335 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
350 if (!headerTextSet) {
351 headerCell.Value = name;
353 if (DataGridView != null) {
354 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
360 public override bool ReadOnly {
363 if (DataGridView != null && DataGridView.ReadOnly)
368 set { readOnly = value; }
371 public override DataGridViewTriState Resizable {
372 get { return base.Resizable; }
373 set { base.Resizable = value; }
377 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
380 set { site = value; }
383 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
384 public DataGridViewColumnSortMode SortMode {
385 get { return sortMode; }
387 if (DataGridView != null && value == DataGridViewColumnSortMode.Automatic) {
388 if (DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect ||
389 DataGridView.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect)
390 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic "+
391 "while the DataGridView control's SelectionMode "+
392 "is set to FullColumnSelect or ColumnHeaderSelect.");
395 if (sortMode != value) {
397 if (DataGridView != null) {
398 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
406 public string ToolTipText {
408 if (toolTipText == null)
410 return toolTipText; }
412 if (toolTipText != value) {
414 if (DataGridView != null) {
415 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
422 [DefaultValue (null)]
423 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
424 public Type ValueType {
425 get { return valueType; }
426 set { valueType = value; }
429 [DefaultValue (true)]
431 public override bool Visible {
432 get { return visible; }
435 if (DataGridView != null)
436 DataGridView.Invalidate ();
441 [RefreshProperties (RefreshProperties.Repaint)]
443 get { return width; }
445 if (width != value) {
446 if (value < minimumWidth) {
447 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
450 if (DataGridView != null) {
451 DataGridView.Invalidate ();
452 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
459 // XXX should we do something like Component.Events?
461 [EditorBrowsable (EditorBrowsableState.Advanced)]
462 public event EventHandler Disposed;
464 public override object Clone () {
465 return this.MemberwiseClone();
467 DataGridViewColumn result = new DataGridViewColumn();
472 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
473 switch (autoSizeColumnMode) {
474 case DataGridViewAutoSizeColumnMode.NotSet:
475 case DataGridViewAutoSizeColumnMode.None:
476 case DataGridViewAutoSizeColumnMode.Fill:
477 throw new ArgumentException("AutoSizeColumnMode is invalid");
487 public override string ToString () {
488 return Name + ", Index: " + base.Index.ToString() + ".";
491 protected override void Dispose (bool disposing) {
496 internal override void SetDataGridView (DataGridView dataGridView) {
497 if (sortMode == DataGridViewColumnSortMode.Automatic && dataGridView != null && dataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) {
498 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
501 base.SetDataGridView (dataGridView);
502 headerCell.SetDataGridView(dataGridView);
505 internal override void SetIndex (int index) {
506 base.SetIndex(index);
507 headerCell.SetColumnIndex(Index);
510 internal override void SetState (DataGridViewElementStates state) {
511 if (State != state) {
512 base.SetState(state);
513 if (DataGridView != null) {
514 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));
521 internal class DataGridViewColumnConverter : TypeConverter