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>
26 using System.Collections;
27 using System.ComponentModel;
28 using System.Drawing.Design;
31 namespace System.Windows.Forms {
34 [Designer ("System.Windows.Forms.Design.DataGridViewComboBoxColumnDesigner, " + Consts.AssemblySystem_Design,
35 "System.ComponentModel.Design.IDesigner")]
36 public class DataGridViewComboBoxColumn : DataGridViewColumn
38 private bool autoComplete;
39 private DataGridViewComboBoxDisplayStyle displayStyle;
40 private bool displayStyleForCurrentCellOnly;
41 private FlatStyle flatStyle;
43 public DataGridViewComboBoxColumn ()
45 CellTemplate = new DataGridViewComboBoxCell();
46 ((DataGridViewComboBoxCell) CellTemplate).OwningColumnTemplate = this;
47 SortMode = DataGridViewColumnSortMode.NotSortable;
49 displayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
50 displayStyleForCurrentCellOnly = false;
55 public bool AutoComplete {
56 get { return autoComplete; }
57 set { autoComplete = value; }
61 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
62 public override DataGridViewCell CellTemplate {
63 get { return base.CellTemplate; }
66 DataGridViewComboBoxCell cellTemplate = value as DataGridViewComboBoxCell;
67 if (cellTemplate == null)
68 throw new InvalidCastException ("Invalid cell tempalte type.");
70 cellTemplate.OwningColumnTemplate = this;
71 base.CellTemplate = cellTemplate;
75 [AttributeProvider (typeof (IListSource))]
77 [RefreshProperties (RefreshProperties.Repaint)]
78 public object DataSource {
80 if (base.CellTemplate == null) {
81 throw new InvalidOperationException("CellTemplate is null.");
83 return (base.CellTemplate as DataGridViewComboBoxCell).DataSource; }
85 if (base.CellTemplate == null) {
86 throw new InvalidOperationException("CellTemplate is null.");
88 (base.CellTemplate as DataGridViewComboBoxCell).DataSource = value;
92 [Editor ("System.Windows.Forms.Design.DataMemberFieldEditor, " + Consts.AssemblySystem_Design,
93 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
95 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
96 public string DisplayMember {
98 if (base.CellTemplate == null) {
99 throw new InvalidOperationException("CellTemplate is null.");
101 return (base.CellTemplate as DataGridViewComboBoxCell).DisplayMember;
104 if (base.CellTemplate == null) {
105 throw new InvalidOperationException("CellTemplate is null.");
107 (base.CellTemplate as DataGridViewComboBoxCell).DisplayMember = value;
111 [DefaultValue (DataGridViewComboBoxDisplayStyle.DropDownButton)]
112 public DataGridViewComboBoxDisplayStyle DisplayStyle {
113 get { return displayStyle; }
114 set { displayStyle = value; }
117 [DefaultValue (false)]
118 public bool DisplayStyleForCurrentCellOnly {
119 get { return displayStyleForCurrentCellOnly; }
120 set { displayStyleForCurrentCellOnly = value; }
124 public int DropDownWidth {
126 if (base.CellTemplate == null) {
127 throw new InvalidOperationException("CellTemplate is null.");
129 return (base.CellTemplate as DataGridViewComboBoxCell).DropDownWidth;
133 throw new ArgumentException("Value is less than 1.");
135 if (base.CellTemplate == null) {
136 throw new InvalidOperationException("CellTemplate is null.");
138 (base.CellTemplate as DataGridViewComboBoxCell).DropDownWidth = value;
142 [DefaultValue (FlatStyle.Standard)]
143 public FlatStyle FlatStyle {
144 get { return flatStyle; }
145 set { flatStyle = value; }
148 [Editor ("System.Windows.Forms.Design.StringCollectionEditor, " + Consts.AssemblySystem_Design,
149 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
150 [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
151 public DataGridViewComboBoxCell.ObjectCollection Items {
153 if (base.CellTemplate == null) {
154 throw new InvalidOperationException("CellTemplate is null.");
156 return (base.CellTemplate as DataGridViewComboBoxCell).Items;
161 public int MaxDropDownItems {
163 if (base.CellTemplate == null) {
164 throw new InvalidOperationException("CellTemplate is null.");
166 return (base.CellTemplate as DataGridViewComboBoxCell).MaxDropDownItems;
169 if (base.CellTemplate == null) {
170 throw new InvalidOperationException("CellTemplate is null.");
172 (base.CellTemplate as DataGridViewComboBoxCell).MaxDropDownItems = value;
176 [DefaultValue (false)]
179 if (base.CellTemplate == null) {
180 throw new InvalidOperationException("CellTemplate is null.");
182 return (base.CellTemplate as DataGridViewComboBoxCell).Sorted;
185 if (base.CellTemplate == null) {
186 throw new InvalidOperationException("CellTemplate is null.");
188 (base.CellTemplate as DataGridViewComboBoxCell).Sorted = value;
193 [Editor ("System.Windows.Forms.Design.DataMemberFieldEditor, " + Consts.AssemblySystem_Design,
194 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
195 [TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
196 public string ValueMember {
198 if (base.CellTemplate == null) {
199 throw new InvalidOperationException("CellTemplate is null.");
201 return (base.CellTemplate as DataGridViewComboBoxCell).ValueMember;
204 if (base.CellTemplate == null) {
205 throw new InvalidOperationException("CellTemplate is null.");
207 (base.CellTemplate as DataGridViewComboBoxCell).ValueMember = value;
211 internal void SyncItems (IList items)
213 if (DataSource != null || DataGridView == null)
216 for (int i = 0; i < DataGridView.RowCount; i++) {
217 DataGridViewComboBoxCell comboCell = DataGridView.Rows[i].Cells[base.Index] as DataGridViewComboBoxCell;
218 if (comboCell != null) {
219 comboCell.Items.ClearInternal ();
220 comboCell.Items.AddRangeInternal (this.Items);
225 public override object Clone ()
227 DataGridViewComboBoxColumn col = (DataGridViewComboBoxColumn) base.Clone();
228 col.autoComplete = this.autoComplete;
229 col.displayStyle = this.displayStyle;
230 col.displayStyleForCurrentCellOnly = this.displayStyleForCurrentCellOnly;
231 col.flatStyle = this.flatStyle;
232 col.CellTemplate = (DataGridViewComboBoxCell) this.CellTemplate.Clone();
236 public override string ToString ()
238 return GetType().Name;