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 // Jordi Mas i Hernandez <jordi@ximian.com>
31 using System.ComponentModel;
33 using System.Runtime.InteropServices;
34 using System.Collections;
36 namespace System.Windows.Forms
38 [DefaultEvent("Navigate")]
39 [DefaultProperty("DataSource")]
40 [Designer("System.Windows.Forms.Design.DataGridDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
41 public class DataGrid : Control, ISupportInitialize, IDataGridEditingService
45 public enum HitTestType
57 public sealed class HitTestInfo
59 public static readonly HitTestInfo Nowhere = null;
61 #region Local Variables
64 internal HitTestType type;
65 #endregion // Local Variables
67 #region Private Constructors
68 internal HitTestInfo ()
72 type = HitTestType.None;
77 #region Public Instance Properties
79 get { return column; }
85 public DataGrid.HitTestType Type {
88 #endregion //Public Instance Properties
90 public override bool Equals (object o)
92 if (!(o is HitTestInfo))
95 HitTestInfo obj = (HitTestInfo) o;
96 return (obj.Column == column && obj.Row == row && obj.Type ==type);
99 public override int GetHashCode ()
104 public override string ToString ()
106 return base.ToString ();
111 #region Local Variables
112 private static readonly Color def_alternating_backcolor = ThemeEngine.Current.DataGridAlternatingBackColor;
113 private static readonly Color def_background_color = ThemeEngine.Current.DataGridBackgroundColor;
114 private static readonly Color def_caption_backcolor = ThemeEngine.Current.DataGridCaptionBackColor;
115 private static readonly Color def_caption_forecolor = ThemeEngine.Current.DataGridCaptionForeColor;
116 private static readonly Color def_gridline_color = ThemeEngine.Current.DataGridGridLineColor;
117 private static readonly Color def_header_backcolor = ThemeEngine.Current.DataGridHeaderBackColor;
118 private static readonly Font def_header_font = ThemeEngine.Current.DefaultFont;
119 private static readonly Color def_header_forecolor = ThemeEngine.Current.DataGridHeaderForeColor;
120 private static readonly Color def_link_hovercolor = ThemeEngine.Current.DataGridLinkHoverColor;
121 private static readonly Color def_parentrowsback_color = ThemeEngine.Current.DataGridParentRowsBackColor;
122 private static readonly Color def_parentrowsfore_color = ThemeEngine.Current.DataGridParentRowsForeColor;
123 private static readonly Color def_selection_backcolor = ThemeEngine.Current.DataGridSelectionBackColor;
124 private static readonly Color def_selection_forecolor = ThemeEngine.Current.DataGridSelectionForeColor;
125 private static readonly Color def_link_color = ThemeEngine.Current.DataGridLinkColor;
126 internal readonly int def_preferredrow_height;
128 private bool allow_navigation;
129 private bool allow_sorting;
130 private Color alternating_backcolor;
131 private Color background_color;
132 internal BorderStyle border_style;
133 private Color caption_backcolor;
134 private Font caption_font;
135 private Color caption_forecolor;
136 private string caption_text;
137 internal bool caption_visible;
138 internal bool columnheaders_visible;
139 private object datasource;
140 private object real_datasource;
141 private string datamember;
142 private int firstvisible_column;
143 private bool flatmode;
144 private Color gridline_color;
145 private DataGridLineStyle gridline_style;
146 private Color header_backcolor;
147 private Color header_forecolor;
148 private Font header_font;
149 private Color link_color;
150 private Color link_hovercolor;
151 private Color parentrowsback_color;
152 private Color parentrowsfore_color;
153 internal bool parentrows_visible;
154 private int preferredcolumn_width;
155 private int preferredrow_height;
156 private bool _readonly;
157 internal bool rowheaders_visible;
158 private Color selection_backcolor;
159 private Color selection_forecolor;
160 private int rowheaders_width;
161 internal int visiblecolumn_count;
162 internal int visiblerow_count;
163 internal int first_visiblecolumn;
164 private GridTableStylesCollection styles_collection;
165 private DataGridParentRowsLabelStyle parentrowslabel_style;
166 internal DataGridCell current_cell;
167 private Color forecolor;
168 private Color backcolor;
169 private DataGridTableStyle default_style;
170 private DataGridTableStyle current_style;
171 internal HScrollBar horiz_scrollbar;
172 internal VScrollBar vert_scrollbar;
173 internal DataGridDrawing grid_drawing;
174 internal int first_visiblerow;
175 internal int horz_pixeloffset;
176 internal bool is_editing; // Current cell is edit mode
177 internal bool is_changing; // Indicates if current cell is been changed (in edit mode)
178 internal bool is_adding; // Indicates when we are adding a row
179 private Hashtable selected_rows;
180 private bool ctrl_pressed;
181 private bool shift_pressed;
182 private bool begininit;
183 private CurrencyManager cached_currencymgr;
184 #endregion // Local Variables
186 #region Public Constructors
189 grid_drawing = new DataGridDrawing (this);
190 allow_navigation = true;
191 allow_sorting = true;
193 alternating_backcolor = def_alternating_backcolor;
194 background_color = def_background_color;
195 border_style = BorderStyle.Fixed3D;
196 caption_backcolor = def_caption_backcolor;
198 caption_forecolor = def_caption_forecolor;
199 caption_text = string.Empty;
200 caption_visible = true;
201 columnheaders_visible = true;
203 real_datasource = null;
204 datamember = string.Empty;
205 firstvisible_column = 0;
207 gridline_color = def_gridline_color;
208 gridline_style = DataGridLineStyle.Solid;
209 header_backcolor = def_header_backcolor;
210 header_forecolor = def_header_forecolor;
211 header_font = def_header_font;
212 link_color = def_link_color;
213 link_hovercolor = def_link_hovercolor;
214 parentrowsback_color = def_parentrowsback_color;
215 parentrowsfore_color = def_parentrowsfore_color;
216 parentrows_visible = false; // should be true (temp)
217 preferredcolumn_width = ThemeEngine.Current.DataGridPreferredColumnWidth;
219 rowheaders_visible = true;
220 selection_backcolor = def_selection_backcolor;
221 selection_forecolor = def_selection_forecolor;
222 rowheaders_width = 35;
223 visiblecolumn_count = 0;
224 visiblerow_count = 0;
225 current_cell = new DataGridCell ();
226 first_visiblerow = 0;
227 first_visiblecolumn = 0;
228 horz_pixeloffset = 0;
232 forecolor = SystemColors.WindowText;
233 parentrowslabel_style = DataGridParentRowsLabelStyle.Both;
234 backcolor = SystemColors.Window;
235 selected_rows = new Hashtable ();
236 ctrl_pressed = false;
237 shift_pressed = false;
238 preferredrow_height = def_preferredrow_height = FontHeight + 3;
239 cached_currencymgr = null;
241 default_style = new DataGridTableStyle (true);
242 styles_collection = new GridTableStylesCollection (this);
243 styles_collection.CollectionChanged += new CollectionChangeEventHandler (OnTableStylesCollectionChanged);
245 CurrentTableStyle = default_style;
247 horiz_scrollbar = new HScrollBar ();
248 horiz_scrollbar.Scroll += new ScrollEventHandler (GridHScrolled);
249 vert_scrollbar = new VScrollBar ();
250 vert_scrollbar.Scroll += new ScrollEventHandler (GridVScrolled);
251 KeyUp += new KeyEventHandler (OnKeyUpDG);
253 SetStyle (ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
257 #endregion // Public Constructor
259 #region Public Instance Properties
262 public bool AllowNavigation {
264 return allow_navigation;
268 if (allow_navigation != value) {
269 allow_navigation = value;
270 OnAllowNavigationChanged (EventArgs.Empty);
276 public bool AllowSorting {
278 return allow_sorting;
282 if (allow_sorting != value) {
283 allow_sorting = value;
288 public Color AlternatingBackColor {
290 return alternating_backcolor;
294 if (alternating_backcolor != value) {
295 alternating_backcolor = value;
301 public Color BackColor {
306 if (backcolor != value) {
313 public Color BackgroundColor {
315 return background_color;
318 if (background_color != value) {
319 background_color = value;
320 OnBackgroundColorChanged (EventArgs.Empty);
327 [EditorBrowsable(EditorBrowsableState.Never)]
328 public Image BackgroundImage {
330 return base.BackgroundImage;
334 base.BackgroundImage = value;
339 [DefaultValue(BorderStyle.Fixed3D)]
340 public BorderStyle BorderStyle {
346 if (border_style != value) {
347 border_style = value;
348 CalcAreasAndInvalidate ();
349 OnBorderStyleChanged (EventArgs.Empty);
354 public Color CaptionBackColor {
356 return caption_backcolor;
360 if (caption_backcolor != value) {
361 caption_backcolor = value;
362 grid_drawing.InvalidateCaption ();
369 public Font CaptionFont {
371 if (caption_font == null) {
379 if (caption_font != null && caption_font.Equals (value)) {
383 caption_font = value;
384 CalcAreasAndInvalidate ();
388 public Color CaptionForeColor {
390 return caption_forecolor;
394 if (caption_forecolor != value) {
395 caption_forecolor = value;
396 grid_drawing.InvalidateCaption ();
403 public string CaptionText {
409 if (caption_text != value) {
410 caption_text = value;
411 grid_drawing.InvalidateCaption ();
417 public bool CaptionVisible {
419 return caption_visible;
423 if (caption_visible != value) {
424 caption_visible = value;
425 CalcAreasAndInvalidate ();
426 OnCaptionVisibleChanged (EventArgs.Empty);
432 public bool ColumnHeadersVisible {
434 return columnheaders_visible;
438 if (columnheaders_visible != value) {
439 columnheaders_visible = value;
440 CalcAreasAndInvalidate ();
446 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
447 public DataGridCell CurrentCell {
453 if (!current_cell.Equals (value)) {
455 EnsureCellVisilibility (value);
456 current_cell = value;
457 OnCurrentCellChanged (EventArgs.Empty);
463 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
464 public int CurrentRowIndex {
466 return current_cell.RowNumber;
470 if (current_cell.RowNumber != value) {
471 CurrentCell = new DataGridCell (value, current_cell.ColumnNumber);
477 [EditorBrowsable(EditorBrowsableState.Never)]
478 public override Cursor Cursor {
488 [Editor ("System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
489 public string DataMember {
490 get { return datamember; }
492 if (SetDataMember (value)) {
494 if (styles_collection.Contains (value) == true) {
495 CurrentTableStyle = styles_collection[value];
496 current_style.CreateColumnsForTable (true);
498 CurrentTableStyle = default_style;
499 current_style.GridColumnStyles.Clear ();
500 current_style.CreateColumnsForTable (false);
507 [RefreshProperties(RefreshProperties.Repaint)]
508 [TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Design, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
509 public object DataSource {
515 if (SetDataSource (value)) {
521 protected override Size DefaultSize {
523 return new Size (130, 80);
528 public int FirstVisibleColumn {
530 return firstvisible_column;
534 [DefaultValue(false)]
535 public bool FlatMode {
541 if (flatmode != value) {
543 OnFlatModeChanged (EventArgs.Empty);
549 public Color ForeColor {
555 if (forecolor != value) {
557 OnForeColorChanged (EventArgs.Empty);
563 public Color GridLineColor {
565 return gridline_color;
569 if (value == Color.Empty) {
570 throw new ArgumentNullException ("Color.Empty value is invalid.");
573 if (gridline_color != value) {
574 gridline_color = value;
580 [DefaultValue(DataGridLineStyle.Solid)]
581 public DataGridLineStyle GridLineStyle {
583 return gridline_style;
587 if (gridline_style != value) {
588 gridline_style = value;
594 public Color HeaderBackColor {
596 return header_backcolor;
600 if (value == Color.Empty) {
601 throw new ArgumentNullException ("Color.Empty value is invalid.");
604 if (header_backcolor != value) {
605 header_backcolor = value;
611 public Font HeaderFont {
617 if (header_font != null && !header_font.Equals (value)) {
619 CalcAreasAndInvalidate ();
624 public Color HeaderForeColor {
626 return header_forecolor;
630 if (header_forecolor != value) {
631 header_forecolor = value;
637 protected ScrollBar HorizScrollBar {
639 return horiz_scrollbar;
643 public object this [DataGridCell cell] {
645 return this [cell.RowNumber, cell.ColumnNumber];
649 this [cell.RowNumber, cell.ColumnNumber] = value;
653 public object this [int rowIndex, int columnIndex] {
655 return CurrentTableStyle.GridColumnStyles[columnIndex].GetColumnValueAtRow (ListManager,
660 CurrentTableStyle.GridColumnStyles[columnIndex].SetColumnValueAtRow (ListManager,
665 public Color LinkColor {
670 if (link_color != value) {
679 [EditorBrowsable(EditorBrowsableState.Never)]
680 public Color LinkHoverColor {
682 return link_hovercolor;
686 if (link_hovercolor != value) {
687 link_hovercolor = value;
694 [EditorBrowsable(EditorBrowsableState.Advanced)]
695 protected internal CurrencyManager ListManager {
697 if (BindingContext == null || DataSource == null) {
701 if (cached_currencymgr != null) {
702 return cached_currencymgr;
705 cached_currencymgr = (CurrencyManager) BindingContext [real_datasource, DataMember];
706 return cached_currencymgr;
710 throw new NotSupportedException ("Operation is not supported.");
714 public Color ParentRowsBackColor {
716 return parentrowsback_color;
720 if (parentrowsback_color != value) {
721 parentrowsback_color = value;
722 if (parentrows_visible) {
729 public Color ParentRowsForeColor {
731 return parentrowsfore_color;
735 if (parentrowsfore_color != value) {
736 parentrowsfore_color = value;
737 if (parentrows_visible) {
744 [DefaultValue(DataGridParentRowsLabelStyle.Both)]
745 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
746 public DataGridParentRowsLabelStyle ParentRowsLabelStyle {
748 return parentrowslabel_style;
752 if (parentrowslabel_style != value) {
753 parentrowslabel_style = value;
754 if (parentrows_visible) {
758 OnParentRowsLabelStyleChanged (EventArgs.Empty);
764 public bool ParentRowsVisible {
766 return parentrows_visible;
770 if (parentrows_visible != value) {
771 parentrows_visible = value;
772 CalcAreasAndInvalidate ();
773 OnParentRowsVisibleChanged (EventArgs.Empty);
778 // Settting this property seems to have no effect.
780 [TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
781 public int PreferredColumnWidth {
783 return preferredcolumn_width;
788 throw new ArgumentException ("PreferredColumnWidth is less than 0");
791 if (preferredcolumn_width != value) {
792 preferredcolumn_width = value;
798 public int PreferredRowHeight {
800 return preferredrow_height;
804 if (preferredrow_height != value) {
805 preferredrow_height = value;
806 CalcAreasAndInvalidate ();
811 [DefaultValue(false)]
812 public bool ReadOnly {
818 if (_readonly != value) {
820 OnReadOnlyChanged (EventArgs.Empty);
821 CalcAreasAndInvalidate ();
827 public bool RowHeadersVisible {
829 return rowheaders_visible;
833 if (rowheaders_visible != value) {
834 rowheaders_visible = value;
835 CalcAreasAndInvalidate ();
841 public int RowHeaderWidth {
843 return rowheaders_width;
847 if (rowheaders_width != value) {
848 rowheaders_width = value;
849 CalcAreasAndInvalidate ();
854 public Color SelectionBackColor {
856 return selection_backcolor;
860 if (selection_backcolor != value) {
861 selection_backcolor = value;
867 public Color SelectionForeColor {
869 return selection_forecolor;
873 if (selection_forecolor != value) {
874 selection_forecolor = value;
880 public override ISite Site {
890 [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
891 public GridTableStylesCollection TableStyles {
892 get { return styles_collection; }
897 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
898 [EditorBrowsable(EditorBrowsableState.Never)]
899 public override string Text {
909 [EditorBrowsable(EditorBrowsableState.Advanced)]
910 protected ScrollBar VertScrollBar {
912 return vert_scrollbar;
917 public int VisibleColumnCount {
919 return visiblecolumn_count;
923 // Calculated at DataGridDrawing.CalcRowsHeaders
925 public int VisibleRowCount {
927 return visiblerow_count;
931 #endregion // Public Instance Properties
933 #region Private Instance Properties
934 internal DataGridTableStyle CurrentTableStyle {
936 return current_style;
939 current_style = value;
940 current_style.DataGrid = this;
941 CalcAreasAndInvalidate ();
945 internal int FirstVisibleRow {
946 get { return first_visiblerow; }
947 set { first_visiblerow = value;}
950 internal int RowsCount {
952 if (ListManager != null) {
953 return ListManager.Count;
960 internal int RowHeight {
962 if (CurrentTableStyle.CurrentPreferredRowHeight > Font.Height + 3 + 1 /* line */) {
963 return CurrentTableStyle.CurrentPreferredRowHeight;
966 return Font.Height + 3 + 1 /* line */;
971 internal bool ShowEditRow {
973 return _readonly == false;
977 #endregion Private Instance Properties
979 #region Public Instance Methods
982 public virtual bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
987 public virtual void BeginInit ()
992 protected virtual void CancelEditing ()
994 CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].Abort (current_cell.RowNumber);
996 if (is_adding == true) {
997 ListManager.RemoveAt (RowsCount - 1);
1002 is_changing = false;
1003 InvalidateCurrentRowHeader ();
1007 public void Collapse (int row)
1012 protected internal virtual void ColumnStartedEditing (Control editingControl)
1017 protected internal virtual void ColumnStartedEditing (Rectangle bounds)
1022 protected override AccessibleObject CreateAccessibilityInstance ()
1024 return base.CreateAccessibilityInstance ();
1027 protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
1029 return CreateGridColumn (prop, false);
1032 protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
1034 return CreateGridColumn (prop, isDefault);
1037 protected override void Dispose (bool disposing)
1039 base.Dispose (disposing);
1042 public virtual bool EndEdit (DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
1044 if (is_adding == true) {
1046 ListManager.CancelCurrentEdit ();
1048 ListManager.EndCurrentEdit ();
1049 CalcAreasAndInvalidate ();
1055 gridColumn.Abort (rowNumber);
1057 gridColumn.Commit (ListManager, rowNumber);
1061 is_changing = false;
1062 InvalidateCurrentRowHeader ();
1066 public virtual void EndInit ()
1071 public void Expand (int row)
1076 public Rectangle GetCellBounds (DataGridCell cell)
1078 return GetCellBounds (cell.RowNumber, cell.ColumnNumber);
1081 public Rectangle GetCellBounds (int row, int col)
1083 return grid_drawing.GetCellBounds (row, col);
1086 public Rectangle GetCurrentCellBounds ()
1088 return GetCellBounds (current_cell.RowNumber, current_cell.ColumnNumber);
1091 protected virtual string GetOutputTextDelimiter ()
1093 return string.Empty;
1096 protected virtual void GridHScrolled (object sender, ScrollEventArgs se)
1098 if (se.NewValue == horz_pixeloffset ||
1099 se.Type == ScrollEventType.EndScroll) {
1103 ScrollToColumnInPixels (se.NewValue);
1106 protected virtual void GridVScrolled (object sender, ScrollEventArgs se)
1108 int old_first_visiblerow = first_visiblerow;
1109 first_visiblerow = se.NewValue;
1110 grid_drawing.UpdateVisibleRowCount ();
1112 if (first_visiblerow == old_first_visiblerow) {
1116 ScrollToRow (old_first_visiblerow, first_visiblerow);
1119 public HitTestInfo HitTest (Point position)
1121 return HitTest (position.X, position.Y);
1124 public HitTestInfo HitTest (int x, int y)
1126 return grid_drawing.HitTest (x, y);
1130 public bool IsExpanded (int rowNumber)
1135 public bool IsSelected (int row)
1137 return selected_rows[row] != null;
1141 public void NavigateBack ()
1147 public void NavigateTo (int rowNumber, string relationName)
1152 protected virtual void OnAllowNavigationChanged (EventArgs e)
1154 if (AllowNavigationChanged != null) {
1155 AllowNavigationChanged (this, e);
1159 protected void OnBackButtonClicked (object sender, EventArgs e)
1161 if (BackButtonClick != null) {
1162 BackButtonClick (sender, e);
1166 protected override void OnBackColorChanged (EventArgs e)
1168 base.OnBackColorChanged (e);
1171 protected virtual void OnBackgroundColorChanged (EventArgs e)
1173 if (BackgroundColorChanged != null) {
1174 BackgroundColorChanged (this, e);
1178 protected override void OnBindingContextChanged( EventArgs e)
1180 base.OnBindingContextChanged (e);
1183 protected virtual void OnBorderStyleChanged (EventArgs e)
1185 if (BorderStyleChanged != null) {
1186 BorderStyleChanged (this, e);
1190 protected virtual void OnCaptionVisibleChanged (EventArgs e)
1192 if (CaptionVisibleChanged != null) {
1193 CaptionVisibleChanged (this, e);
1197 protected virtual void OnCurrentCellChanged (EventArgs e)
1199 if (CurrentCellChanged != null) {
1200 CurrentCellChanged (this, e);
1204 protected virtual void OnDataSourceChanged (EventArgs e)
1206 if (DataSourceChanged != null) {
1207 DataSourceChanged (this, e);
1211 protected override void OnEnter (EventArgs e)
1216 protected virtual void OnFlatModeChanged (EventArgs e)
1218 if (FlatModeChanged != null) {
1219 FlatModeChanged (this, e);
1223 protected override void OnFontChanged (EventArgs e)
1225 grid_drawing.CalcGridAreas ();
1226 base.OnFontChanged (e);
1229 protected override void OnForeColorChanged (EventArgs e)
1231 base.OnForeColorChanged (e);
1234 protected override void OnHandleCreated (EventArgs e)
1236 base.OnHandleCreated (e);
1237 grid_drawing.CalcGridAreas ();
1240 protected override void OnHandleDestroyed (EventArgs e)
1242 base.OnHandleDestroyed (e);
1245 protected override void OnKeyDown (KeyEventArgs ke)
1247 base.OnKeyDown (ke);
1249 if (ProcessGridKey (ke) == true) {
1253 CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].OnKeyDown
1254 (ke, current_cell.RowNumber, current_cell.ColumnNumber);
1257 protected override void OnKeyPress (KeyPressEventArgs kpe)
1259 base.OnKeyPress (kpe);
1262 protected override void OnLayout (LayoutEventArgs levent)
1264 base.OnLayout (levent);
1267 protected override void OnLeave (EventArgs e)
1272 protected override void OnMouseDown (MouseEventArgs e)
1274 base.OnMouseDown (e);
1276 HitTestInfo testinfo;
1277 testinfo = grid_drawing.HitTest (e.X, e.Y);
1279 switch (testinfo.type) {
1280 case HitTestType.Cell:
1282 DataGridCell new_cell = new DataGridCell (testinfo.Row, testinfo.Column);
1284 if (new_cell.Equals (current_cell) == false) {
1286 CurrentCell = new_cell;
1287 EditCell (current_cell);
1290 CurrentTableStyle.GridColumnStyles[testinfo.Column].OnMouseDown (e, testinfo.Row, testinfo.Column);
1295 case HitTestType.RowHeader:
1297 if (ctrl_pressed == false && shift_pressed == false) {
1298 ResetSelection (); // Invalidates selected rows
1301 if (shift_pressed == true) {
1302 ShiftSelection (testinfo.Row);
1303 } else { // ctrl_pressed or single item
1304 Select (testinfo.Row);
1308 CurrentCell = new DataGridCell (testinfo.Row, current_cell.ColumnNumber);
1309 OnRowHeaderClick (EventArgs.Empty);
1317 protected override void OnMouseLeave (EventArgs e)
1319 base.OnMouseLeave (e);
1322 protected override void OnMouseMove (MouseEventArgs e)
1324 base.OnMouseMove (e);
1327 protected override void OnMouseUp (MouseEventArgs e)
1332 protected override void OnMouseWheel (MouseEventArgs e)
1334 base.OnMouseWheel (e);
1337 if (current_cell.RowNumber > 0) {
1338 CurrentCell = new DataGridCell (current_cell.RowNumber - 1, current_cell.ColumnNumber);
1342 if (current_cell.RowNumber < RowsCount - 1) {
1343 CurrentCell = new DataGridCell (current_cell.RowNumber + 1, current_cell.ColumnNumber);
1348 protected void OnNavigate (NavigateEventArgs e)
1350 if (Navigate != null) {
1355 protected override void OnPaint (PaintEventArgs pe)
1357 grid_drawing.OnPaint (pe);
1360 protected override void OnPaintBackground (PaintEventArgs ebe)
1362 base.OnPaintBackground (ebe);
1365 protected virtual void OnParentRowsLabelStyleChanged (EventArgs e)
1367 if (ParentRowsLabelStyleChanged != null) {
1368 ParentRowsLabelStyleChanged (this, e);
1372 protected virtual void OnParentRowsVisibleChanged (EventArgs e)
1374 if (ParentRowsVisibleChanged != null) {
1375 ParentRowsVisibleChanged (this, e);
1379 protected virtual void OnReadOnlyChanged (EventArgs e)
1381 if (ReadOnlyChanged != null) {
1382 ReadOnlyChanged (this, e);
1386 protected override void OnResize (EventArgs e)
1391 protected void OnRowHeaderClick (EventArgs e)
1393 if (RowHeaderClick != null) {
1394 RowHeaderClick (this, e);
1398 protected void OnScroll (EventArgs e)
1400 if (Scroll != null) {
1405 protected void OnShowParentDetailsButtonClicked (object sender, EventArgs e)
1407 if (ShowParentDetailsButtonClick != null) {
1408 ShowParentDetailsButtonClick (sender, e);
1412 protected override bool ProcessDialogKey (Keys keyData)
1414 return base.ProcessDialogKey (keyData);
1417 protected bool ProcessGridKey (KeyEventArgs ke)
1419 if (RowsCount == 0) {
1423 switch (ke.KeyCode) {
1424 case Keys.ControlKey:
1425 ctrl_pressed = true;
1428 shift_pressed = true;
1432 if (current_cell.RowNumber > 0) {
1433 CurrentCell = new DataGridCell (current_cell.RowNumber - 1, current_cell.ColumnNumber);
1434 EditCell (current_cell);
1440 if (current_cell.RowNumber < RowsCount - 1) {
1441 CurrentCell = new DataGridCell (current_cell.RowNumber + 1, current_cell.ColumnNumber);
1442 EditCell (current_cell);
1448 if (current_cell.ColumnNumber + 1 < CurrentTableStyle.GridColumnStyles.Count) {
1449 CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber + 1);
1450 EditCell (current_cell);
1456 if (current_cell.ColumnNumber > 0) {
1457 CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber - 1);
1458 EditCell (current_cell);
1464 if (current_cell.RowNumber > grid_drawing.VLargeChange) {
1465 CurrentCell = new DataGridCell (current_cell.RowNumber - grid_drawing.VLargeChange, current_cell.ColumnNumber);
1467 CurrentCell = new DataGridCell (0, current_cell.ColumnNumber);
1470 EditCell (current_cell);
1475 if (current_cell.RowNumber + grid_drawing.VLargeChange < RowsCount) {
1476 CurrentCell = new DataGridCell (current_cell.RowNumber + grid_drawing.VLargeChange, current_cell.ColumnNumber);
1478 CurrentCell = new DataGridCell (RowsCount - 1, current_cell.ColumnNumber);
1481 EditCell (current_cell);
1486 CurrentCell = new DataGridCell (0, current_cell.ColumnNumber);
1487 EditCell (current_cell);
1492 CurrentCell = new DataGridCell (RowsCount - 1, current_cell.ColumnNumber);
1493 EditCell (current_cell);
1498 foreach (int row in selected_rows.Keys) {
1499 ListManager.RemoveAt (row);
1501 selected_rows.Clear ();
1502 CalcAreasAndInvalidate ();
1506 return false; // message not processed
1509 return true; // message processed
1512 // Called from DataGridTextBox
1513 protected override bool ProcessKeyPreview (ref Message m)
1515 Keys key = (Keys) m.WParam.ToInt32 ();
1516 KeyEventArgs ke = new KeyEventArgs (key);
1517 if (ProcessGridKey (ke) == true) {
1521 return base.ProcessKeyPreview (ref m);
1525 protected bool ProcessTabKey (Keys keyData)
1530 public void ResetAlternatingBackColor ()
1532 alternating_backcolor = def_alternating_backcolor;
1535 public override void ResetBackColor ()
1537 background_color = def_background_color;
1540 public override void ResetForeColor ()
1542 base.ResetForeColor ();
1545 public void ResetGridLineColor ()
1547 gridline_color = def_gridline_color;
1550 public void ResetHeaderBackColor ()
1552 header_backcolor = def_header_backcolor;
1555 public void ResetHeaderFont ()
1557 header_font = def_header_font;
1560 public void ResetHeaderForeColor ()
1562 header_forecolor = def_header_forecolor;
1565 public void ResetLinkColor ()
1567 link_color = def_link_color;
1570 public void ResetLinkHoverColor ()
1572 link_hovercolor = def_link_hovercolor;
1575 protected void ResetSelection ()
1577 foreach (int row in selected_rows.Keys) {
1578 grid_drawing.InvalidateRow (row);
1579 grid_drawing.InvalidateRowHeader (row);
1582 selected_rows.Clear ();
1585 public void ResetSelectionBackColor ()
1587 selection_backcolor = def_selection_backcolor;
1590 public void ResetSelectionForeColor ()
1592 selection_forecolor = def_selection_forecolor;
1595 public void Select (int row)
1597 if (selected_rows[row] == null) {
1598 selected_rows.Add (row, true);
1600 selected_rows[row] = true;
1603 grid_drawing.InvalidateRow (row);
1606 public void SetDataBinding (object dataSource, string dataMember)
1608 if (SetDataSource (dataSource) == false && SetDataMember (dataMember) == false) {
1612 SetNewDataSource ();
1615 protected virtual bool ShouldSerializeAlternatingBackColor ()
1617 return (alternating_backcolor != def_alternating_backcolor);
1620 protected virtual bool ShouldSerializeBackgroundColor ()
1622 return (background_color != def_background_color);
1625 protected virtual bool ShouldSerializeCaptionBackColor ()
1627 return (caption_backcolor != def_caption_backcolor);
1630 protected virtual bool ShouldSerializeCaptionForeColor ()
1632 return (caption_forecolor != def_caption_forecolor);
1635 protected virtual bool ShouldSerializeGridLineColor ()
1637 return (gridline_color != def_gridline_color);
1640 protected virtual bool ShouldSerializeHeaderBackColor ()
1642 return (header_backcolor != def_header_backcolor);
1645 protected bool ShouldSerializeHeaderFont ()
1647 return (header_font != def_header_font);
1650 protected virtual bool ShouldSerializeHeaderForeColor ()
1652 return (header_forecolor != def_header_forecolor);
1655 protected virtual bool ShouldSerializeLinkHoverColor ()
1657 return (link_hovercolor != def_link_hovercolor);
1660 protected virtual bool ShouldSerializeParentRowsBackColor ()
1662 return (parentrowsback_color != def_parentrowsback_color);
1665 protected virtual bool ShouldSerializeParentRowsForeColor ()
1667 return (parentrowsback_color != def_parentrowsback_color);
1670 protected bool ShouldSerializePreferredRowHeight ()
1672 return (preferredrow_height != def_preferredrow_height);
1675 protected bool ShouldSerializeSelectionBackColor ()
1677 return (selection_backcolor != def_selection_backcolor);
1680 protected virtual bool ShouldSerializeSelectionForeColor ()
1682 return (selection_forecolor != def_selection_forecolor);
1685 public void SubObjectsSiteChange (bool site)
1690 public void UnSelect (int row)
1692 selected_rows.Remove (row);
1693 grid_drawing.InvalidateRow (row);
1696 #endregion // Public Instance Methods
1698 #region Private Instance Methods
1700 internal void CalcAreasAndInvalidate ()
1702 grid_drawing.CalcGridAreas ();
1706 // EndEdit current editing operation
1707 public virtual bool EndEdit (bool shouldAbort)
1709 return EndEdit (CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber],
1710 current_cell.RowNumber, shouldAbort);
1713 private void EnsureCellVisilibility (DataGridCell cell)
1715 if (current_cell.RowNumber != cell.RowNumber) {
1716 grid_drawing.InvalidateRowHeader (current_cell.RowNumber);
1717 grid_drawing.InvalidateRowHeader (cell.RowNumber);
1720 if (cell.ColumnNumber < first_visiblecolumn ||
1721 cell.ColumnNumber >= first_visiblecolumn + visiblecolumn_count) {
1725 if (cell.ColumnNumber + 1 >= first_visiblecolumn + visiblecolumn_count) {
1726 col = 1 + cell.ColumnNumber - visiblecolumn_count;
1728 col = cell.RowNumber;
1731 pixel = grid_drawing.GetColumnStartingPixel (col);
1732 ScrollToColumnInPixels (pixel);
1735 if (cell.RowNumber < first_visiblerow ||
1736 cell.RowNumber + 1 >= first_visiblerow + visiblerow_count) {
1738 if (cell.RowNumber + 1 >= first_visiblerow + visiblerow_count) {
1739 int old_first_visiblerow = first_visiblerow;
1740 first_visiblerow = 1 + cell.RowNumber - visiblerow_count;
1741 grid_drawing.UpdateVisibleRowCount ();
1742 ScrollToRow (old_first_visiblerow, first_visiblerow);
1744 int old_first_visiblerow = first_visiblerow;
1745 first_visiblerow = cell.RowNumber;
1746 grid_drawing.UpdateVisibleRowCount ();
1747 ScrollToRow (old_first_visiblerow, first_visiblerow);
1750 vert_scrollbar.Value = first_visiblerow;
1754 internal void InvalidateCurrentRowHeader ()
1756 grid_drawing.InvalidateRowHeader (current_cell.RowNumber);
1759 private bool SetDataMember (string member)
1761 if (member == datamember) {
1765 datamember = member;
1766 real_datasource = DataSourceHelper.GetResolvedDataSource (datasource, member);
1767 cached_currencymgr = null;
1771 private bool SetDataSource (object source)
1773 if (datasource != null && datasource.Equals (source)) {
1777 if (source != null && source as IListSource != null && source as IList != null) {
1778 throw new Exception ("Wrong complex data binding source");
1781 datasource = source;
1782 cached_currencymgr = null;
1784 real_datasource = DataSourceHelper.GetResolvedDataSource (datasource, DataMember);
1785 }catch (Exception e) {
1787 real_datasource = source;
1790 OnDataSourceChanged (EventArgs.Empty);
1794 private void SetNewDataSource ()
1796 // Create Table Style
1797 // Create columns Styles
1799 current_style.GridColumnStyles.Clear ();
1800 current_style.CreateColumnsForTable (false);
1801 CalcAreasAndInvalidate ();
1804 private void OnKeyUpDG (object sender, KeyEventArgs e)
1806 switch (e.KeyCode) {
1807 case Keys.ControlKey:
1808 ctrl_pressed = false;
1811 shift_pressed = false;
1818 private void OnTableStylesCollectionChanged (object sender, CollectionChangeEventArgs e)
1820 if (String.Compare (ListManager.ListName, ((DataGridTableStyle)e.Element).MappingName, true) == 0) {
1821 CurrentTableStyle = (DataGridTableStyle)e.Element;
1822 ((DataGridTableStyle) e.Element).CreateColumnsForTable (false);
1825 CalcAreasAndInvalidate ();
1828 private void EditCell (DataGridCell cell)
1830 ResetSelection (); // Invalidates selected rows
1832 is_changing = false;
1834 if (ShowEditRow && cell.RowNumber >= RowsCount) {
1835 ListManager.AddNew ();
1837 Invalidate (); // We have just added a new row
1840 CurrentTableStyle.GridColumnStyles[cell.ColumnNumber].Edit (ListManager,
1841 cell.RowNumber, GetCellBounds (cell.RowNumber, cell.ColumnNumber),
1842 _readonly, string.Empty, true);
1845 private void ShiftSelection (int index)
1847 int shorter_item = -1, dist = RowsCount + 1, cur_dist;
1849 foreach (int row in selected_rows.Keys) {
1852 cur_dist = row - index;
1855 cur_dist = index - row;
1858 if (cur_dist < dist) {
1864 if (shorter_item != -1) {
1867 if (shorter_item > index) {
1871 start = shorter_item;
1876 for (int idx = start; idx <= end; idx++) {
1882 private void ScrollToColumnInPixels (int pixel)
1884 Rectangle invalidate = new Rectangle ();
1885 Rectangle invalidate_column = new Rectangle ();
1887 if (pixel > horz_pixeloffset) { // ScrollRight
1888 int pixels = pixel - horz_pixeloffset;
1891 invalidate_column.X = grid_drawing.ColumnsHeadersArea.X + grid_drawing.ColumnsHeadersArea.Width - pixels;
1892 invalidate_column.Y = grid_drawing.ColumnsHeadersArea.Y;
1893 invalidate_column.Width = pixels;
1894 invalidate_column.Height = grid_drawing.ColumnsHeadersArea.Height;
1895 XplatUI.ScrollWindow (Handle, grid_drawing.ColumnsHeadersArea, -pixels, 0, false);
1898 invalidate.X = grid_drawing.CellsArea.X + grid_drawing.CellsArea.Width - pixels;
1899 invalidate.Y = grid_drawing.CellsArea.Y;
1900 invalidate.Width = pixels;
1901 invalidate.Height = grid_drawing.CellsArea.Height;
1902 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, -pixels, 0, false);
1905 int pixels = horz_pixeloffset - pixel;
1908 invalidate_column.X = grid_drawing.ColumnsHeadersArea.X;
1909 invalidate_column.Y = grid_drawing.ColumnsHeadersArea.Y;
1910 invalidate_column.Width = pixels;
1911 invalidate_column.Height = grid_drawing.ColumnsHeadersArea.Height;
1912 XplatUI.ScrollWindow (Handle, grid_drawing.ColumnsHeadersArea, pixels, 0, false);
1915 invalidate.X = grid_drawing.CellsArea.X;
1916 invalidate.Y = grid_drawing.CellsArea.Y;
1917 invalidate.Width = pixels;
1918 invalidate.Height = grid_drawing.CellsArea.Height;
1919 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, pixels, 0, false);
1922 horz_pixeloffset = horiz_scrollbar.Value = pixel;
1923 grid_drawing.UpdateVisibleColumn ();
1924 Invalidate (invalidate_column);
1925 Invalidate (invalidate);
1928 private void ScrollToRow (int old_row, int new_row)
1930 Rectangle invalidate = new Rectangle ();
1932 if (new_row > old_row) { // Scrolldown
1933 int scrolled_rows = new_row - old_row;
1934 int pixels = scrolled_rows * RowHeight;
1936 invalidate.X = grid_drawing.CellsArea.X;
1937 invalidate.Y = grid_drawing.CellsArea.Y + grid_drawing.CellsArea.Height - pixels;
1938 invalidate.Width = grid_drawing.CellsArea.Width;
1939 invalidate.Height = pixels;
1941 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, 0, -pixels, false);
1943 } else { // ScrollUp
1944 int scrolled_rows = old_row - new_row;
1945 int pixels = scrolled_rows * RowHeight;
1947 invalidate.X = grid_drawing.CellsArea.X;
1948 invalidate.Y = grid_drawing.CellsArea.Y;
1949 invalidate.Width = grid_drawing.CellsArea.Width;
1950 invalidate.Height = pixels;
1951 XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, 0, pixels, false);
1954 // Right now we use ScrollWindow Invalidate, let's leave remarked it here for X11 if need it
1956 Invalidate (grid_drawing.RowsHeadersArea);
1959 #endregion Private Instance Methods
1963 public event EventHandler AllowNavigationChanged;
1964 public event EventHandler BackButtonClick;
1965 public event EventHandler BackgroundColorChanged;
1968 [EditorBrowsable(EditorBrowsableState.Never)]
1969 public new event EventHandler BackgroundImageChanged;
1971 public event EventHandler BorderStyleChanged;
1972 public event EventHandler CaptionVisibleChanged;
1973 public event EventHandler CurrentCellChanged;
1976 [EditorBrowsable(EditorBrowsableState.Never)]
1977 public new event EventHandler CursorChanged;
1979 public event EventHandler DataSourceChanged;
1980 public event EventHandler FlatModeChanged;
1981 public event NavigateEventHandler Navigate;
1982 public event EventHandler ParentRowsLabelStyleChanged;
1983 public event EventHandler ParentRowsVisibleChanged;
1984 public event EventHandler ReadOnlyChanged;
1985 protected event EventHandler RowHeaderClick;
1986 public event EventHandler Scroll;
1987 public event EventHandler ShowParentDetailsButtonClick;
1990 [EditorBrowsable(EditorBrowsableState.Never)]
1991 public new event EventHandler TextChanged;
1992 #endregion // Events