2 // System.Windows.Forms.DataGrid
5 // stubbed out by Jaak Simm (jaaksimm@firm.ee)
6 // Dennis Hayes (dennish@Raytek.com)
7 // Modified for System.Window.Forms/Gtk by :
9 // (C) Ximian, Inc., 2002/3
13 using System
.ComponentModel
;
14 using System
.Collections
;
15 using System
.Reflection
;
17 using System
.Runtime
.InteropServices
;
22 namespace System
.Windows
.Forms
{
25 /// Displays ADO.NET data in a scrollable grid.
30 public class DataGrid
: Control
, ISupportInitialize
{
32 TreeView treeView
= null;
33 DataGridColumn
[] gridColumns
= null;
34 ListStore store
= null;
39 Color alternatingBackColor
;
40 Color backgroundColor
;
41 BorderStyle borderStyle
;
42 Color captionBackColor
;
44 Color captionForeColor
;
47 bool columnHeadersVisible
;
51 DataGridLineStyle gridLineStyle
;
52 Color headerBackColor
;
53 Color headerForeColor
;
56 Color parentRowsBackColor
;
57 Color parentRowsForeColor
;
58 DataGridParentRowsLabelStyle parentRowsLabelStyle
;
59 bool parentRowsVisible
;
62 Color selectionBackColor
;
63 Color selectionForeColor
;
70 // setting default values:
72 borderStyle
= BorderStyle
.FixedSingle
;
73 captionBackColor
= SystemColors
.ActiveCaption
;
74 captionForeColor
= SystemColors
.ActiveCaptionText
;
76 captionVisible
= true;
77 columnHeadersVisible
= true;
80 gridLineColor
= SystemColors
.Control
;
81 gridLineStyle
= DataGridLineStyle
.Solid
;
82 headerBackColor
= SystemColors
.Control
;
83 headerForeColor
= SystemColors
.ControlText
;
84 linkColor
= SystemColors
.HotTrack
;
85 linkHoverColor
= SystemColors
.HotTrack
;
86 parentRowsBackColor
= SystemColors
.Control
;
87 parentRowsForeColor
= SystemColors
.WindowText
;
88 parentRowsLabelStyle
= DataGridParentRowsLabelStyle
.Both
;
89 parentRowsVisible
= true;
92 selectionBackColor
= SystemColors
.ActiveCaption
;
93 selectionForeColor
= SystemColors
.ActiveCaptionText
;
96 internal class DataGridColumn
98 internal string columnName
= "";
99 internal TreeViewColumn treeViewColumn
= null;
101 internal string ColumnName
{
110 internal TreeViewColumn TreeViewColumn
{
112 return treeViewColumn
;
115 treeViewColumn
= value;
120 internal override Gtk
.Widget
CreateWidget () {
122 //VBox vb = new VBox(false, 4);
124 ScrolledWindow sw
= new ScrolledWindow ();
125 //vb.PackStart (sw, true, true, 0);
127 treeView
= new TreeView (store
);
128 treeView
.HeadersVisible
= CaptionVisible
;
137 protected override void Dispose(bool disposing
){
142 public bool AllowNavigation
{
144 return allowNavigation
;
147 allowNavigation
=value;
151 public bool AllowSorting
{
160 public Color AlternatingBackColor
{
162 return alternatingBackColor
;
165 alternatingBackColor
=value;
170 public override Color BackColor
{
172 throw new NotImplementedException ();
179 public Color BackgroundColor
{
181 return backgroundColor
;
184 backgroundColor
=value;
189 public override System
.Drawing
.Image BackgroundImage
{
192 return base.BackgroundImage
;
196 base.BackgroundImage
= value;
200 public BorderStyle BorderStyle
{
209 public Color CaptionBackColor
{
211 return captionBackColor
;
214 captionBackColor
=value;
218 public Font CaptionFont
{
227 public Color CaptionForeColor
{
229 return captionForeColor
;
232 captionForeColor
=value;
236 public string CaptionText
{
245 public bool CaptionVisible
{
247 return captionVisible
;
250 captionVisible
=value;
254 public bool ColumnHeadersVisible
{
256 return columnHeadersVisible
;
259 columnHeadersVisible
=value;
264 public DataGridCell CurrentCell
{
266 throw new NotImplementedException ();
274 public int CurrentRowIndex
{
276 throw new NotImplementedException ();
284 //public override Cursor Cursor {
286 // throw new NotImplementedException ();
293 public string DataMember
{
294 get { return dataMember; }
295 set { dataMember=value; }
299 public object DataSource
{
301 throw new NotImplementedException ();
308 protected override Size DefaultSize
{
310 //FIXME: verify numbers
311 return new Size(300,200);
316 public int FirstVisibleColumn
{
317 get { throw new NotImplementedException (); }
320 public bool FlatMode
{
321 get { return flatMode; }
322 set { flatMode=value; }
326 public override Color ForeColor
{
329 return base.ForeColor
;
333 base.ForeColor
= value;
337 public Color GridLineColor
{
338 get { return gridLineColor; }
339 set { gridLineColor=value; }
342 public DataGridLineStyle GridLineStyle
{
343 get { return gridLineStyle; }
344 set { gridLineStyle=value; }
347 public Color HeaderBackColor
{
348 get { return headerBackColor; }
350 if (value==Color
.Empty
) throw new ArgumentNullException();
351 headerBackColor
=value;
355 public Color HeaderForeColor
{
356 get { return headerForeColor; }
357 set { headerForeColor=value; }
361 protected ScrollBar HorizScrollBar
{
362 get { throw new NotImplementedException (); }
366 public object this[DataGridCell cell
] {
367 get { throw new NotImplementedException (); }
368 set { throw new NotImplementedException (); }
372 public object this[int rowIndex
,int columnIndex
] {
373 get { throw new NotImplementedException (); }
374 set { throw new NotImplementedException (); }
377 public Color LinkColor
{
378 get { return linkColor; }
379 set { linkColor=value; }
382 public Color LinkHoverColor
{
383 get { return linkHoverColor; }
384 set { linkHoverColor=value; }
388 protected internal CurrencyManager ListManager
{
389 get { throw new NotImplementedException (); }
390 set { throw new NotImplementedException (); }
393 public Color ParentRowsBackColor
{
394 get { return parentRowsBackColor; }
395 set { parentRowsBackColor=value; }
398 public Color ParentRowsForeColor
{
399 get { return parentRowsForeColor; }
400 set { parentRowsForeColor=value; }
403 public DataGridParentRowsLabelStyle ParentRowsLabelStyle
{
404 get { return parentRowsLabelStyle; }
405 set { parentRowsLabelStyle=value; }
408 public bool ParentRowsVisible
{
409 get { return parentRowsVisible; }
410 set { parentRowsVisible=value; }
414 public int PreferredColumnWidth
{
415 get { throw new NotImplementedException (); }
416 set { throw new NotImplementedException (); }
420 public int PreferredRowHeight
{
421 get { throw new NotImplementedException (); }
422 set { throw new NotImplementedException (); }
425 public bool ReadOnly
{
426 get { return readOnly; }
427 set { readOnly=value; }
431 public bool RowHeadersVisible
{
432 get { throw new NotImplementedException (); }
433 set { throw new NotImplementedException (); }
437 public int RowHeaderWidth
{
438 get { return rowHeaderWidth; }
439 set { rowHeaderWidth=value; }
442 public Color SelectionBackColor
{
443 get { return selectionBackColor; }
444 set { selectionBackColor=value; }
447 public Color SelectionForeColor
{
448 get { return selectionForeColor; }
449 set { selectionForeColor=value; }
453 public override ISite Site
{
454 get { throw new NotImplementedException (); }
455 set { throw new NotImplementedException (); }
459 public GridTableStylesCollection TableStyles
{
460 get { throw new NotImplementedException (); }
464 public override string Text
{
476 protected ScrollBar VertScrollBar
{
477 get { throw new NotImplementedException (); }
481 public int VisibleColumnCount
{
482 get { throw new NotImplementedException (); }
486 public int VisibleRowCount
{
487 get { throw new NotImplementedException (); }
492 /* Following members support the .NET Framework infrastructure and are not intended to be used directly from your code.
493 * Methods not stubbed out:
494 - protected virtual string GetOutputTextDelimiter()
495 - public void ResetLinkHoverColor()
496 - public void SubObjectsSiteChange(bool site)
499 public virtual bool BeginEdit(DataGridColumnStyle gridColumn
,int rowNumber
)
501 throw new NotImplementedException ();
505 public void BeginInit()
511 protected virtual void CancelEditing()
517 public void Collapse(int row
)
523 protected internal virtual void ColumnStartedEditing(Control editingControl
)
529 protected internal virtual void ColumnStartedEditing(Rectangle bounds
)
535 protected override AccessibleObject
CreateAccessibilityInstance()
538 return base.CreateAccessibilityInstance();
542 protected virtual DataGridColumnStyle
CreateGridColumn(PropertyDescriptor prop
)
544 throw new NotImplementedException ();
548 protected virtual DataGridColumnStyle
CreateGridColumn(PropertyDescriptor prop
,bool isDefault
)
550 throw new NotImplementedException ();
554 public virtual bool EndEdit(DataGridColumnStyle gridColumn
,int rowNumber
,bool shouldAbort
)
556 throw new NotImplementedException ();
560 public void EndInit()
566 public void Expand(int row
)
568 throw new NotImplementedException ();
572 public Rectangle
GetCellBounds(DataGridCell dgc
)
574 throw new NotImplementedException ();
578 public Rectangle
GetCellBounds(int row
,int col
)
580 throw new NotImplementedException ();
584 public Rectangle
GetCurrentCellBounds()
586 throw new NotImplementedException ();
590 protected virtual void GridHScrolled(object sender
,ScrollEventArgs se
)
596 protected virtual void GridVScrolled(object sender
,ScrollEventArgs se
)
602 public HitTestInfo
HitTest(Point position
)
604 throw new NotImplementedException ();
608 public HitTestInfo
HitTest(int x
,int y
)
610 throw new NotImplementedException ();
614 public bool IsExpanded(int rowNumber
)
616 throw new NotImplementedException ();
620 public bool IsSelected(int row
)
622 throw new NotImplementedException ();
626 public void NavigateBack()
632 public void NavigateTo(int rowNumber
,string relationName
)
638 protected virtual void OnAllowNavigationChanged(EventArgs e
)
644 protected void OnBackButtonClicked(object sender
,EventArgs e
)
650 protected override void OnBackColorChanged(EventArgs e
)
653 base.OnBackColorChanged(e
);
657 protected virtual void OnBackgroundColorChanged(EventArgs e
)
663 protected override void OnBindingContextChanged(EventArgs e
)
666 base.OnBindingContextChanged(e
);
670 protected virtual void OnBorderStyleChanged(EventArgs e
)
676 protected virtual void OnCaptionVisibleChanged(EventArgs e
)
682 protected virtual void OnCurrentCellChanged(EventArgs e
)
688 protected virtual void OnDataSourceChanged(EventArgs e
)
694 protected override void OnEnter(EventArgs e
)
701 protected virtual void OnFlatModeChanged(EventArgs e
)
707 protected override void OnFontChanged(EventArgs e
)
710 base.OnFontChanged(e
);
714 protected override void OnForeColorChanged(EventArgs e
)
717 base.OnForeColorChanged(e
);
721 protected override void OnHandleCreated(EventArgs e
)
724 base.OnHandleCreated(e
);
728 protected override void OnHandleDestroyed(EventArgs e
)
731 base.OnHandleDestroyed(e
);
735 protected override void OnKeyDown(KeyEventArgs e
)
742 protected override void OnKeyPress(KeyPressEventArgs kpe
)
745 base.OnKeyPress(kpe
);
749 protected override void OnLayout(LayoutEventArgs levent
)
752 base.OnLayout(levent
);
756 protected override void OnLeave(EventArgs e
)
763 protected override void OnMouseDown(MouseEventArgs e
)
770 protected override void OnMouseLeave(EventArgs e
)
773 base.OnMouseLeave(e
);
777 protected override void OnMouseMove(MouseEventArgs e
)
784 protected override void OnMouseUp(MouseEventArgs e
)
791 protected override void OnMouseWheel(MouseEventArgs e
)
794 base.OnMouseWheel(e
);
798 protected void OnNavigate(NavigateEventArgs e
)
804 protected override void OnPaint(PaintEventArgs pe
)
811 protected override void OnPaintBackground(PaintEventArgs ebe
)
814 base.OnPaintBackground(ebe
);
818 protected virtual void OnParentRowsLabelStyleChanged(EventArgs e
)
824 protected virtual void OnParentRowsVisibleChanged(EventArgs e
)
832 protected virtual void OnReadOnlyChanged(EventArgs e
)
837 protected void OnRowHeaderClick(EventArgs e
)
843 protected void OnScroll(EventArgs e
)
849 protected void OnShowParentDetailsButtonClicked(object sender
,EventArgs e
)
855 protected override bool ProcessDialogKey(Keys keyData
)
858 return base.ProcessDialogKey(keyData
);
862 protected bool ProcessGridKey(KeyEventArgs ke
)
864 throw new NotImplementedException ();
868 //protected override bool ProcessKeyPreview(ref Message m)
871 //return base.ProcessKeyPreview(ref m);
875 protected bool ProcessTabKey(Keys keyData
)
877 throw new NotImplementedException ();
881 public void ResetAlternatingBackColor()
887 public override void ResetBackColor()
890 base.ResetBackColor();
894 public override void ResetForeColor()
897 base.ResetForeColor();
901 public void ResetGridLineColor()
907 public void ResetHeaderBackColor()
913 public void ResetHeaderFont()
919 public void ResetHeaderForeColor()
925 public void ResetLinkColor()
931 protected void ResetSelection()
937 public void ResetSelectionBackColor()
943 public void ResetSelectionForeColor()
949 public void Select(int row
)
954 // start of Gtk# stuff
956 public void SetDataBinding(object mydataSource
, string mydataMember
)
959 System
.Object o
= null;
960 o
= GetResolvedDataSource (mydataSource
, mydataMember
);
961 IEnumerable ie
= (IEnumerable
) o
;
962 ITypedList tlist
= (ITypedList
) o
;
964 // FIXME: does not belong in this base method
965 TreeIter iter
= new TreeIter ();
967 PropertyDescriptorCollection pdc
= tlist
.GetItemProperties (new PropertyDescriptor
[0]);
969 // FIXME: does not belong in this base method
970 gridColumns
= new DataGridColumn
[pdc
.Count
];
972 // FIXME: does not belong in base method
973 // define the columns in the treeview store
974 // based on the schema of the result
975 GLib
.GType
[] theTypes
= new GLib
.GType
[pdc
.Count
];
977 for (int col
= 0; col
< pdc
.Count
; col
++) {
978 theTypes
[col
] = GType
.String
;
980 store
.SetColumnTypes (theTypes
);
982 // FIXME: does not belong in this base method
984 foreach (PropertyDescriptor pd
in pdc
) {
986 gridColumns
[colndx
] = new DataGridColumn ();
987 gridColumns
[colndx
].ColumnName
= pd
.Name
;
990 foreach (System
.Object obj
in ie
) {
991 ICustomTypeDescriptor custom
= (ICustomTypeDescriptor
) obj
;
992 PropertyDescriptorCollection properties
;
993 properties
= custom
.GetProperties ();
997 foreach (PropertyDescriptor property
in properties
) {
998 object oPropValue
= property
.GetValue (obj
);
999 string sPropValue
= oPropValue
.ToString ();
1001 // FIXME: does not belong in this base method
1002 SetColumnValue (iter
, cv
, sPropValue
);
1008 // FIXME: does not belong in this base method
1009 treeView
.Model
= store
;
1010 AutoCreateTreeViewColumns (treeView
);
1013 private IEnumerable
GetResolvedDataSource(object source
, string member
)
1015 if (source
!= null && source
is IListSource
) {
1016 IListSource src
= (IListSource
) source
;
1017 IList list
= src
.GetList ();
1018 if (!src
.ContainsListCollection
) {
1021 if (list
!= null && list
is ITypedList
) {
1023 ITypedList tlist
= (ITypedList
) list
;
1024 PropertyDescriptorCollection pdc
= tlist
.GetItemProperties (new PropertyDescriptor
[0]);
1025 if (pdc
!= null && pdc
.Count
> 0) {
1026 PropertyDescriptor pd
= null;
1027 if (member
!= null && member
.Length
> 0) {
1028 pd
= pdc
.Find (member
, true);
1033 object rv
= pd
.GetValue (list
[0]);
1034 if (rv
!= null && rv
is IEnumerable
) {
1035 return (IEnumerable
)rv
;
1038 throw new Exception ("ListSource_Missing_DataMember");
1040 throw new Exception ("ListSource_Without_DataMembers");
1043 if (source
is IEnumerable
) {
1044 return (IEnumerable
)source
;
1049 private TreeIter
NewRow ()
1051 TreeIter rowTreeIter
= new TreeIter();
1052 rowTreeIter
= store
.Append ();
1056 private void AutoCreateTreeViewColumns (TreeView theTreeView
)
1058 for(int col
= 0; col
< gridColumns
.Length
; col
++) {
1059 // escape underscore _ because it is used
1060 // as the underline in menus and labels
1061 StringBuilder name
= new StringBuilder ();
1062 foreach (char ch
in gridColumns
[col
].ColumnName
) {
1069 tvc
= CreateColumn (theTreeView
, col
,
1071 theTreeView
.AppendColumn (tvc
);
1075 private void SetColumnValue (TreeIter iter
, int column
, string value)
1077 GLib
.Value cell
= new GLib
.Value (value);
1078 store
.SetValue (iter
, column
, cell
);
1081 public TreeViewColumn
CreateColumn (TreeView theTreeView
, int col
,
1084 TreeViewColumn NameCol
= new TreeViewColumn ();
1085 CellRenderer NameRenderer
= new CellRendererText ();
1087 NameCol
.Title
= columnName
;
1088 NameCol
.PackStart (NameRenderer
, true);
1089 NameCol
.AddAttribute (NameRenderer
, "text", col
);
1091 gridColumns
[col
].TreeViewColumn
= NameCol
;
1096 internal void Clear ()
1098 if (store
!= null) {
1101 store
= new ListStore (typeof (string));
1104 store
= new ListStore (typeof (string));
1106 if (gridColumns
!= null) {
1107 for (int c
= 0; c
< gridColumns
.Length
; c
++) {
1108 if (gridColumns
[c
] != null) {
1109 if (gridColumns
[c
].TreeViewColumn
!= null) {
1110 treeView
.RemoveColumn (gridColumns
[c
].TreeViewColumn
);
1111 gridColumns
[c
].TreeViewColumn
= null;
1113 gridColumns
[c
] = null;
1119 // end of gtk# stuff
1122 protected virtual bool ShouldSerializeAlternatingBackColor()
1124 throw new NotImplementedException ();
1128 protected virtual bool ShouldSerializeBackgroundColor()
1130 throw new NotImplementedException ();
1134 protected virtual bool ShouldSerializeCaptionBackColor()
1136 throw new NotImplementedException ();
1140 protected virtual bool ShouldSerializeCaptionForeColor()
1142 throw new NotImplementedException ();
1146 protected virtual bool ShouldSerializeGridLineColor()
1148 throw new NotImplementedException ();
1152 protected virtual bool ShouldSerializeHeaderBackColor()
1154 throw new NotImplementedException ();
1158 protected bool ShouldSerializeHeaderFont()
1160 throw new NotImplementedException ();
1164 protected virtual bool ShouldSerializeHeaderForeColor()
1166 throw new NotImplementedException ();
1170 protected virtual bool ShouldSerializeLinkHoverColor()
1172 throw new NotImplementedException ();
1176 protected virtual bool ShouldSerializeParentRowsBackColor()
1178 throw new NotImplementedException ();
1182 protected virtual bool ShouldSerializeParentRowsForeColor()
1184 throw new NotImplementedException ();
1188 protected bool ShouldSerializePreferredRowHeight()
1190 throw new NotImplementedException ();
1194 protected bool ShouldSerializeSelectionBackColor()
1196 throw new NotImplementedException ();
1200 protected virtual bool ShouldSerializeSelectionForeColor()
1202 throw new NotImplementedException ();
1206 public void UnSelect(int row
)
1213 public event EventHandler AllowNavigationChanged
;
1214 public event EventHandler BackButtonClick
;
1215 public event EventHandler BackgroundColorChanged
;
1216 public event EventHandler BorderStyleChanged
;
1217 public event EventHandler CaptionVisibleChanged
;
1218 public event EventHandler CurrentCellChanged
;
1219 public event EventHandler DataSourceChanged
;
1220 public event EventHandler FlatModeChanged
;
1221 public event NavigateEventHandler Navigate
;
1222 public event EventHandler ParentRowsLabelStyleChanged
;
1223 public event EventHandler ParentRowsVisibleChanged
;
1224 public event EventHandler ReadOnlyChanged
;
1225 protected event EventHandler RowHeaderClick
;
1226 public event EventHandler Scroll
;
1227 public event EventHandler ShowParentDetailsButtonClick
;
1230 /// sub-class: DataGrid.HitTestInfo
1232 /// Contains information about a part of the System.Windows.Forms.DataGrid at a specified coordinate. This class cannot be inherited.
1235 public sealed class HitTestInfo
{
1236 #region DataGrid.HitTestInfo: Fields
1238 public static readonly DataGrid
.HitTestInfo Nowhere
;
1241 #region DataGrid.HitTestInfo: Properties
1245 throw new NotImplementedException ();
1252 throw new NotImplementedException ();
1257 public HitTestType Type
{
1259 throw new NotImplementedException ();
1264 /// sub-enumeration: DataGrid.HitTestType
1266 /// Specifies the part of the System.Windows.Forms.DataGrid control the user has clicked
1270 public enum HitTestType
{