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 public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
36 private DataGridViewAutoSizeColumnMode autoSizeMode;
37 private DataGridViewCell cellTemplate;
38 //private ContextMenuStrip contextMenuStrip;
39 private string dataPropertyName;
40 private int displayIndex;
41 private int dividerWidth;
42 private float fillWeight;
44 private DataGridViewColumnHeaderCell headerCell;
45 private string headerText = "";
46 private DataGridViewAutoSizeColumnMode inheritedAutoSizeMode;
47 private bool isDataBound;
48 private int minimumWidth = 5;
49 private string name = "";
50 private bool readOnly;
51 private DataGridViewTriState resizable = DataGridViewTriState.True;
53 private DataGridViewColumnSortMode sortMode;
54 private string toolTipText;
55 private Type valueType;
56 private bool visible = true;
57 private int width = 100;
59 public DataGridViewColumn () {
61 base.DefaultCellStyle = new DataGridViewCellStyle();
63 headerCell = new DataGridViewColumnHeaderCell();
64 headerCell.SetColumnIndex(Index);
68 public DataGridViewColumn (DataGridViewCell cellTemplate) : this () {
69 this.cellTemplate = (DataGridViewCell) cellTemplate.Clone();
72 public DataGridViewAutoSizeColumnMode AutoSizeMode {
73 get { return autoSizeMode; }
74 set { autoSizeMode = value; }
77 public virtual DataGridViewCell CellTemplate {
78 get { return cellTemplate; }
81 if (DataGridView != null) {
82 cellTemplate.SetDataGridView(DataGridView);
87 public Type CellType {
89 if (cellTemplate == null) {
92 return cellTemplate.GetType();
97 public override ContextMenuStrip ContextMenuStrip {
98 get { return contextMenuStrip; }
100 if (contextMenuStrip != value) {
101 contextMenuStrip = value;
102 if (DataGridView != null) {
103 DataGridView.OnColumnContextMenuStripChanged(new DataGridViewColumnEventArgs(this));
110 public string DataPropertyName {
111 get { return dataPropertyName; }
113 if (dataPropertyName != value) {
114 dataPropertyName = value;
115 if (DataGridView != null) {
116 DataGridView.OnColumnDataPropertyNameChanged(new DataGridViewColumnEventArgs(this));
122 public override DataGridViewCellStyle DefaultCellStyle {
124 return base.DefaultCellStyle;
127 if (DefaultCellStyle != value) {
128 base.DefaultCellStyle = value;
129 if (DataGridView != null) {
130 DataGridView.OnColumnDefaultCellStyleChanged(new DataGridViewColumnEventArgs(this));
136 public int DisplayIndex {
138 if (displayIndex < 0) {
144 if (displayIndex != value) {
145 if (value < 0 || value > Int32.MaxValue) {
146 throw new ArgumentOutOfRangeException("DisplayIndex is out of range");
148 displayIndex = value;
149 if (DataGridView != null) {
150 DataGridView.OnColumnDisplayIndexChanged(new DataGridViewColumnEventArgs(this));
156 public int DividerWidth {
157 get { return dividerWidth; }
159 if (dividerWidth != value) {
160 dividerWidth = value;
161 if (DataGridView != null) {
162 DataGridView.OnColumnDividerWidthChanged(new DataGridViewColumnEventArgs(this));
168 public float FillWeight {
169 get { return fillWeight; }
172 /* 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.
174 The maximum sum of System.Windows.Forms.DataGridViewColumn.FillWeight values for all columns in a System.Windows.Forms.DataGridView control is 65535.
179 public override bool Frozen {
180 get { return frozen; }
181 set { frozen = value; }
183 /* 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.
186 public DataGridViewColumnHeaderCell HeaderCell {
187 get { return headerCell; }
189 if (headerCell != value) {
191 if (DataGridView != null) {
192 DataGridView.OnColumnHeaderCellChanged(new DataGridViewColumnEventArgs(this));
198 public string HeaderText {
199 get { return headerText; }
200 set { headerText = value; }
203 public DataGridViewAutoSizeColumnMode InheritedAutoSizeMode {
204 get { return inheritedAutoSizeMode; }
207 public override DataGridViewCellStyle InheritedStyle {
209 if (DataGridView == null) {
210 return base.DefaultCellStyle;
213 if (base.DefaultCellStyle == null) {
214 return DataGridView.DefaultCellStyle;
217 DataGridViewCellStyle style = (DataGridViewCellStyle) base.DefaultCellStyle.Clone();
218 /////// Combination with dataGridView.DefaultCellStyle
225 public bool IsDataBound {
226 get { return isDataBound; }
229 public int MinimumWidth {
230 get { return minimumWidth; }
232 if (minimumWidth != value) {
233 if (value < 2 || value > Int32.MaxValue) {
234 throw new ArgumentOutOfRangeException("MinimumWidth is out of range");
236 minimumWidth = value;
237 if (DataGridView != null) {
238 DataGridView.OnColumnMinimumWidthChanged(new DataGridViewColumnEventArgs(this));
249 headerCell.Value = value;
250 if (DataGridView != null) {
251 DataGridView.OnColumnNameChanged(new DataGridViewColumnEventArgs(this));
257 public override bool ReadOnly {
258 get { return readOnly; }
259 set { readOnly = value; }
262 public override DataGridViewTriState Resizable {
263 get { return resizable; }
264 set { resizable = value; }
267 public virtual ISite Site {
269 set { site = value; }
272 public DataGridViewColumnSortMode SortMode {
273 get { return sortMode; }
275 // System.InvalidOperationException: The value assigned to the property conflicts with System.Windows.Forms.DataGridView.SelectionMode.
276 if (sortMode != value) {
278 if (DataGridView != null) {
279 DataGridView.OnColumnSortModeChanged(new DataGridViewColumnEventArgs(this));
285 public string ToolTipText {
286 get { return toolTipText; }
288 if (toolTipText != value) {
290 if (DataGridView != null) {
291 DataGridView.OnColumnToolTipTextChanged(new DataGridViewColumnEventArgs(this));
297 public Type ValueType {
298 get { return valueType; }
299 set { valueType = value; }
302 public override bool Visible {
303 get { return visible; }
304 set { visible = value; }
308 get { return width; }
310 if (width != value) {
311 if (value < minimumWidth) {
312 throw new ArgumentOutOfRangeException("Width is less than MinimumWidth");
315 if (DataGridView != null) {
316 DataGridView.OnColumnWidthChanged(new DataGridViewColumnEventArgs(this));
323 //public sealed event EventHandler Disposed;
324 public event EventHandler Disposed;
326 public override object Clone () {
327 return this.MemberwiseClone();
329 DataGridViewColumn result = new DataGridViewColumn();
334 //public sealed void Dispose () {
337 public virtual int GetPreferredWidth (DataGridViewAutoSizeColumnMode autoSizeColumnMode, bool fixedHeight) {
338 switch (autoSizeColumnMode) {
339 case DataGridViewAutoSizeColumnMode.NotSet:
340 case DataGridViewAutoSizeColumnMode.None:
341 case DataGridViewAutoSizeColumnMode.Fill:
342 throw new ArgumentException("AutoSizeColumnMode is invalid");
352 public override string ToString () {
353 return Name + ", Index: " + base.Index.ToString() + ".";
356 protected override void Dispose (bool disposing) {
361 internal override void SetDataGridView (DataGridView dataGridView) {
362 base.SetDataGridView(dataGridView);
363 if (cellTemplate != null) {
364 cellTemplate.SetDataGridView(dataGridView);
366 headerCell.SetDataGridView(dataGridView);
369 internal override void SetIndex (int index) {
370 base.SetIndex(index);
371 headerCell.SetColumnIndex(Index);
374 internal override void SetState (DataGridViewElementStates state) {
375 if (State != state) {
376 base.SetState(state);
377 if (DataGridView != null) {
378 DataGridView.OnColumnStateChanged(new DataGridViewColumnStateChangedEventArgs(this, state));