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:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
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.
19 //
20 // Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //	Pedro Martínez Juliá <pedromj@gmail.com>
24 //
25 
26 using System.Collections;
27 using System.ComponentModel;
28 using System.Drawing.Design;
29 using System.Drawing;
30 
31 namespace System.Windows.Forms {
32 
33 	[ToolboxBitmap ("")]
34 	[Designer ("System.Windows.Forms.Design.DataGridViewComboBoxColumnDesigner, " + Consts.AssemblySystem_Design,
35 		   "System.ComponentModel.Design.IDesigner")]
36 	public class DataGridViewComboBoxColumn : DataGridViewColumn
37 	{
38 		private bool autoComplete;
39 		private DataGridViewComboBoxDisplayStyle displayStyle;
40 		private bool displayStyleForCurrentCellOnly;
41 		private FlatStyle flatStyle;
42 
DataGridViewComboBoxColumn()43 		public DataGridViewComboBoxColumn ()
44 		{
45 			CellTemplate = new DataGridViewComboBoxCell();
46 			((DataGridViewComboBoxCell) CellTemplate).OwningColumnTemplate = this;
47 			SortMode = DataGridViewColumnSortMode.NotSortable;
48 			autoComplete = true;
49 			displayStyle = DataGridViewComboBoxDisplayStyle.DropDownButton;
50 			displayStyleForCurrentCellOnly = false;
51 		}
52 
53 		[Browsable (true)]
54 		[DefaultValue (true)]
55 		public bool AutoComplete {
56 			get { return autoComplete; }
57 			set { autoComplete = value; }
58 		}
59 
60 		[Browsable (false)]
61 		[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
62 		public override DataGridViewCell CellTemplate {
63 			get { return base.CellTemplate; }
64 			set {
65 
66 				DataGridViewComboBoxCell cellTemplate = value as DataGridViewComboBoxCell;
67 				if (cellTemplate == null)
68 					throw new InvalidCastException ("Invalid cell tempalte type.");
69 
70 				cellTemplate.OwningColumnTemplate = this;
71 				base.CellTemplate = cellTemplate;
72 			}
73 		}
74 
75 		[AttributeProvider (typeof (IListSource))]
76 		[DefaultValue (null)]
77 		[RefreshProperties (RefreshProperties.Repaint)]
78 		public object DataSource {
79 			get {
80 				if (base.CellTemplate == null) {
81 					throw new InvalidOperationException("CellTemplate is null.");
82 				}
83 				return (base.CellTemplate as DataGridViewComboBoxCell).DataSource; }
84 			set {
85 				if (base.CellTemplate == null) {
86 					throw new InvalidOperationException("CellTemplate is null.");
87 				}
88 				(base.CellTemplate as DataGridViewComboBoxCell).DataSource = value;
89 			}
90 		}
91 
92 		[Editor ("System.Windows.Forms.Design.DataMemberFieldEditor, " + Consts.AssemblySystem_Design,
93 			 "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
94 		[DefaultValue ("")]
95 		[TypeConverter ("System.Windows.Forms.Design.DataMemberFieldConverter, " + Consts.AssemblySystem_Design)]
96 		public string DisplayMember {
97 			get {
98 				if (base.CellTemplate == null) {
99 					throw new InvalidOperationException("CellTemplate is null.");
100 				}
101 				return (base.CellTemplate as DataGridViewComboBoxCell).DisplayMember;
102 			}
103 			set {
104 				if (base.CellTemplate == null) {
105 					throw new InvalidOperationException("CellTemplate is null.");
106 				}
107 				(base.CellTemplate as DataGridViewComboBoxCell).DisplayMember = value;
108 			}
109 		}
110 
111 		[DefaultValue (DataGridViewComboBoxDisplayStyle.DropDownButton)]
112 		public DataGridViewComboBoxDisplayStyle DisplayStyle {
113 			get { return displayStyle; }
114 			set { displayStyle = value; }
115 		}
116 
117 		[DefaultValue (false)]
118 		public bool DisplayStyleForCurrentCellOnly {
119 			get { return displayStyleForCurrentCellOnly; }
120 			set { displayStyleForCurrentCellOnly = value; }
121 		}
122 
123 		[DefaultValue (1)]
124 		public int DropDownWidth {
125 			get {
126 				if (base.CellTemplate == null) {
127 					throw new InvalidOperationException("CellTemplate is null.");
128 				}
129 				return (base.CellTemplate as DataGridViewComboBoxCell).DropDownWidth;
130 			}
131 			set {
132 				if (value < 1) {
133 					throw new ArgumentException("Value is less than 1.");
134 				}
135 				if (base.CellTemplate == null) {
136 					throw new InvalidOperationException("CellTemplate is null.");
137 				}
138 				(base.CellTemplate as DataGridViewComboBoxCell).DropDownWidth = value;
139 			}
140 		}
141 
142 		[DefaultValue (FlatStyle.Standard)]
143 		public FlatStyle FlatStyle {
144 			get { return flatStyle; }
145 			set { flatStyle = value; }
146 		}
147 
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 {
152 			get {
153 				if (base.CellTemplate == null) {
154 					throw new InvalidOperationException("CellTemplate is null.");
155 				}
156 				return (base.CellTemplate as DataGridViewComboBoxCell).Items;
157 			}
158 		}
159 
160 		[DefaultValue (8)]
161 		public int MaxDropDownItems {
162 			get {
163 				if (base.CellTemplate == null) {
164 					throw new InvalidOperationException("CellTemplate is null.");
165 				}
166 				return (base.CellTemplate as DataGridViewComboBoxCell).MaxDropDownItems;
167 			}
168 			set {
169 				if (base.CellTemplate == null) {
170 					throw new InvalidOperationException("CellTemplate is null.");
171 				}
172 				(base.CellTemplate as DataGridViewComboBoxCell).MaxDropDownItems = value;
173 			}
174 		}
175 
176 		[DefaultValue (false)]
177 		public bool Sorted {
178 			get {
179 				if (base.CellTemplate == null) {
180 					throw new InvalidOperationException("CellTemplate is null.");
181 				}
182 				return (base.CellTemplate as DataGridViewComboBoxCell).Sorted;
183 			}
184 			set {
185 				if (base.CellTemplate == null) {
186 					throw new InvalidOperationException("CellTemplate is null.");
187 				}
188 				(base.CellTemplate as DataGridViewComboBoxCell).Sorted = value;
189 			}
190 		}
191 
192 		[DefaultValue ("")]
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 {
197 			get {
198 				if (base.CellTemplate == null) {
199 					throw new InvalidOperationException("CellTemplate is null.");
200 				}
201 				return (base.CellTemplate as DataGridViewComboBoxCell).ValueMember;
202 			}
203 			set {
204 				if (base.CellTemplate == null) {
205 					throw new InvalidOperationException("CellTemplate is null.");
206 				}
207 				(base.CellTemplate as DataGridViewComboBoxCell).ValueMember = value;
208 			}
209 		}
210 
SyncItems(IList items)211 		internal void SyncItems (IList items)
212 		{
213 			if (DataSource != null || DataGridView == null)
214 				return;
215 
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);
221 				}
222 			}
223 		}
224 
Clone()225 		public override object Clone ()
226 		{
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();
233 			return col;
234 		}
235 
ToString()236 		public override string ToString ()
237 		{
238 			return GetType().Name;
239 		}
240 
241 	}
242 
243 }
244