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>
29 using System
.ComponentModel
;
31 using System
.Runtime
.InteropServices
;
33 namespace System
.Windows
.Forms
{
35 // XXX [TypeConverter (typeof (DataGridRowConverter))]
36 public class DataGridViewRow
: DataGridViewBand
{
38 private AccessibleObject accessibilityObject
;
39 private DataGridViewCellCollection cells
;
40 private ContextMenuStrip contextMenuStrip
;
41 private object dataBoundItem
;
42 private int dividerHeight
;
43 private string errorText
;
44 private DataGridViewRowHeaderCell headerCell
;
46 private int minimumHeight
;
48 public DataGridViewRow ()
50 cells
= new DataGridViewCellCollection(this);
53 headerCell
= new DataGridViewRowHeaderCell();
57 public AccessibleObject AccessibilityObject
{
58 get { return accessibilityObject; }
62 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
63 public DataGridViewCellCollection Cells
{
68 public override ContextMenuStrip ContextMenuStrip
{
69 get { return contextMenuStrip; }
71 if (contextMenuStrip
!= value) {
72 contextMenuStrip
= value;
73 if (DataGridView
!= null) {
74 DataGridView
.OnRowContextMenuStripChanged(new DataGridViewRowEventArgs(this));
81 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
82 public object DataBoundItem
{
83 get { return dataBoundItem; }
87 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Content
)]
88 [NotifyParentProperty (true)]
89 public override DataGridViewCellStyle DefaultCellStyle
{
90 get { return base.DefaultCellStyle; }
92 if (DefaultCellStyle
!= value) {
93 base.DefaultCellStyle
= value;
94 if (DataGridView
!= null) {
95 DataGridView
.OnRowDefaultCellStyleChanged(new DataGridViewRowEventArgs(this));
102 public override bool Displayed
{
103 get { return base.Displayed; }
107 [NotifyParentProperty (true)]
108 public int DividerHeight
{
109 get { return dividerHeight; }
110 set { dividerHeight = value; }
114 [NotifyParentProperty (true)]
115 public string ErrorText
{
116 get { return errorText; }
118 if (errorText
!= value) {
120 if (DataGridView
!= null) {
121 DataGridView
.OnRowErrorTextChanged(new DataGridViewRowEventArgs(this));
128 public override bool Frozen
{
129 get { return base.Frozen; }
130 set { base.Frozen = value; }
134 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
135 public DataGridViewRowHeaderCell HeaderCell
{
136 get { return headerCell; }
138 if (headerCell
!= value) {
140 if (DataGridView
!= null) {
141 DataGridView
.OnRowHeaderCellChanged(new DataGridViewRowEventArgs(this));
148 [NotifyParentProperty (true)]
152 if (DefaultCellStyle
!= null && DefaultCellStyle
.Font
!= null) {
153 return DefaultCellStyle
.Font
.Height
+ 9;
155 if (InheritedStyle
!= null && InheritedStyle
.Font
!= null) {
156 return InheritedStyle
.Font
.Height
+ 9;
158 return System
.Windows
.Forms
.Control
.DefaultFont
.Height
+ 9;
163 if (height
!= value) {
164 if (height
< minimumHeight
) {
165 throw new ArgumentOutOfRangeException("Height can't be less than MinimumHeight.");
168 if (DataGridView
!= null) {
169 DataGridView
.OnRowHeightChanged(new DataGridViewRowEventArgs(this));
175 public override DataGridViewCellStyle InheritedStyle
{
177 if (DataGridView
== null) {
178 return DefaultCellStyle
;
181 if (DefaultCellStyle
== null) {
182 return DataGridView
.DefaultCellStyle
;
185 DataGridViewCellStyle style
= (DataGridViewCellStyle
) DefaultCellStyle
.Clone();
186 /////// Combination with dataGridView.DefaultCellStyle
194 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
195 public bool IsNewRow
{
197 if (DataGridView
!= null && DataGridView
.Rows
[DataGridView
.Rows
.Count
- 1] == this) {
205 [DesignerSerializationVisibility (DesignerSerializationVisibility
.Hidden
)]
206 public int MinimumHeight
{
207 get { return minimumHeight; }
209 if (minimumHeight
!= value) {
210 if (value < 2 || value > Int32
.MaxValue
) {
211 throw new ArgumentOutOfRangeException("MinimumHeight should be between 2 and Int32.MaxValue.");
213 minimumHeight
= value;
214 if (DataGridView
!= null) {
215 DataGridView
.OnRowMinimumHeightChanged(new DataGridViewRowEventArgs(this));
222 [DefaultValue (false)]
223 [NotifyParentProperty (true)]
224 public override bool ReadOnly
{
225 get { return base.ReadOnly; }
226 set { base.ReadOnly = value; }
229 [NotifyParentProperty (true)]
230 public override DataGridViewTriState Resizable
{
231 get { return base.Resizable; }
232 set { base.Resizable = value; }
235 public override bool Selected
{
238 throw new InvalidOperationException("The row is a shared row.");
240 if (DataGridView
== null) {
241 throw new InvalidOperationException("The row has not been added to a DataGridView control.");
243 return base.Selected
;
247 throw new InvalidOperationException("The row is a shared row.");
249 if (DataGridView
== null) {
250 throw new InvalidOperationException("The row has not been added to a DataGridView control.");
252 base.Selected
= value;
253 foreach (DataGridViewCell cell
in cells
) {
254 cell
.Selected
= value;
259 public override DataGridViewElementStates State
{
260 get { return base.State; }
264 public override bool Visible
{
265 get { return base.Visible; }
267 if (IsNewRow
&& value == false) {
268 throw new InvalidOperationException("Cant make invisible a new row.");
270 base.Visible
= value;
274 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
275 public virtual DataGridViewAdvancedBorderStyle
AdjustRowHeaderBorderStyle (DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStyleInput
, DataGridViewAdvancedBorderStyle dataGridViewAdvancedBorderStylePlaceholder
, bool singleVerticalBorderAdded
, bool singleHorizontalBorderAdded
, bool isFirstDisplayedRow
, bool isLastVisibleRow
)
277 throw new NotImplementedException();
280 public override object Clone ()
282 DataGridViewRow row
= (DataGridViewRow
) MemberwiseClone();
283 row
.cells
= new DataGridViewCellCollection(row
);
284 foreach (DataGridViewCell cell
in cells
) {
285 row
.cells
.Add(cell
.Clone() as DataGridViewCell
);
290 public void CreateCells (DataGridView dataGridView
)
292 if (dataGridView
== null) {
293 throw new ArgumentNullException("DataGridView is null.");
295 if (dataGridView
.Rows
.Contains(this)) {
296 throw new InvalidOperationException("The row already exists in the DataGridView.");
298 DataGridViewCellCollection newCellCollection
= new DataGridViewCellCollection(this);
299 foreach (DataGridViewColumn column
in dataGridView
.Columns
) {
300 if (column
.CellTemplate
== null) {
301 throw new InvalidOperationException("Cell template not set in column: " + column
.Index
.ToString() + ".");
303 newCellCollection
.Add((DataGridViewCell
) column
.CellTemplate
.Clone());
305 cells
= newCellCollection
;
308 public void CreateCells (DataGridView dataGridView
, params object[] values
)
310 if (values
== null) {
311 throw new ArgumentNullException("values is null");
313 CreateCells(dataGridView
);
314 for (int i
= 0; i
< values
.Length
; i
++) {
315 cells
[i
].Value
= values
[i
];
319 public ContextMenuStrip
GetContextMenuStrip (int rowIndex
)
321 if (rowIndex
== -1) {
322 throw new InvalidOperationException("rowIndex is -1");
324 if (rowIndex
< 0 || rowIndex
>= DataGridView
.Rows
.Count
) {
325 throw new ArgumentOutOfRangeException("rowIndex is out of range");
331 public string GetErrorText (int rowIndex
)
336 public virtual int GetPreferredHeight (int rowIndex
, DataGridViewAutoSizeRowMode autoSizeRowMode
, bool fixedWidth
)
338 throw new NotImplementedException();
341 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
342 public virtual DataGridViewElementStates
GetState (int rowIndex
)
344 throw new NotImplementedException();
347 public bool SetValues (params object[] values
)
349 if (values
== null) {
350 throw new ArgumentNullException("vues is null");
352 if (DataGridView
!= null && DataGridView
.VirtualMode
) {
353 throw new InvalidOperationException("DataGridView is operating in virtual mode");
355 /////// COLUMNAS //////////
356 for (int i
= 0; i
< values
.Length
; i
++) {
357 DataGridViewCell cell
= new DataGridViewTextBoxCell();
358 cell
.Value
= values
[i
];
366 public override string ToString ()
368 return this.GetType().Name
+ ", Band Index: " + base.Index
.ToString();
371 protected virtual AccessibleObject
CreateAccessibilityInstance ()
373 return new DataGridViewRowAccessibleObject(this);
376 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
377 protected virtual DataGridViewCellCollection
CreateCellsInstance ()
379 cells
= new DataGridViewCellCollection(this);
383 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
384 protected internal virtual void DrawFocus (Graphics graphics
, Rectangle clipBounds
, Rectangle bounds
, int rowIndex
, DataGridViewElementStates rowState
, DataGridViewCellStyle cellStyle
, bool cellsPaintSelectionBackground
)
388 protected internal virtual void Paint (Graphics graphics
, Rectangle clipBounds
, Rectangle rowBounds
, int rowIndex
, DataGridViewElementStates rowState
, bool isFirstDisplayedRow
, bool isLastVisibleRow
)
392 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
393 protected internal virtual void PaintCells (Graphics graphics
, Rectangle clipBounds
, Rectangle rowBounds
, int rowIndex
, DataGridViewElementStates rowState
, bool isFirstDisplayedRow
, bool isLastVisibleRow
, DataGridViewPaintParts paintParts
)
397 [EditorBrowsable (EditorBrowsableState
.Advanced
)]
398 protected internal virtual void PaintHeader (Graphics graphics
, Rectangle clipBounds
, Rectangle rowBounds
, int rowIndex
, DataGridViewElementStates rowState
, bool isFirstDisplayedRow
, bool isLastVisibleRow
, DataGridViewPaintParts paintParts
)
402 internal override void SetDataGridView (DataGridView dataGridView
)
404 base.SetDataGridView(dataGridView
);
405 headerCell
.SetDataGridView(dataGridView
);
408 internal override void SetState (DataGridViewElementStates state
)
410 if (State
!= state
) {
411 base.SetState(state
);
412 if (DataGridView
!= null) {
413 DataGridView
.OnRowStateChanged(this.Index
, new DataGridViewRowStateChangedEventArgs(this, state
));
418 [ComVisibleAttribute(true)]
419 protected class DataGridViewRowAccessibleObject
: AccessibleObject
{
421 private DataGridViewRow dataGridViewRow
;
423 public DataGridViewRowAccessibleObject ()
427 public DataGridViewRowAccessibleObject (DataGridViewRow row
)
429 this.dataGridViewRow
= row
;
432 public override Rectangle Bounds
{
433 get { throw new NotImplementedException(); }
436 public override string DefaultAction
{
437 get { return "Edit"; }
440 public override string Name
{
441 get { return "Index: " + dataGridViewRow.Index.ToString(); }
444 public DataGridViewRow Owner
{
445 get { return dataGridViewRow; }
446 set { dataGridViewRow = value; }
449 public override AccessibleObject Parent
{
450 get { return dataGridViewRow.AccessibilityObject; }
453 public override AccessibleRole Role
{
454 get { return AccessibleRole.Row; }
457 public override AccessibleStates State
{
459 if (dataGridViewRow
.Selected
) {
460 return AccessibleStates
.Selected
;
463 return AccessibleStates
.Focused
;
468 public override string Value
{
470 if (dataGridViewRow
.Cells
.Count
== 0) {
471 return "(Create New)";
474 foreach (DataGridViewCell cell
in dataGridViewRow
.Cells
) {
475 result
+= cell
.AccessibilityObject
.Value
;
481 public override AccessibleObject
GetChild (int index
) {
482 throw new NotImplementedException();
485 public override int GetChildCount () {
486 throw new NotImplementedException();
489 public override AccessibleObject
GetFocused () {
493 public override AccessibleObject
GetSelected () {
497 public override AccessibleObject
Navigate (AccessibleNavigation navigationDirection
) {
498 switch (navigationDirection
) {
499 case AccessibleNavigation
.Right
:
501 case AccessibleNavigation
.Left
:
503 case AccessibleNavigation
.Next
:
505 case AccessibleNavigation
.Previous
:
507 case AccessibleNavigation
.Up
:
509 case AccessibleNavigation
.Down
:
517 public override void Select (AccessibleSelection flags
) {
519 case AccessibleSelection
.TakeFocus
:
520 dataGridViewRow
.DataGridView
.Focus();
522 case AccessibleSelection
.TakeSelection
:
523 //dataGridViewRow.Focus();
525 case AccessibleSelection
.AddSelection
:
526 dataGridViewRow
.DataGridView
.SelectedRows
.InternalAdd(dataGridViewRow
);
528 case AccessibleSelection
.RemoveSelection
:
529 dataGridViewRow
.DataGridView
.SelectedRows
.InternalRemove(dataGridViewRow
);