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>
28 using System.ComponentModel;
30 namespace System.Windows.Forms {
32 [Designer ("System.Windows.Forms.Design.DataGridViewColumnDesigner, " + Consts.AssemblySystem_Design,
33 "System.ComponentModel.Design.IDesigner")]
34 [TypeConverter (typeof (DataGridViewColumnConverter))]
36 [DesignTimeVisible (false)]
37 public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
38 private bool auto_generated;
39 private DataGridViewAutoSizeColumnMode autoSizeMode;
40 private DataGridViewCell cellTemplate;
41 private ContextMenuStrip contextMenuStrip;
42 private string dataPropertyName;
43 private int displayIndex;
44 private int dividerWidth;
45 private float fillWeight;
47 private DataGridViewColumnHeaderCell headerCell;
48 private bool isDataBound;
49 private int minimumWidth = 5;
50 private string name = "";
51 private bool readOnly;
53 private DataGridViewColumnSortMode sortMode;
54 private string toolTipText;
55 private Type valueType;
56 private bool visible = true;
57 private int width = 100;
58 private int dataColumnIndex;
60 private bool headerTextSet = false;
62 public DataGridViewColumn () {
64 base.DefaultCellStyle = new DataGridViewCellStyle();
66 headerCell = new DataGridViewColumnHeaderCell();
67 headerCell.SetColumnIndex(Index);
68 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; }
103 set { cellTemplate = value; }
107 [EditorBrowsable (EditorBrowsableState.Advanced)]
108 public Type CellType {
110 if (cellTemplate == null) {
113 return cellTemplate.GetType();
117 [DefaultValue (null)]
118 public override ContextMenuStrip ContextMenuStrip {
119 get { return contextMenuStrip; }
121 if (contextMenuStrip != value) {
122 contextMenuStrip = value;
123 if (DataGridView != null) {
124 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
132 [Editor ("System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, " + Consts.AssemblySystem_Design,
133 typeof (System.Drawing.Design.UITypeEditor))]
134 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
135 public string DataPropertyName {
136 get { return dataPropertyName; }
138 if (dataPropertyName != value) {
139 dataPropertyName = value;
140 if (DataGridView != null) {
141 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
148 public override DataGridViewCellStyle DefaultCellStyle {
150 return base.DefaultCellStyle;
153 if (DefaultCellStyle != value) {
154 base.DefaultCellStyle = value;
155 if (DataGridView != null) {
156 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
163 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
164 public int DisplayIndex {
166 if (displayIndex < 0) {
172 if (displayIndex != value) {
173 if (value < 0 || value > Int32.MaxValue) {
174 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
176 displayIndex = value;
177 if (DataGridView != null) {
178 DataGridView.Columns.RegenerateSortedList ();
179 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
185 internal int DisplayIndexInternal {
186 get { return DisplayIndex; }
187 set { displayIndex = value; }
190 internal int DataColumnIndex {
191 get { return dataColumnIndex; }
193 dataColumnIndex = value;
194 if (dataColumnIndex >= 0)
200 public int DividerWidth {
201 get { return dividerWidth; }
203 if (dividerWidth != value) {
204 dividerWidth = value;
205 if (DataGridView != null) {
206 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
213 public float FillWeight {
214 get { return fillWeight; }
217 /* 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.
219 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
224 [DefaultValue (false)]
225 [RefreshProperties (RefreshProperties.All)]
226 public override bool Frozen {
227 get { return frozen; }
228 set { frozen = value; }
230 /* 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.
234 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
235 public DataGridViewColumnHeaderCell HeaderCell {
240 if (headerCell != value) {
242 if (DataGridView != null) {
243 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
250 public string HeaderText {
252 if (headerCell.Value == null) {
255 return (string) headerCell.Value;
258 headerCell.Value = value;
259 headerTextSet = true;
263 internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
264 internal bool HeaderTextSet { get { return headerTextSet; } }
267 [EditorBrowsable (EditorBrowsableState.Advanced)]
268 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
269 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
271 if (this.DataGridView == null)
272 return this.autoSizeMode;
274 if (this.autoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
275 return this.autoSizeMode;
277 switch (this.DataGridView.AutoSizeColumnsMode) {
278 case DataGridViewAutoSizeColumnsMode.AllCells:
279 return DataGridViewAutoSizeColumnMode.AllCells;
280 case DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader:
281 return DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
282 case DataGridViewAutoSizeColumnsMode.ColumnHeader:
283 return DataGridViewAutoSizeColumnMode.ColumnHeader;
284 case DataGridViewAutoSizeColumnsMode.DisplayedCells:
285 return DataGridViewAutoSizeColumnMode.DisplayedCells;
286 case DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader:
287 return DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
288 case DataGridViewAutoSizeColumnsMode.Fill:
289 return DataGridViewAutoSizeColumnMode.Fill;
291 return DataGridViewAutoSizeColumnMode.None;
297 public override DataGridViewCellStyle InheritedStyle {
299 if (DataGridView == null) {
300 return base.DefaultCellStyle;
303 if (base.DefaultCellStyle == null) {
304 return DataGridView.DefaultCellStyle;
307 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
308 /////// Combination with dataGridView.DefaultCellStyle
316 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
317 public bool IsDataBound {
318 get { return isDataBound; }
322 [RefreshProperties (RefreshProperties.Repaint)]
324 public int MinimumWidth {
325 get { return minimumWidth; }
327 if (minimumWidth != value) {
328 if (value < 2 || value > Int32.MaxValue) {
329 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
331 minimumWidth = value;
332 if (DataGridView != null) {
333 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
348 if (!headerTextSet) {
349 headerCell.Value = name;
351 if (DataGridView != null) {
352 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
358 public override bool ReadOnly {
361 if (DataGridView != null && DataGridView.ReadOnly)
366 set { readOnly = value; }
369 public override DataGridViewTriState Resizable {
370 get { return base.Resizable; }
371 set { base.Resizable = value; }
375 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
378 set { site = value; }
381 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
382 public DataGridViewColumnSortMode SortMode {
383 get { return sortMode; }
385 if (DataGridView != null && value == DataGridViewColumnSortMode.Automatic) {
386 if (DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect ||
387 DataGridView.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect)
388 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic "+
389 "while the DataGridView control's SelectionMode "+
390 "is set to FullColumnSelect or ColumnHeaderSelect.");
393 if (sortMode != value) {
395 if (DataGridView != null) {
396 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
404 public string ToolTipText {
406 if (toolTipText == null)
408 return toolTipText; }
410 if (toolTipText != value) {
412 if (DataGridView != null) {
413 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
420 [DefaultValue (null)]
421 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
422 public Type ValueType {
423 get { return valueType; }
424 set { valueType = value; }
427 [DefaultValue (true)]
429 public override bool Visible {
430 get { return visible; }
433 if (DataGridView != null)
434 DataGridView.Invalidate ();
439 [RefreshProperties (RefreshProperties.Repaint)]
441 get { return width; }
443 if (width != value) {
444 if (value < minimumWidth) {
445 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
448 if (DataGridView != null) {
449 DataGridView.Invalidate ();
450 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
457 // XXX should we do something like Component.Events?
459 [EditorBrowsable (EditorBrowsableState.Advanced)]
460 public event EventHandler Disposed;
462 public override object Clone () {
463 return this.MemberwiseClone();
465 DataGridViewColumn result = new DataGridViewColumn();
470 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
471 switch (autoSizeColumnMode) {
472 case DataGridViewAutoSizeColumnMode.NotSet:
473 case DataGridViewAutoSizeColumnMode.None:
474 case DataGridViewAutoSizeColumnMode.Fill:
475 throw new ArgumentException("AutoSizeColumnMode is invalid");
485 public override string ToString () {
486 return Name + ", Index: " + base.Index.ToString() + ".";
489 protected override void Dispose (bool disposing) {
494 internal override void SetDataGridView (DataGridView dataGridView) {
495 if (sortMode == DataGridViewColumnSortMode.Automatic && dataGridView != null && dataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) {
496 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
499 base.SetDataGridView (dataGridView);
500 headerCell.SetDataGridView(dataGridView);
503 internal override void SetIndex (int index) {
504 base.SetIndex(index);
505 headerCell.SetColumnIndex(Index);
508 internal override void SetState (DataGridViewElementStates state) {
509 if (State != state) {
510 base.SetState(state);
511 if (DataGridView != null) {
512 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));
519 internal class DataGridViewColumnConverter : TypeConverter