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 // XXX [TypeConverter (typeof (DataGridViewColumnConverter))]
38 [DesignTimeVisible (false)]
39 public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
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; }
85 set { autoSizeMode = value; }
89 [EditorBrowsable (EditorBrowsableState.Advanced)]
90 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
91 public virtual DataGridViewCell CellTemplate {
92 get { return cellTemplate; }
95 if (DataGridView != null) {
96 cellTemplate.SetDataGridView(DataGridView);
102 [EditorBrowsable (EditorBrowsableState.Advanced)]
103 public Type CellType {
105 if (cellTemplate == null) {
108 return cellTemplate.GetType();
112 [DefaultValue (null)]
113 public override ContextMenuStrip ContextMenuStrip {
114 get { return contextMenuStrip; }
116 if (contextMenuStrip != value) {
117 contextMenuStrip = value;
118 if (DataGridView != null) {
119 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
127 [Editor ("System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, " + Consts.AssemblySystem_Design,
128 typeof (System.Drawing.Design.UITypeEditor))]
129 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
130 public string DataPropertyName {
131 get { return dataPropertyName; }
133 if (dataPropertyName != value) {
134 dataPropertyName = value;
135 if (DataGridView != null) {
136 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
143 public override DataGridViewCellStyle DefaultCellStyle {
145 return base.DefaultCellStyle;
148 if (DefaultCellStyle != value) {
149 base.DefaultCellStyle = value;
150 if (DataGridView != null) {
151 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
158 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
159 public int DisplayIndex {
161 if (displayIndex < 0) {
167 if (displayIndex != value) {
168 if (value < 0 || value > Int32.MaxValue) {
169 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
171 displayIndex = value;
172 if (DataGridView != null) {
173 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
180 public int DividerWidth {
181 get { return dividerWidth; }
183 if (dividerWidth != value) {
184 dividerWidth = value;
185 if (DataGridView != null) {
186 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
193 public float FillWeight {
194 get { return fillWeight; }
197 /* 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.
199 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
204 [DefaultValue (false)]
205 [RefreshProperties (RefreshProperties.All)]
206 public override bool Frozen {
207 get { return frozen; }
208 set { frozen = value; }
210 /* 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.
214 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
215 public DataGridViewColumnHeaderCell HeaderCell {
220 if (headerCell != value) {
222 if (DataGridView != null) {
223 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
230 public string HeaderText {
232 if (headerCell.Value == null) {
235 return (string) headerCell.Value;
238 headerCell.Value = value;
239 headerTextSet = true;
244 [EditorBrowsable (EditorBrowsableState.Advanced)]
245 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
246 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
248 if (this.DataGridView == null)
249 return this.autoSizeMode;
251 if (this.autoSizeMode != DataGridViewAutoSizeColumnMode.NotSet)
252 return this.autoSizeMode;
254 switch (this.DataGridView.AutoSizeColumnsMode) {
255 case DataGridViewAutoSizeColumnsMode.AllCells:
256 return DataGridViewAutoSizeColumnMode.AllCells;
257 case DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader:
258 return DataGridViewAutoSizeColumnMode.AllCellsExceptHeader;
259 case DataGridViewAutoSizeColumnsMode.ColumnHeader:
260 return DataGridViewAutoSizeColumnMode.ColumnHeader;
261 case DataGridViewAutoSizeColumnsMode.DisplayedCells:
262 return DataGridViewAutoSizeColumnMode.DisplayedCells;
263 case DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader:
264 return DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;
265 case DataGridViewAutoSizeColumnsMode.Fill:
266 return DataGridViewAutoSizeColumnMode.Fill;
268 return DataGridViewAutoSizeColumnMode.None;
274 public override DataGridViewCellStyle InheritedStyle {
276 if (DataGridView == null) {
277 return base.DefaultCellStyle;
280 if (base.DefaultCellStyle == null) {
281 return DataGridView.DefaultCellStyle;
284 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
285 /////// Combination with dataGridView.DefaultCellStyle
293 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
294 public bool IsDataBound {
295 get { return isDataBound; }
299 [RefreshProperties (RefreshProperties.Repaint)]
301 public int MinimumWidth {
302 get { return minimumWidth; }
304 if (minimumWidth != value) {
305 if (value < 2 || value > Int32.MaxValue) {
306 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
308 minimumWidth = value;
309 if (DataGridView != null) {
310 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
325 if (!headerTextSet) {
326 headerCell.Value = name;
328 if (DataGridView != null) {
329 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
335 public override bool ReadOnly {
338 if (DataGridView != null && DataGridView.ReadOnly)
343 set { readOnly = value; }
346 public override DataGridViewTriState Resizable {
347 get { return base.Resizable; }
348 set { base.Resizable = value; }
352 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
355 set { site = value; }
358 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
359 public DataGridViewColumnSortMode SortMode {
360 get { return sortMode; }
362 if (value == DataGridViewColumnSortMode.Automatic && DataGridView != null && DataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect)
363 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
365 if (sortMode != value) {
367 if (DataGridView != null) {
368 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
376 public string ToolTipText {
378 if (toolTipText == null)
380 return toolTipText; }
382 if (toolTipText != value) {
384 if (DataGridView != null) {
385 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
392 [DefaultValue (null)]
393 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
394 public Type ValueType {
395 get { return valueType; }
396 set { valueType = value; }
399 [DefaultValue (true)]
401 public override bool Visible {
402 get { return visible; }
403 set { visible = value; }
407 [RefreshProperties (RefreshProperties.Repaint)]
409 get { return width; }
411 if (width != value) {
412 if (value < minimumWidth) {
413 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
416 if (DataGridView != null) {
417 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
424 // XXX should we do something like Component.Events?
426 [EditorBrowsable (EditorBrowsableState.Advanced)]
427 public event EventHandler Disposed;
429 public override object Clone () {
430 return this.MemberwiseClone();
432 DataGridViewColumn result = new DataGridViewColumn();
437 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
438 switch (autoSizeColumnMode) {
439 case DataGridViewAutoSizeColumnMode.NotSet:
440 case DataGridViewAutoSizeColumnMode.None:
441 case DataGridViewAutoSizeColumnMode.Fill:
442 throw new ArgumentException("AutoSizeColumnMode is invalid");
452 public override string ToString () {
453 return Name + ", Index: " + base.Index.ToString() + ".";
456 protected override void Dispose (bool disposing) {
461 internal override void SetDataGridView (DataGridView dataGridView) {
462 if (sortMode == DataGridViewColumnSortMode.Automatic && dataGridView != null && dataGridView.SelectionMode == DataGridViewSelectionMode.FullColumnSelect) {
463 throw new InvalidOperationException ("Column's SortMode cannot be set to Automatic while the DataGridView control's SelectionMode is set to FullColumnSelect.");
466 base.SetDataGridView (dataGridView);
467 if (cellTemplate != null) {
468 cellTemplate.SetDataGridView(dataGridView);
470 headerCell.SetDataGridView(dataGridView);
473 internal override void SetIndex (int index) {
474 base.SetIndex(index);
475 headerCell.SetColumnIndex(Index);
478 internal override void SetState (DataGridViewElementStates state) {
479 if (State != state) {
480 base.SetState(state);
481 if (DataGridView != null) {
482 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));