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 string headerText = "";
51 private DataGridViewAutoSizeColumnMode inheritedAutoSizeMode;
52 private bool isDataBound;
53 private int minimumWidth = 5;
54 private string name = "";
55 private bool readOnly;
56 private DataGridViewTriState resizable = DataGridViewTriState.True;
58 private DataGridViewColumnSortMode sortMode;
59 private string toolTipText;
60 private Type valueType;
61 private bool visible = true;
62 private int width = 100;
64 public DataGridViewColumn () {
66 base.DefaultCellStyle = new DataGridViewCellStyle();
68 headerCell = new DataGridViewColumnHeaderCell();
69 headerCell.SetColumnIndex(Index);
73 public DataGridViewColumn (DataGridViewCell cellTemplate) : this () {
74 this.cellTemplate = (DataGridViewCell) cellTemplate.Clone();
77 [DefaultValue (DataGridViewAutoSizeColumnMode.NotSet)]
78 [RefreshProperties (RefreshProperties.Repaint)]
79 public DataGridViewAutoSizeColumnMode AutoSizeMode {
80 get { return autoSizeMode; }
81 set { autoSizeMode = value; }
85 [EditorBrowsable (EditorBrowsableState.Advanced)]
86 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
87 public virtual DataGridViewCell CellTemplate {
88 get { return cellTemplate; }
91 if (DataGridView != null) {
92 cellTemplate.SetDataGridView(DataGridView);
98 [EditorBrowsable (EditorBrowsableState.Advanced)]
99 public Type CellType {
101 if (cellTemplate == null) {
104 return cellTemplate.GetType();
108 [DefaultValue (null)]
109 public override ContextMenuStrip ContextMenuStrip {
110 get { return contextMenuStrip; }
112 if (contextMenuStrip != value) {
113 contextMenuStrip = value;
114 if (DataGridView != null) {
115 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
123 [Editor ("System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, " + Consts.AssemblySystem_Design,
124 typeof (System.Drawing.Design.UITypeEditor))]
125 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
126 public string DataPropertyName {
127 get { return dataPropertyName; }
129 if (dataPropertyName != value) {
130 dataPropertyName = value;
131 if (DataGridView != null) {
132 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
139 public override DataGridViewCellStyle DefaultCellStyle {
141 return base.DefaultCellStyle;
144 if (DefaultCellStyle != value) {
145 base.DefaultCellStyle = value;
146 if (DataGridView != null) {
147 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
154 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
155 public int DisplayIndex {
157 if (displayIndex < 0) {
163 if (displayIndex != value) {
164 if (value < 0 || value > Int32.MaxValue) {
165 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
167 displayIndex = value;
168 if (DataGridView != null) {
169 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
176 public int DividerWidth {
177 get { return dividerWidth; }
179 if (dividerWidth != value) {
180 dividerWidth = value;
181 if (DataGridView != null) {
182 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
189 public float FillWeight {
190 get { return fillWeight; }
193 /* 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.
195 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
200 [DefaultValue (false)]
201 [RefreshProperties (RefreshProperties.All)]
202 public override bool Frozen {
203 get { return frozen; }
204 set { frozen = value; }
206 /* 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.
210 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
211 public DataGridViewColumnHeaderCell HeaderCell {
212 get { return headerCell; }
214 if (headerCell != value) {
216 if (DataGridView != null) {
217 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
224 public string HeaderText {
225 get { return headerText; }
226 set { headerText = value; }
230 [EditorBrowsable (EditorBrowsableState.Advanced)]
231 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
232 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
233 get { return inheritedAutoSizeMode; }
237 public override DataGridViewCellStyle InheritedStyle {
239 if (DataGridView == null) {
240 return base.DefaultCellStyle;
243 if (base.DefaultCellStyle == null) {
244 return DataGridView.DefaultCellStyle;
247 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
248 /////// Combination with dataGridView.DefaultCellStyle
256 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
257 public bool IsDataBound {
258 get { return isDataBound; }
262 [RefreshProperties (RefreshProperties.Repaint)]
264 public int MinimumWidth {
265 get { return minimumWidth; }
267 if (minimumWidth != value) {
268 if (value < 2 || value > Int32.MaxValue) {
269 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
271 minimumWidth = value;
272 if (DataGridView != null) {
273 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
285 headerCell.Value = value;
286 if (DataGridView != null) {
287 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
293 public override bool ReadOnly {
294 get { return readOnly; }
295 set { readOnly = value; }
298 public override DataGridViewTriState Resizable {
299 get { return resizable; }
300 set { resizable = value; }
304 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
307 set { site = value; }
310 [DefaultValue (DataGridViewColumnSortMode.NotSortable)]
311 public DataGridViewColumnSortMode SortMode {
312 get { return sortMode; }
314 // System.InvalidOperationException: The value assigned to the property conflicts with System.Windows.Forms.DataGridView.SelectionMode.
315 if (sortMode != value) {
317 if (DataGridView != null) {
318 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
326 public string ToolTipText {
327 get { return toolTipText; }
329 if (toolTipText != value) {
331 if (DataGridView != null) {
332 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
339 [DefaultValue (null)]
340 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
341 public Type ValueType {
342 get { return valueType; }
343 set { valueType = value; }
346 [DefaultValue (true)]
348 public override bool Visible {
349 get { return visible; }
350 set { visible = value; }
354 [RefreshProperties (RefreshProperties.Repaint)]
356 get { return width; }
358 if (width != value) {
359 if (value < minimumWidth) {
360 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
363 if (DataGridView != null) {
364 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
371 // XXX should we do something like Component.Events?
373 [EditorBrowsable (EditorBrowsableState.Advanced)]
374 public event EventHandler Disposed;
376 public override object Clone () {
377 return this.MemberwiseClone();
379 DataGridViewColumn result = new DataGridViewColumn();
384 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
385 switch (autoSizeColumnMode) {
386 case DataGridViewAutoSizeColumnMode.NotSet:
387 case DataGridViewAutoSizeColumnMode.None:
388 case DataGridViewAutoSizeColumnMode.Fill:
389 throw new ArgumentException("AutoSizeColumnMode is invalid");
399 public override string ToString () {
400 return Name + ", Index: " + base.Index.ToString() + ".";
403 protected override void Dispose (bool disposing) {
408 internal override void SetDataGridView (DataGridView dataGridView) {
409 base.SetDataGridView(dataGridView);
410 if (cellTemplate != null) {
411 cellTemplate.SetDataGridView(dataGridView);
413 headerCell.SetDataGridView(dataGridView);
416 internal override void SetIndex (int index) {
417 base.SetIndex(index);
418 headerCell.SetColumnIndex(Index);
421 internal override void SetState (DataGridViewElementStates state) {
422 if (State != state) {
423 base.SetState(state);
424 if (DataGridView != null) {
425 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));