Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataGrid.cs « System.Windows.Forms « Managed.Windows.Forms « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 03d6d49d3d4625b7c691e3ebd9b93263f9a72930 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Copyright (c) 2005 Novell, Inc. (http://www.novell.com)
//
// Author:
//	Jordi Mas i Hernandez <jordi@ximian.com>
//
//

// NOT COMPLETE


using System;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Collections;

namespace System.Windows.Forms
{
	[DefaultEvent("Navigate")]
	[DefaultProperty("DataSource")]
	[Designer("System.Windows.Forms.Design.DataGridDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
	public class DataGrid : Control, ISupportInitialize, IDataGridEditingService
	{
		[Flags]
		[Serializable]
		public enum HitTestType
		{
			None		= 0,
			Cell		= 1,
			ColumnHeader	= 2,
			RowHeader	= 4,
			ColumnResize	= 8,
			RowResize	= 16,
			Caption		= 32,
			ParentRows	= 64
		}

		public sealed class HitTestInfo
		{
			public static readonly HitTestInfo Nowhere = null;

			#region	Local Variables
			internal int column;
			internal int row;
			internal HitTestType type;
			#endregion // Local Variables

			#region Private Constructors
			internal HitTestInfo ()
			{
				column = -1;
				row = -1;
				type =  HitTestType.None;
			}
			#endregion


			#region Public Instance Properties
			public int Column {
				get { return column; }
			}

			public int Row {
				get { return row; }
			}
			public DataGrid.HitTestType Type {
				get { return type; }
			}
			#endregion //Public Instance Properties

			public override bool Equals (object o)
			{
				if (!(o is HitTestInfo))
					return false;

				HitTestInfo obj = (HitTestInfo) o;
				return (obj.Column == column && obj.Row == row && obj.Type ==type);
			}

			public override int GetHashCode ()
			{
				return row ^ column;
			}

			public override string ToString ()
			{
				return "{ " + type + "," + row + "," + column + "}";
			}

		}

		#region	Local Variables
		private static readonly Color	def_alternating_backcolor = ThemeEngine.Current.DataGridAlternatingBackColor;
		private static readonly Color	def_background_color = ThemeEngine.Current.DataGridBackgroundColor;
		private static readonly Color	def_caption_backcolor = ThemeEngine.Current.DataGridCaptionBackColor;
		private static readonly Color	def_caption_forecolor = ThemeEngine.Current.DataGridCaptionForeColor;
		private static readonly Color	def_gridline_color = ThemeEngine.Current.DataGridGridLineColor;
		private static readonly Color	def_header_backcolor = ThemeEngine.Current.DataGridHeaderBackColor;
		private static readonly Font	def_header_font = ThemeEngine.Current.DefaultFont;
		private static readonly Color	def_header_forecolor = ThemeEngine.Current.DataGridHeaderForeColor;
		private static readonly Color	def_link_hovercolor = ThemeEngine.Current.DataGridLinkHoverColor;
		private static readonly Color	def_parentrowsback_color = ThemeEngine.Current.DataGridParentRowsBackColor;
		private static readonly Color	def_parentrowsfore_color = ThemeEngine.Current.DataGridParentRowsForeColor;
		private static readonly Color	def_selection_backcolor = ThemeEngine.Current.DataGridSelectionBackColor;
		private static readonly Color	def_selection_forecolor = ThemeEngine.Current.DataGridSelectionForeColor;
		private static readonly Color	def_link_color = ThemeEngine.Current.DataGridLinkColor;
		internal readonly int def_preferredrow_height;

		private bool allow_navigation;
		private bool allow_sorting;
		private Color alternating_backcolor;
		private Color backColor;
		private Color background_color;
		private Color caption_backcolor;
		private Font caption_font;
		private Color caption_forecolor;
		private string caption_text;
		internal bool caption_visible;
		internal bool columnheaders_visible;
		private object datasource;
		private object real_datasource;
		private string datamember;
		private int firstvisible_column;
		private bool flatmode;
		private Color gridline_color;
		private DataGridLineStyle gridline_style;
		private Color header_backcolor;
		private Color header_forecolor;
		private Font header_font;
		private Color link_color;
		private Color link_hovercolor;
		private Color parentrowsback_color;
		private Color parentrowsfore_color;
		private bool parentrows_visible;
		private int preferredcolumn_width;
		private int preferredrow_height;
		private bool _readonly;
		internal bool rowheaders_visible;
		private Color selection_backcolor;
		private Color selection_forecolor;
		private int rowheaders_width;
		internal int visiblecolumn_count;
		internal int visiblerow_count;
		internal int first_visiblecolumn;
		private GridTableStylesCollection styles_collection;
		private DataGridParentRowsLabelStyle parentrowslabel_style;
		internal DataGridCell current_cell;
		private DataGridTableStyle default_style;
		private DataGridTableStyle current_style;
		internal HScrollBar horiz_scrollbar;
		internal VScrollBar vert_scrollbar;
		internal DataGridDrawing grid_drawing;
		internal int first_visiblerow;
		internal int horz_pixeloffset;
		internal bool is_editing; 	// Current cell is edit mode
		internal bool is_changing;	// Indicates if current cell is been changed (in edit mode)
		internal bool is_adding;	// Indicates when we are adding a row
		private Hashtable selected_rows;
		private bool ctrl_pressed;
		private bool shift_pressed;
		private bool begininit;
		private CurrencyManager cached_currencymgr;
		private CurrencyManager cached_currencymgr_events;
		private bool accept_listmgrevents;
		#endregion // Local Variables

		#region Public Constructors
		public DataGrid ()
		{
			grid_drawing = new DataGridDrawing (this);
			allow_navigation = true;
			allow_sorting = true;
			begininit = false;
			backColor = ThemeEngine.Current.DataGridBackColor;
			alternating_backcolor = def_alternating_backcolor;
			background_color = def_background_color;
			border_style = BorderStyle.Fixed3D;
			caption_backcolor = def_caption_backcolor;
			caption_font = null;
			caption_forecolor = def_caption_forecolor;
			caption_text = string.Empty;
			caption_visible = true;
			columnheaders_visible = true;
			datasource = null;
			real_datasource = null;
			datamember = string.Empty;
			firstvisible_column = 0;
			flatmode = false;
			gridline_color = def_gridline_color;
			gridline_style = DataGridLineStyle.Solid;
			header_backcolor = def_header_backcolor;
			header_forecolor = def_header_forecolor;
			header_font = def_header_font;
			link_color = def_link_color;
			link_hovercolor = def_link_hovercolor;
			parentrowsback_color = def_parentrowsback_color;
			parentrowsfore_color = def_parentrowsfore_color;
			parentrows_visible = true;
			preferredcolumn_width = ThemeEngine.Current.DataGridPreferredColumnWidth;
			_readonly = false;
			rowheaders_visible = true;
			selection_backcolor = def_selection_backcolor;
			selection_forecolor = def_selection_forecolor;
			rowheaders_width = 35;
			visiblecolumn_count = 0;
			visiblerow_count = 0;
			current_cell = new DataGridCell ();
			first_visiblerow = 0;
			first_visiblecolumn = 0;
			horz_pixeloffset = 0;
			is_editing = false;
			is_changing = false;
			is_adding = false;
			parentrowslabel_style = DataGridParentRowsLabelStyle.Both;
			selected_rows = new Hashtable ();
			ctrl_pressed = false;
			shift_pressed = false;
			preferredrow_height = def_preferredrow_height = FontHeight + 3;
			cached_currencymgr_events = cached_currencymgr = null;
			accept_listmgrevents = true;

			default_style = new DataGridTableStyle (true);
			styles_collection = new GridTableStylesCollection (this);
			styles_collection.CollectionChanged += new CollectionChangeEventHandler (OnTableStylesCollectionChanged);

			CurrentTableStyle = default_style;

			horiz_scrollbar = new HScrollBar ();
			horiz_scrollbar.Scroll += new ScrollEventHandler  (GridHScrolled);
			vert_scrollbar = new VScrollBar ();
			vert_scrollbar.Scroll += new ScrollEventHandler (GridVScrolled);			
			KeyUp += new KeyEventHandler (OnKeyUpDG);			

			SetStyle (ControlStyles.UserMouse, true);

		}

		#endregion	// Public Constructor

		#region Public Instance Properties

		[DefaultValue(true)]
		public bool AllowNavigation {
			get {
				return allow_navigation;
			}

			set {
				if (allow_navigation != value) {
					allow_navigation = value;
					OnAllowNavigationChanged (EventArgs.Empty);
				}
			}
		}

		[DefaultValue(true)]
		public bool AllowSorting {
			get {
				return allow_sorting;
			}

			set {
				if (allow_sorting != value) {
					allow_sorting = value;
				}
			}
		}

		public Color AlternatingBackColor  {
			get {
				return alternating_backcolor;
			}

			set {
				if (alternating_backcolor != value) {
					alternating_backcolor = value;
					Refresh ();
				}
			}
		}

		public override Color BackColor {
			get {
				return backColor;
			}
			set {
				backColor = value;
			}
		}

		public Color BackgroundColor {
			get {
				return background_color;
			}
			set {
				 if (background_color != value) {
					background_color = value;
					OnBackgroundColorChanged (EventArgs.Empty);
					Refresh ();
				}
			}
		}

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public override Image BackgroundImage {
			get {
				return base.BackgroundImage;
			}

			set {
				base.BackgroundImage = value;
			}
		}

		[DispId(-504)]
		[DefaultValue(BorderStyle.Fixed3D)]
		public BorderStyle BorderStyle {
			get { return InternalBorderStyle; }
			set { 
				InternalBorderStyle = value; 
				CalcAreasAndInvalidate ();
				OnBorderStyleChanged (EventArgs.Empty);
			}
		}

		public Color CaptionBackColor {
			get {
				return caption_backcolor;
			}

			set {
				if (caption_backcolor != value) {
					caption_backcolor = value;
					grid_drawing.InvalidateCaption ();
				}
			}
		}

		[Localizable(true)]
		[AmbientValue(null)]
		public Font CaptionFont {
			get {
				if (caption_font == null) {
					return Font;
				}

				return caption_font;
			}

			set {
				if (caption_font != null && caption_font.Equals (value)) {
					return;
				}

				caption_font = value;
				CalcAreasAndInvalidate ();
			}
		}

		public Color CaptionForeColor {
			get {
				return caption_forecolor;
			}

			set {
				if (caption_forecolor != value) {
					caption_forecolor = value;
					grid_drawing.InvalidateCaption ();
				}
			}
		}

		[Localizable(true)]
		[DefaultValue("")]
		public string CaptionText {
			get {
				return caption_text;
			}

			set {
				if (caption_text != value) {
					caption_text = value;
					grid_drawing.InvalidateCaption ();
				}
			}
		}

		[DefaultValue(true)]
		public bool CaptionVisible {
			get {
				return caption_visible;
			}

			set {
				if (caption_visible != value) {
					caption_visible = value;
					CalcAreasAndInvalidate ();
					OnCaptionVisibleChanged (EventArgs.Empty);
				}
			}
		}

		[DefaultValue(true)]
		public bool ColumnHeadersVisible {
			get {
				return columnheaders_visible;
			}

			set {
				if (columnheaders_visible != value) {
					columnheaders_visible = value;
					CalcAreasAndInvalidate ();
				}
			}
		}

		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public DataGridCell CurrentCell {
			get {
				return current_cell;
			}

			set {
				if (!current_cell.Equals (value)) {
					CancelEditing ();
					
					int old_row = current_cell.RowNumber;
					
					if (value.RowNumber >= RowsCount) {
						value.RowNumber = RowsCount == 0 ? 0 : RowsCount - 1;
					}
					
					if (value.ColumnNumber >= CurrentTableStyle.GridColumnStyles.Count) {
						value.ColumnNumber = CurrentTableStyle.GridColumnStyles.Count == 0 ? 0: CurrentTableStyle.GridColumnStyles.Count - 1;
					}
					
					EnsureCellVisilibility (value);
					current_cell = value;					
					
					if (current_cell.RowNumber != old_row) {
						grid_drawing.InvalidateRowHeader (old_row);
					}
					
					accept_listmgrevents = false;

					if (cached_currencymgr_events !=  null) {
						cached_currencymgr_events.Position = current_cell.RowNumber;
					}
					accept_listmgrevents = true;
					InvalidateCurrentRowHeader ();
					OnCurrentCellChanged (EventArgs.Empty);
				}
			}
		}

		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public int CurrentRowIndex {
			get {
				if (ListManager == null) {
					return -1;
				}
				
				return current_cell.RowNumber;
			}

			set {
				if (current_cell.RowNumber != value) {
					CurrentCell = new DataGridCell (value, current_cell.ColumnNumber);
				}
			}
		}

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public override Cursor Cursor {
			get {
				return base.Cursor;
			}
			set {
				base.Cursor = value;
			}
		}

		[DefaultValue(null)]
		[Editor ("System.Windows.Forms.Design.DataMemberListEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
		public string DataMember {
			get { return datamember; }
			set {
				if (SetDataMember (value)) {					
					SetDataSource (datasource);
					if (styles_collection.Contains (value) == true) {
						CurrentTableStyle = styles_collection[value];
						current_style.CreateColumnsForTable (false);
					} else {
						CurrentTableStyle = default_style;
						current_style.GridColumnStyles.Clear ();
						current_style.CreateColumnsForTable (false);
					}					
				}
			}
		}

		[DefaultValue(null)]
		[RefreshProperties(RefreshProperties.Repaint)]
		[TypeConverter("System.Windows.Forms.Design.DataSourceConverter, " + Consts.AssemblySystem_Design)]
		public object DataSource {
			get {
				return datasource;
			}

			set {
				if (SetDataSource (value)) {
					SetNewDataSource ();					
				}
			}
		}

		protected override Size DefaultSize {
			get {
				return new Size (130, 80);
			}
		}

		[Browsable(false)]
		public int FirstVisibleColumn {
			get {
				return firstvisible_column;
			}
		}

		[DefaultValue(false)]
		public bool FlatMode {
			get {
				return flatmode;
			}

			set {
				if (flatmode != value) {
					flatmode = value;
					OnFlatModeChanged (EventArgs.Empty);
					Refresh ();
				}
			}
		}

		public override Color ForeColor {
			get {
				return base.ForeColor;
			}

			set {
				base.ForeColor = value;
			}
		}

		public Color GridLineColor {
			get {
				return gridline_color;
			}

			set {
				if (value == Color.Empty) {
					throw new ArgumentException ("Color.Empty value is invalid.");
				}

				if (gridline_color != value) {
					gridline_color = value;
					Refresh ();
				}
			}
		}

		[DefaultValue(DataGridLineStyle.Solid)]
		public DataGridLineStyle GridLineStyle {
			get {
				return gridline_style;
			}

			set {
				if (gridline_style != value) {
					gridline_style = value;
					Refresh ();
				}
			}
		}

		public Color HeaderBackColor {
			get {
				return header_backcolor;
			}

			set {
				if (value == Color.Empty) {
					throw new ArgumentException ("Color.Empty value is invalid.");
				}

				if (header_backcolor != value) {
					header_backcolor = value;
					Refresh ();
				}
			}
		}

		public Font HeaderFont {
			get {
				return header_font;
			}

			set {
				if (header_font != null && !header_font.Equals (value)) {
					header_font = value;
					CalcAreasAndInvalidate ();
				}
			}
		}

		public Color HeaderForeColor {
			get {
				return header_forecolor;
			}

			set {
				if (header_forecolor != value) {
					header_forecolor = value;
					Refresh ();
				}
			}
		}

		protected ScrollBar HorizScrollBar {
			get {
				return horiz_scrollbar;
			}
		}

		public object this [DataGridCell cell] {
			get  {
				return this [cell.RowNumber, cell.ColumnNumber];
			}

			set {
				this [cell.RowNumber, cell.ColumnNumber] = value;
			}
		}

		public object this [int rowIndex, int columnIndex] {
			get  {
				return CurrentTableStyle.GridColumnStyles[columnIndex].GetColumnValueAtRow (ListManager,
					rowIndex);
			}

			set {
				CurrentTableStyle.GridColumnStyles[columnIndex].SetColumnValueAtRow (ListManager,
					rowIndex, value);
			}
		}

		public Color LinkColor {
			get {
				return link_color;
			}
			set {
				if (link_color != value) {
					link_color = value;
					Refresh ();
				}
			}
		}

		[ComVisible(false)]
		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public Color LinkHoverColor {
			get {
				return link_hovercolor;
			}

			set {
				if (link_hovercolor != value) {
					link_hovercolor = value;
					Refresh ();
				}
			}
		}

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Advanced)]
		protected internal CurrencyManager ListManager {
			get {
				if (BindingContext == null || DataSource  == null) {
					return null;
				}

				if (cached_currencymgr != null) {
					return cached_currencymgr;
				}

				// If we bind real_datasource object we do not get the events from ListManger
				// since the object is not the datasource and does not match
				cached_currencymgr = (CurrencyManager) BindingContext [real_datasource, DataMember];
				cached_currencymgr_events = (CurrencyManager) BindingContext [datasource, DataMember];
				ConnectListManagerEvents ();
				return cached_currencymgr;
			}

			set {
				throw new NotSupportedException ("Operation is not supported.");
			}
		}

		public Color ParentRowsBackColor {
			get {
				return parentrowsback_color;
			}

			set {
				if (parentrowsback_color != value) {
					parentrowsback_color = value;
					if (parentrows_visible) {
						Refresh ();
					}
				}
			}
		}

		public Color ParentRowsForeColor {
			get {
				return parentrowsfore_color;
			}

			set {
				if (parentrowsfore_color != value) {
					parentrowsfore_color = value;
					if (parentrows_visible) {
						Refresh ();
					}
				}
			}
		}

		[DefaultValue(DataGridParentRowsLabelStyle.Both)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		public DataGridParentRowsLabelStyle ParentRowsLabelStyle {
			get {
				return parentrowslabel_style;
			}

			set {
				if (parentrowslabel_style != value) {
					parentrowslabel_style = value;
					if (parentrows_visible) {
						Refresh ();
					}

					OnParentRowsLabelStyleChanged (EventArgs.Empty);
				}
			}
		}

		[DefaultValue(true)]
		public bool ParentRowsVisible {
			get {
				return parentrows_visible;
			}

			set {
				if (parentrows_visible != value) {
					parentrows_visible = value;
					CalcAreasAndInvalidate ();
					OnParentRowsVisibleChanged (EventArgs.Empty);
				}
			}
		}

		// Settting this property seems to have no effect.
		[DefaultValue(75)]
		[TypeConverter(typeof(DataGridPreferredColumnWidthTypeConverter))]
		public int PreferredColumnWidth {
			get {
				return preferredcolumn_width;
			}

			set {
				if (value < 0) {
					throw new ArgumentException ("PreferredColumnWidth is less than 0");
				}

				if (preferredcolumn_width != value) {
					preferredcolumn_width = value;
					Refresh ();
				}
			}
		}

		public int PreferredRowHeight {
			get {
				return preferredrow_height;
			}

			set {
				if (preferredrow_height != value) {
					preferredrow_height = value;
					CalcAreasAndInvalidate ();
				}
			}
		}

		[DefaultValue(false)]
		public bool ReadOnly {
			get {
				return _readonly;
			}

			set {
				if (_readonly != value) {
					_readonly = value;
					OnReadOnlyChanged (EventArgs.Empty);
					CalcAreasAndInvalidate ();
				}
			}
		}

		[DefaultValue(true)]
		public bool RowHeadersVisible {
			get {
				return rowheaders_visible;
			}

			set {
				if (rowheaders_visible != value) {
					rowheaders_visible = value;
					CalcAreasAndInvalidate ();
				}
			}
		}

		[DefaultValue(35)]
		public int RowHeaderWidth {
			get {
				return rowheaders_width;
			}

			set {
				if (rowheaders_width != value) {
					rowheaders_width = value;
					CalcAreasAndInvalidate ();
				}
			}
		}

		public Color SelectionBackColor {
			get {
				return selection_backcolor;
			}

			set {
				if (selection_backcolor != value) {
					selection_backcolor = value;
					Refresh ();
				}
			}
		}

		public Color SelectionForeColor  {
			get {
				return selection_forecolor;
			}

			set {
				if (selection_forecolor != value) {
					selection_forecolor = value;
					Refresh ();
				}
			}
		}

		public override ISite Site {
			get {
				return base.Site;
			}
			set {
				base.Site = value;
			}
		}

		[Localizable(true)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
		public GridTableStylesCollection TableStyles {
			get { return styles_collection; }
		}

		[Bindable(false)]
		[Browsable(false)]
		[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public override string Text {
			get {
				return base.Text;
			}
			set {
				base.Text = value;
			}
		}

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Advanced)]
		protected ScrollBar VertScrollBar {
			get {
				return vert_scrollbar;
			}
		}

		[Browsable(false)]
		public int VisibleColumnCount {
			get {
				return visiblecolumn_count;
			}
		}

		// Calculated at DataGridDrawing.CalcRowsHeaders
		[Browsable(false)]
		public int VisibleRowCount {
			get {
				return visiblerow_count;
			}
		}

		#endregion	// Public Instance Properties

		#region Private Instance Properties
		internal DataGridTableStyle CurrentTableStyle {
			get {
				return current_style;
			}
			set {
				current_style = value;
				current_style.DataGrid = this;
				CalcAreasAndInvalidate ();
			}
		}

		internal int FirstVisibleRow {
			get { return first_visiblerow; }
			set { first_visiblerow = value;}
		}
		
		internal int RowsCount {
			get {				
				if (ListManager != null) {
					return ListManager.Count;					
				}

				return 0;
			}
		}

		internal int RowHeight {
			get {
				if (CurrentTableStyle.CurrentPreferredRowHeight > Font.Height + 3 + 1 /* line */) {
					return CurrentTableStyle.CurrentPreferredRowHeight;

				} else {
					return Font.Height + 3 + 1 /* line */;
				}
			}
		}
		
		internal bool ShowEditRow {
			get {
				if (ListManager != null && ListManager.CanAddRows == false) {
					return false;
				}
								
				return _readonly == false;
			}
		}
		
		// It should only be shown if there are relations that
		// we do not support right now
		internal bool ShowParentRowsVisible {
			get {
				//See parentrows_visible;
				return false;
			}
		}
		
		#endregion Private Instance Properties

		#region Public Instance Methods

		[MonoTODO]
		public virtual bool BeginEdit (DataGridColumnStyle gridColumn, int rowNumber)
		{
			return false;
		}

		public virtual void BeginInit ()
		{
			begininit = true;
		}

		protected virtual void CancelEditing ()
		{			
			if (current_cell.ColumnNumber < CurrentTableStyle.GridColumnStyles.Count) {
				CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].Abort (current_cell.RowNumber);
			}
			
			if (is_adding == true) {
				ListManager.RemoveAt (RowsCount - 1);
				is_adding = false;
			}
			
			is_editing = false;
			is_changing = false;
			InvalidateCurrentRowHeader ();
		}

		[MonoTODO]
		public void Collapse (int row)
		{

		}

		protected internal virtual void ColumnStartedEditing (Control editingControl)
		{

		}

		protected internal virtual void ColumnStartedEditing (Rectangle bounds)
		{

		}

		protected override AccessibleObject CreateAccessibilityInstance ()
		{
			return base.CreateAccessibilityInstance ();
		}

		protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop)
		{
			return CreateGridColumn (prop, false);
		}

		protected virtual DataGridColumnStyle CreateGridColumn (PropertyDescriptor prop, bool isDefault)
		{
			throw new NotImplementedException();
		}

		protected override void Dispose (bool disposing)
		{
			base.Dispose (disposing);
		}

		public virtual bool EndEdit (DataGridColumnStyle gridColumn, int rowNumber, bool shouldAbort)
		{						
			if (is_adding == true) {				
				if (shouldAbort) {
					ListManager.CancelCurrentEdit ();
				} else {
					ListManager.EndCurrentEdit ();
					CalcAreasAndInvalidate ();
				}
				is_adding = false;
			} 

			if (shouldAbort || gridColumn.ParentReadOnly ==true) {
				gridColumn.Abort (rowNumber);
			} else {
				gridColumn.Commit (ListManager, rowNumber);
			}

			is_editing = false;
			is_changing = false;
			InvalidateCurrentRowHeader ();
			return true;
		}

		public virtual void EndInit ()
		{
			begininit = false;
		}

		public void Expand (int row)
		{

		}

		public Rectangle GetCellBounds (DataGridCell cell)
		{
			return GetCellBounds (cell.RowNumber, cell.ColumnNumber);
		}

		public Rectangle GetCellBounds (int row, int col)
		{
			return grid_drawing.GetCellBounds (row, col);
		}

		public Rectangle GetCurrentCellBounds ()
		{
			return GetCellBounds (current_cell.RowNumber, current_cell.ColumnNumber);
		}

		protected virtual string GetOutputTextDelimiter ()
		{
			return string.Empty;
		}

		protected virtual void GridHScrolled (object sender, ScrollEventArgs se)
		{
			if (se.NewValue == horz_pixeloffset ||
				se.Type == ScrollEventType.EndScroll) {
				return;
			}

			ScrollToColumnInPixels (se.NewValue);
		}

		protected virtual void GridVScrolled (object sender, ScrollEventArgs se)
		{
			int old_first_visiblerow = first_visiblerow;
			first_visiblerow = se.NewValue;
			grid_drawing.UpdateVisibleRowCount ();
			
			if (first_visiblerow == old_first_visiblerow) {
				return;
			}			
			ScrollToRow (old_first_visiblerow, first_visiblerow);
		}

		public HitTestInfo HitTest (Point position)
		{
			return HitTest (position.X, position.Y);
		}

		public HitTestInfo HitTest (int x, int y)
		{
			return grid_drawing.HitTest (x, y);
		}

		[MonoTODO]
		public bool IsExpanded (int rowNumber)
		{
			return false;
		}

		public bool IsSelected (int row)
		{
			return selected_rows[row] != null;
		}

		[MonoTODO]
		public void NavigateBack ()
		{

		}

		[MonoTODO]
		public void NavigateTo (int rowNumber, string relationName)
		{

		}

		protected virtual void OnAllowNavigationChanged (EventArgs e)
		{
			if (AllowNavigationChanged != null) {
				AllowNavigationChanged (this, e);
			}
		}

		protected void OnBackButtonClicked (object sender,  EventArgs e)
		{
			if (BackButtonClick != null) {
				BackButtonClick (sender, e);
			}
		}

		protected override void OnBackColorChanged (EventArgs e)
		{
			base.OnBackColorChanged (e);
		}

		protected virtual void OnBackgroundColorChanged (EventArgs e)
		{
			if (BackgroundColorChanged != null) {
				BackgroundColorChanged (this, e);
			}
		}

		protected override void OnBindingContextChanged( EventArgs e)
		{
			base.OnBindingContextChanged (e);
		}

		protected virtual void OnBorderStyleChanged (EventArgs e)
		{
			if (BorderStyleChanged != null) {
				BorderStyleChanged (this, e);
			}
		}

		protected virtual void OnCaptionVisibleChanged (EventArgs e)
		{
			if (CaptionVisibleChanged != null) {
				CaptionVisibleChanged (this, e);
			}
		}

		protected virtual void OnCurrentCellChanged (EventArgs e)
		{
			if (CurrentCellChanged != null) {
				CurrentCellChanged (this, e);
			}
		}

		protected virtual void OnDataSourceChanged (EventArgs e)
		{
			if (DataSourceChanged != null) {
				DataSourceChanged (this, e);
			}
		}

		protected override void OnEnter (EventArgs e)
		{
			base.OnEnter (e);
		}

		protected virtual void OnFlatModeChanged (EventArgs e)
		{
			if (FlatModeChanged != null) {
				FlatModeChanged (this, e);
			}
		}

		protected override void OnFontChanged (EventArgs e)
		{
			grid_drawing.CalcGridAreas ();
			base.OnFontChanged (e);
		}

		protected override void OnForeColorChanged (EventArgs e)
		{
			base.OnForeColorChanged (e);
		}

		protected override void OnHandleCreated (EventArgs e)
		{
			base.OnHandleCreated (e);
			grid_drawing.CalcGridAreas ();
		}

		protected override void OnHandleDestroyed (EventArgs e)
		{
			base.OnHandleDestroyed (e);
		}

		protected override void OnKeyDown (KeyEventArgs ke)
		{
			base.OnKeyDown (ke);
			
			if (ProcessGridKey (ke) == true) {
				ke.Handled = true;
			}

			if (CurrentTableStyle.GridColumnStyles.Count > 0) {
				CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber].OnKeyDown
					(ke, current_cell.RowNumber, current_cell.ColumnNumber);
			}
		}

		protected override void OnKeyPress (KeyPressEventArgs kpe)
		{
			base.OnKeyPress (kpe);
		}

		protected override void OnLayout (LayoutEventArgs levent)
		{
			base.OnLayout (levent);
			CalcAreasAndInvalidate ();			
		}

		protected override void OnLeave (EventArgs e)
		{
			base.OnLeave (e);
		}

		protected override void OnMouseDown (MouseEventArgs e)
		{
			base.OnMouseDown (e);

			HitTestInfo testinfo;
			testinfo = grid_drawing.HitTest (e.X, e.Y);

			switch (testinfo.type) {
			case HitTestType.Cell:
			{
				if (testinfo.Row < 0 || testinfo.Column < 0)
					break;
					
				DataGridCell new_cell = new DataGridCell (testinfo.Row, testinfo.Column);

				if (new_cell.Equals (current_cell) == false) {
					CancelEditing ();
					CurrentCell = new_cell;
					EditCell (current_cell);

				} else {
					CurrentTableStyle.GridColumnStyles[testinfo.Column].OnMouseDown (e, testinfo.Row, testinfo.Column);
				}

				break;
			}
			case HitTestType.RowHeader:
			{
				if (ctrl_pressed == false && shift_pressed == false) {
					ResetSelection (); // Invalidates selected rows
				}

				if (shift_pressed == true) {
					ShiftSelection (testinfo.Row);
				} else { // ctrl_pressed or single item
					Select (testinfo.Row);
				}

				CancelEditing ();
				CurrentCell = new DataGridCell (testinfo.Row, current_cell.ColumnNumber);
				OnRowHeaderClick (EventArgs.Empty);
				break;
			}

			case HitTestType.ColumnHeader:
			{
				if (CurrentTableStyle.GridColumnStyles.Count == 0)
					break;

				if (allow_sorting == false)
					break;

				if (ListManager.List is IBindingList == false)
					break;
			
				ListSortDirection direction = ListSortDirection.Ascending;
				PropertyDescriptor prop = CurrentTableStyle.GridColumnStyles[testinfo.Column].PropertyDescriptor;
				IBindingList list = (IBindingList) ListManager.List;

				if (list.SortProperty != null) {
					CurrentTableStyle.GridColumnStyles[list.SortProperty].ArrowDrawingMode 
						= DataGridColumnStyle.ArrowDrawing.No;
				}

				if (prop == list.SortProperty && list.SortDirection == ListSortDirection.Ascending) {
					direction = ListSortDirection.Descending;
				}
				
				CurrentTableStyle.GridColumnStyles[testinfo.Column].ArrowDrawingMode =
					direction == ListSortDirection.Ascending ? 
					DataGridColumnStyle.ArrowDrawing.Ascending : DataGridColumnStyle.ArrowDrawing.Descending;
				
				list.ApplySort (prop, direction);
				Refresh ();
				break;
			}
			
			default:
				break;
			}
		}

		protected override void OnMouseLeave (EventArgs e)
		{
			base.OnMouseLeave (e);
		}

		protected override void OnMouseMove (MouseEventArgs e)
		{
			base.OnMouseMove (e);
		}

		protected override void OnMouseUp (MouseEventArgs e)
		{
			base.OnMouseUp (e);
		}

		protected override void OnMouseWheel (MouseEventArgs e)
		{
			base.OnMouseWheel (e);

			if (ctrl_pressed == false) { // scroll horizontal
				if (e.Delta > 0) {
					if (current_cell.RowNumber > 0) {
						CurrentCell = new DataGridCell (current_cell.RowNumber - 1, current_cell.ColumnNumber);
					}
				}
				else {
					if (current_cell.RowNumber < RowsCount - 1) {
						CurrentCell = new DataGridCell (current_cell.RowNumber + 1, current_cell.ColumnNumber);					
					}
				}
			} else {
				if (e.Delta > 0) {
					if (current_cell.ColumnNumber > 0) {
						CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber - 1);
					}
				}
				else {
					if (current_cell.ColumnNumber < CurrentTableStyle.GridColumnStyles.Count - 1) {
						CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber + 1);					
					}
				}
			}
		}

		protected void OnNavigate (NavigateEventArgs e)
		{
			if (Navigate != null) {
				Navigate (this, e);
			}
		}

		protected override void OnPaint (PaintEventArgs pe)
		{
			ThemeEngine.Current.DataGridPaint (pe, this);
		}

		protected override void OnPaintBackground (PaintEventArgs ebe)
		{
		}

		protected virtual void OnParentRowsLabelStyleChanged (EventArgs e)
		{
			if (ParentRowsLabelStyleChanged != null) {
				ParentRowsLabelStyleChanged (this, e);
			}
		}

		protected virtual void OnParentRowsVisibleChanged (EventArgs e)
		{
			if (ParentRowsVisibleChanged != null) {
				ParentRowsVisibleChanged (this, e);
			}
		}

		protected virtual void OnReadOnlyChanged (EventArgs e)
		{
			if (ReadOnlyChanged != null) {
				ReadOnlyChanged (this, e);
			}
		}

		protected override void OnResize (EventArgs e)
		{
			base.OnResize (e);
		}

		protected void OnRowHeaderClick (EventArgs e)
		{
			if (RowHeaderClick != null) {
				RowHeaderClick (this, e);
			}
		}

		protected void OnScroll (EventArgs e)
		{
			if (Scroll != null) {
				Scroll (this, e);
			}
		}

		protected void OnShowParentDetailsButtonClicked (object sender, EventArgs e)
		{
			if (ShowParentDetailsButtonClick != null) {
				ShowParentDetailsButtonClick (sender, e);
			}
		}

		protected override bool ProcessDialogKey (Keys keyData)
		{
			return base.ProcessDialogKey (keyData);
		}

		protected bool ProcessGridKey (KeyEventArgs ke)
		{
			if (RowsCount == 0) {
				return false;
			}

			switch (ke.KeyCode) {
			case Keys.ControlKey:
				ctrl_pressed = true;
				break;
			case Keys.ShiftKey:
				shift_pressed = true;
				break;
			case Keys.Up:
			{
				if (current_cell.RowNumber > 0) {
					CurrentCell = new DataGridCell (current_cell.RowNumber - 1, current_cell.ColumnNumber);
					EditCell (current_cell);
				}
				break;
			}
			case Keys.Down:
			{
				if (current_cell.RowNumber < RowsCount - 1) {
					CurrentCell = new DataGridCell (current_cell.RowNumber + 1, current_cell.ColumnNumber);
					EditCell (current_cell);
				}
				break;
			}
			case Keys.Tab:
			case Keys.Right:
			{				
				if (current_cell.ColumnNumber + 1 < CurrentTableStyle.GridColumnStyles.Count) {
					CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber + 1);
					EditCell (current_cell);
				}
				break;
			}
			case Keys.Left:
			{
				if (current_cell.ColumnNumber > 0) {
					CurrentCell = new DataGridCell (current_cell.RowNumber, current_cell.ColumnNumber - 1);
					EditCell (current_cell);
				}
				break;
			}
			case Keys.PageUp:
			{
				if (current_cell.RowNumber > grid_drawing.VLargeChange) {
					CurrentCell = new DataGridCell (current_cell.RowNumber - grid_drawing.VLargeChange, current_cell.ColumnNumber);
				} else {
					CurrentCell = new DataGridCell (0, current_cell.ColumnNumber);
				}

				EditCell (current_cell);
				break;
			}
			case Keys.PageDown:
			{
				if (current_cell.RowNumber + grid_drawing.VLargeChange < RowsCount) {
					CurrentCell = new DataGridCell (current_cell.RowNumber + grid_drawing.VLargeChange, current_cell.ColumnNumber);
				} else {
					CurrentCell = new DataGridCell (RowsCount - 1, current_cell.ColumnNumber);
				}

				EditCell (current_cell);
				break;
			}
			case Keys.Home:
			{
				CurrentCell = new DataGridCell (0, current_cell.ColumnNumber);
				EditCell (current_cell);
				break;
			}
			case Keys.End:
			{
				CurrentCell = new DataGridCell (RowsCount - 1, current_cell.ColumnNumber);
				EditCell (current_cell);
				break;
			}
			case Keys.Delete:
			{				
				foreach (int row in selected_rows.Keys) {
					ListManager.RemoveAt (row);						
				}
				selected_rows.Clear ();
				CalcAreasAndInvalidate ();
				break;					
			}
			default:
				return false; // message not processed
			}

			return true; // message processed
		}

		// Called from DataGridTextBox
		protected override bool ProcessKeyPreview (ref Message m)
		{
			Keys key = (Keys) m.WParam.ToInt32 ();
			KeyEventArgs ke = new KeyEventArgs (key);
			if (ProcessGridKey (ke) == true) {
				return true;
			}

			return base.ProcessKeyPreview (ref m);
		}
		
		protected bool ProcessTabKey (Keys keyData)
		{
			return false;
		}

		public void ResetAlternatingBackColor ()
		{
			alternating_backcolor = def_alternating_backcolor;
		}

		public override void ResetBackColor ()
		{
			backColor = ThemeEngine.Current.DataGridBackColor;
		}

		public override void ResetForeColor ()
		{
			base.ResetForeColor ();
		}

		public void ResetGridLineColor ()
		{
			gridline_color = def_gridline_color;
		}

		public void ResetHeaderBackColor ()
		{
			header_backcolor = def_header_backcolor;
		}

		public void ResetHeaderFont ()
		{
			header_font = def_header_font;
		}

		public void ResetHeaderForeColor ()
		{
			header_forecolor = def_header_forecolor;
		}

		public void ResetLinkColor ()
		{
			link_color = def_link_color;
		}

		public void ResetLinkHoverColor ()
		{
			link_hovercolor = def_link_hovercolor;
		}

		protected void ResetSelection ()
		{			
			foreach (int row in selected_rows.Keys) {
				grid_drawing.InvalidateRow (row);
				grid_drawing.InvalidateRowHeader (row);
			}

			selected_rows.Clear ();
		}

		public void ResetSelectionBackColor ()
		{
			selection_backcolor = def_selection_backcolor;
		}

		public void ResetSelectionForeColor ()
		{
			selection_forecolor = def_selection_forecolor;
		}

		public void Select (int row)
		{
			if (selected_rows[row] == null) {
				selected_rows.Add (row, true);
			} else {
				selected_rows[row] = true;
			}

			grid_drawing.InvalidateRow (row);
		}

		public void SetDataBinding (object dataSource, string dataMember)
		{
			dataMember = null;
			SetDataSource (dataSource);
			SetDataMember (dataMember);		
			SetNewDataSource ();
		}

		protected virtual bool ShouldSerializeAlternatingBackColor ()
		{
			return (alternating_backcolor != def_alternating_backcolor);
		}

		protected virtual bool ShouldSerializeBackgroundColor ()
		{
			return (background_color != def_background_color);
		}

		protected virtual bool ShouldSerializeCaptionBackColor ()
		{
			return (caption_backcolor != def_caption_backcolor);
		}

		protected virtual bool ShouldSerializeCaptionForeColor ()
		{
			return (caption_forecolor != def_caption_forecolor);
		}

		protected virtual bool ShouldSerializeGridLineColor ()
		{
			return (gridline_color != def_gridline_color);
		}

		protected virtual bool ShouldSerializeHeaderBackColor ()
		{
			return (header_backcolor != def_header_backcolor);
		}

		protected bool ShouldSerializeHeaderFont ()
		{
			return (header_font != def_header_font);
		}

		protected virtual bool ShouldSerializeHeaderForeColor ()
		{
			return (header_forecolor != def_header_forecolor);
		}

		protected virtual bool ShouldSerializeLinkHoverColor ()
		{
			return (link_hovercolor != def_link_hovercolor);
		}

		protected virtual bool ShouldSerializeParentRowsBackColor ()
		{
			return (parentrowsback_color != def_parentrowsback_color);
		}

		protected virtual bool ShouldSerializeParentRowsForeColor ()
		{
			return (parentrowsback_color != def_parentrowsback_color);
		}

		protected bool ShouldSerializePreferredRowHeight ()
		{
			return (preferredrow_height != def_preferredrow_height);
		}

		protected bool ShouldSerializeSelectionBackColor ()
		{
			return (selection_backcolor != def_selection_backcolor);
		}

		protected virtual bool ShouldSerializeSelectionForeColor ()
		{
			return (selection_forecolor != def_selection_forecolor);
		}

		public void SubObjectsSiteChange (bool site)
		{

		}

		public void UnSelect (int row)
		{
			selected_rows.Remove (row);
			grid_drawing.InvalidateRow (row);

		}
		#endregion	// Public Instance Methods

		#region Private Instance Methods

		internal void CalcAreasAndInvalidate ()
		{
			grid_drawing.CalcGridAreas ();
			Invalidate ();
		}
		
		private void ConnectListManagerEvents ()
		{
			cached_currencymgr_events.CurrentChanged += new EventHandler (OnListManagerCurrentChanged);			
		}
		
		private void DisconnectListManagerEvents ()
		{
			
		}

		// EndEdit current editing operation
		internal virtual bool EndEdit (bool shouldAbort)
		{
			return EndEdit (CurrentTableStyle.GridColumnStyles[current_cell.ColumnNumber],
				current_cell.RowNumber, shouldAbort);
		}

		private void EnsureCellVisilibility (DataGridCell cell)
		{
			if (cell.ColumnNumber <= first_visiblecolumn ||
				cell.ColumnNumber + 1 >= first_visiblecolumn + visiblecolumn_count) {			
					
				first_visiblecolumn = grid_drawing.GetFirstColumnForColumnVisilibility (first_visiblecolumn, cell.ColumnNumber);						
				
				int pixel = grid_drawing.GetColumnStartingPixel (first_visiblecolumn);
				ScrollToColumnInPixels (pixel);
			}

			if (cell.RowNumber < first_visiblerow ||
				cell.RowNumber + 1 >= first_visiblerow + visiblerow_count) {

				if (cell.RowNumber + 1 >= first_visiblerow + visiblerow_count) {
					int old_first_visiblerow = first_visiblerow;
					first_visiblerow = 1 + cell.RowNumber - visiblerow_count;
					grid_drawing.UpdateVisibleRowCount ();
					ScrollToRow (old_first_visiblerow, first_visiblerow);
				}else {
					int old_first_visiblerow = first_visiblerow;
					first_visiblerow = cell.RowNumber;
					grid_drawing.UpdateVisibleRowCount ();
					ScrollToRow (old_first_visiblerow, first_visiblerow);
				}

				vert_scrollbar.Value = first_visiblerow;
			}
		}
		
		internal IEnumerable GetDataSource (object source, string member)
		{	
			IListSource src = (IListSource) source;
			IList list = src.GetList();
			IListSource listsource;
			ITypedList typedlist;
					
			if (source is IEnumerable) {
				return (IEnumerable) source;
			}
			
			if(src.ContainsListCollection == false)	{
				return list;
			}
			
			listsource = (IListSource) source;
			
			if (listsource == null) {
				return null;
			}
			
			list = src.GetList ();
			
			if (list == null) {
				return null;
			}
			
			typedlist = (ITypedList) list;
				
			if (typedlist == null) {
				return null;
			}			

			PropertyDescriptorCollection col = typedlist.GetItemProperties (new PropertyDescriptor [0]);
			PropertyDescriptor prop = col.Find (member, true);
								
			if (prop == null) {
				if (col.Count > 0) {
					prop = col[0];

					if (prop == null) {
						return null;
					}
				}
			}
			
			IEnumerable result =  (IEnumerable)(prop.GetValue (list[0]));
			return result;		
			
		}

		internal void InvalidateCurrentRowHeader ()
		{
			grid_drawing.InvalidateRowHeader (current_cell.RowNumber);
		}

		private bool SetDataMember (string member)
		{			
			if (member == datamember) {
				return false;
			}

			datamember = member;
			real_datasource = GetDataSource (datasource, member);
			DisconnectListManagerEvents ();
			cached_currencymgr = cached_currencymgr_events = null;
			return true;
		}

		private bool SetDataSource (object source)
		{			

			if (source != null && source as IListSource != null && source as IList != null) {
				throw new Exception ("Wrong complex data binding source");
			}
			
			current_cell = new DataGridCell ();
			datasource = source;
			DisconnectListManagerEvents ();
			cached_currencymgr = cached_currencymgr_events = null;
			try {
				real_datasource = GetDataSource (datasource, DataMember);
			}catch (Exception e) {				
				real_datasource = source;
			}

			OnDataSourceChanged (EventArgs.Empty);
			return true;
		}

		private void SetNewDataSource ()
		{				
			if (ListManager != null && TableStyles[datamember] == null) {
				current_style.GridColumnStyles.Clear ();
			}
			current_style.CreateColumnsForTable (false);
			CalcAreasAndInvalidate ();			
		}

		private void OnKeyUpDG (object sender, KeyEventArgs e)
		{
			switch (e.KeyCode) {
			case Keys.ControlKey:
				ctrl_pressed = false;
				break;
			case Keys.ShiftKey:
				shift_pressed = false;
				break;
			default:
				break;
			}
		}
		
		private void OnListManagerCurrentChanged (object sender, EventArgs e)
		{			
			if (accept_listmgrevents == false) {
				return;
			}
			
			CurrentCell = new DataGridCell (cached_currencymgr_events.Position, current_cell.RowNumber);
		}
		
		private void OnTableStylesCollectionChanged (object sender, CollectionChangeEventArgs e)
		{				
			if (ListManager == null)
				return;
			
			switch (e.Action){
				case CollectionChangeAction.Add: {
					if (e.Element != null && String.Compare (ListManager.ListName, ((DataGridTableStyle)e.Element).MappingName, true) == 0) {
						CurrentTableStyle = (DataGridTableStyle)e.Element;
						((DataGridTableStyle) e.Element).CreateColumnsForTable (false);
					}
					break;
				}

				case CollectionChangeAction.Remove: {
					if (e.Element != null && String.Compare (ListManager.ListName, ((DataGridTableStyle)e.Element).MappingName, true) == 0) {
						CurrentTableStyle = default_style;						
						current_style.GridColumnStyles.Clear ();
						current_style.CreateColumnsForTable (false);
					}
					break;
				}	

				
				case CollectionChangeAction.Refresh: {
					if (e.Element != null && String.Compare (ListManager.ListName, ((DataGridTableStyle)e.Element).MappingName, true) == 0) {
						CurrentTableStyle = (DataGridTableStyle)e.Element;
						((DataGridTableStyle) e.Element).CreateColumnsForTable (false);
					} else {
						CurrentTableStyle = default_style;
						current_style.GridColumnStyles.Clear ();
						current_style.CreateColumnsForTable (false);

					}
					break;

				}
			}						
			CalcAreasAndInvalidate ();
		}

		private void EditCell (DataGridCell cell)
		{
			ResetSelection (); // Invalidates selected rows
			is_editing = false;
			is_changing = false;
			
			if (ShowEditRow && is_adding == false && cell.RowNumber >= RowsCount) {
				ListManager.AddNew ();
				is_adding = true;
				Invalidate (); // We have just added a new row
			}
			
			CurrentTableStyle.GridColumnStyles[cell.ColumnNumber].Edit (ListManager,
				cell.RowNumber, GetCellBounds (cell.RowNumber, cell.ColumnNumber),
				_readonly, string.Empty, true);
		}

		private void ShiftSelection (int index)
		{
			int shorter_item = -1, dist = RowsCount + 1, cur_dist;			

			foreach (int row in selected_rows.Keys) {

				if (row > index) {
					cur_dist = row - index;
				}
				else {
					cur_dist = index - row;
				}

				if (cur_dist < dist) {
					dist = cur_dist;
					shorter_item = row;
				}
			}

			if (shorter_item != -1) {
				int start, end;

				if (shorter_item > index) {
					start = index;
					end = shorter_item;
				} else {
					start = shorter_item;
					end = index;
				}

				ResetSelection ();
				for (int idx = start; idx <= end; idx++) {
					Select (idx);
				}
			}
		}

		private void ScrollToColumnInPixels (int pixel)
		{
			Rectangle invalidate = new Rectangle ();
			Rectangle invalidate_column = new Rectangle ();

			if (pixel > horz_pixeloffset) { // ScrollRight
				int pixels = pixel - horz_pixeloffset;
				
				horz_pixeloffset = horiz_scrollbar.Value = pixel;
				grid_drawing.UpdateVisibleColumn ();

				// Columns header
				invalidate_column.X = grid_drawing.ColumnsHeadersArea.X + grid_drawing.ColumnsHeadersArea.Width - pixels;
				invalidate_column.Y = grid_drawing.ColumnsHeadersArea.Y;
				invalidate_column.Width = pixels;
				invalidate_column.Height = grid_drawing.ColumnsHeadersArea.Height;
				XplatUI.ScrollWindow (Handle, grid_drawing.ColumnsHeadersArea, -pixels, 0, false);

				// Cells
				invalidate.X = grid_drawing.CellsArea.X + grid_drawing.CellsArea.Width - pixels;
				invalidate.Y = grid_drawing.CellsArea.Y;
				invalidate.Width = pixels;
				invalidate.Height = grid_drawing.CellsArea.Height;
				
				
				if (columnheaders_visible == true) {
					invalidate.Y -= grid_drawing.ColumnsHeadersArea.Height;
					invalidate.Height += grid_drawing.ColumnsHeadersArea.Height;
				}
				
				XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, -pixels, 0, false);
				Invalidate (invalidate_column);
				Invalidate (invalidate);


			} else {
				int pixels = horz_pixeloffset - pixel;
				Rectangle area = grid_drawing.CellsArea;
				
				horz_pixeloffset = horiz_scrollbar.Value = pixel;
				grid_drawing.UpdateVisibleColumn ();

				// Columns header
				invalidate_column.X = grid_drawing.ColumnsHeadersArea.X;
				invalidate_column.Y = grid_drawing.ColumnsHeadersArea.Y;
				invalidate_column.Width = pixels;
				invalidate_column.Height = grid_drawing.ColumnsHeadersArea.Height;
				//XplatUI.ScrollWindow (Handle, grid_drawing.ColumnsHeadersArea, pixels, 0, false);

				// Cells
				invalidate.X =  grid_drawing.CellsArea.X;
				invalidate.Y =  grid_drawing.CellsArea.Y;
				invalidate.Width = pixels;
				invalidate.Height = grid_drawing.CellsArea.Height;
				
				if (columnheaders_visible == true) {
					invalidate.Y -= grid_drawing.ColumnsHeadersArea.Height;
					invalidate.Height += grid_drawing.ColumnsHeadersArea.Height;
					area.Y -= grid_drawing.ColumnsHeadersArea.Height;
					area.Height += grid_drawing.ColumnsHeadersArea.Height;
				}
				
				XplatUI.ScrollWindow (Handle, area, pixels, 0, false);
				Invalidate (invalidate);
			}		
			
		}

		private void ScrollToRow (int old_row, int new_row)
		{
			Rectangle invalidate = new Rectangle ();			
			
			if (new_row > old_row) { // Scrolldown
				int scrolled_rows = new_row - old_row;
				int pixels = scrolled_rows * RowHeight;
				Rectangle rows_area = grid_drawing.CellsArea; // Cells area - partial rows space
				rows_area.Height = grid_drawing.CellsArea.Height - grid_drawing.CellsArea.Height % RowHeight;
				
				invalidate.X =  grid_drawing.CellsArea.X;
				invalidate.Y =  grid_drawing.CellsArea.Y + rows_area.Height - pixels;
				invalidate.Width = grid_drawing.CellsArea.Width;
				invalidate.Height = pixels;

				XplatUI.ScrollWindow (Handle, rows_area, 0, -pixels, false);

			} else { // ScrollUp
				int scrolled_rows = old_row - new_row;				
				int pixels = scrolled_rows * RowHeight;

				invalidate.X =  grid_drawing.CellsArea.X;
				invalidate.Y =  grid_drawing.CellsArea.Y;
				invalidate.Width = grid_drawing.CellsArea.Width;
				invalidate.Height = pixels;
				XplatUI.ScrollWindow (Handle, grid_drawing.CellsArea, 0, pixels, false);				
			}

			// Right now we use ScrollWindow Invalidate, let's leave remarked it here for X11 if need it
			//Invalidate (invalidate);
			Invalidate (grid_drawing.RowsHeadersArea);
		}

		#endregion Private Instance Methods


		#region Events
		public event EventHandler AllowNavigationChanged;
		public event EventHandler BackButtonClick;
		public event EventHandler BackgroundColorChanged;

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public new event EventHandler BackgroundImageChanged;

		public event EventHandler BorderStyleChanged;
		public event EventHandler CaptionVisibleChanged;
		public event EventHandler CurrentCellChanged;

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public new event EventHandler CursorChanged;

		public event EventHandler DataSourceChanged;
		public event EventHandler FlatModeChanged;
		public event NavigateEventHandler Navigate;
		public event EventHandler ParentRowsLabelStyleChanged;
		public event EventHandler ParentRowsVisibleChanged;
		public event EventHandler ReadOnlyChanged;
		protected event EventHandler RowHeaderClick;
		public event EventHandler Scroll;
		public event EventHandler ShowParentDetailsButtonClick;

		[Browsable(false)]
		[EditorBrowsable(EditorBrowsableState.Never)]
		public new event EventHandler TextChanged;
		#endregion	// Events
	}
}