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

DataTable.cs « Data « System « System.Data « referencesource « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 637816ef0c379f684e5e1133aca1e168b9896e41 (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
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
//------------------------------------------------------------------------------
// <copyright file="DataTable.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
// <owner current="true" primary="false">[....]</owner>
//------------------------------------------------------------------------------

namespace System.Data {
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Diagnostics;
    using System.Globalization;
    using System.IO;
    using System.Runtime.Serialization;
    using System.Text;
    using System.Threading;
    using System.Xml;
    using System.Xml.Schema;
    using System.Xml.Serialization;
    using System.Data.Common;
    using System.Runtime.Versioning;
    using System.Runtime.CompilerServices;

    /// <devdoc>
    ///    <para>Represents one table of in-memory data.</para>
    /// </devdoc>
    [
    ToolboxItem(false),
    DesignTimeVisible(false),
    DefaultProperty("TableName"),
    Editor("Microsoft.VSDesigner.Data.Design.DataTableEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing),
    DefaultEvent("RowChanging"),
    XmlSchemaProvider("GetDataTableSchema"),
    Serializable
    ]
    public class DataTable : MarshalByValueComponent, System.ComponentModel.IListSource, ISupportInitializeNotification, ISerializable, IXmlSerializable{
        private DataSet dataSet;
        private DataView defaultView = null;

        // rows
        /// <summary>
        /// Monotonically increasing number representing the order <see cref="DataRow"/> have been added to <see cref="DataRowCollection"/>.
        /// </summary>
        /// <remarks>This limits <see cref="DataRowCollection.Add(DataRow)"/> to <see cref="Int32.MaxValue"/> operations.</remarks>
        internal long nextRowID;
        internal readonly DataRowCollection rowCollection;

        // columns
        internal readonly DataColumnCollection columnCollection;

        // constraints
        private readonly ConstraintCollection constraintCollection;

        //SimpleContent implementation
        private int elementColumnCount = 0;

        // relations
        internal DataRelationCollection parentRelationsCollection;
        internal DataRelationCollection childRelationsCollection;

        // RecordManager
        internal readonly RecordManager recordManager;

        // index mgmt
        internal readonly List<Index> indexes;

        private List<Index> shadowIndexes;
        private int shadowCount;

        // props
        internal PropertyCollection extendedProperties = null;
        private string tableName = "";
        internal string tableNamespace = null;
        private string tablePrefix = "";
        internal DataExpression displayExpression;
        internal bool fNestedInDataset = true;

        // globalization stuff
        private CultureInfo _culture;
        private bool _cultureUserSet;
        private CompareInfo _compareInfo;
        private CompareOptions _compareFlags = CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth;
        private IFormatProvider _formatProvider;
        private StringComparer _hashCodeProvider;
        private bool _caseSensitive;
        private bool _caseSensitiveUserSet;

        // XML properties
        internal string encodedTableName;           // For XmlDataDocument only
        internal DataColumn xmlText;            // text values of a complex xml element
        internal DataColumn _colUnique;
        internal bool textOnly = false;         // the table has only text value with possible attributes
        internal decimal minOccurs = 1;    // default = 1
        internal decimal maxOccurs = 1;    // default = 1
        internal bool repeatableElement = false;
        private object typeName = null;

        // primary key info
        private readonly static Int32[] zeroIntegers = new Int32[0];
        internal readonly static DataColumn[] zeroColumns = new DataColumn[0];
        internal readonly static DataRow[] zeroRows = new DataRow[0];
        internal UniqueConstraint primaryKey;
        internal readonly static IndexField[] zeroIndexField = new IndexField[0];
        internal IndexField[] _primaryIndex = zeroIndexField;
        private DataColumn[] delayedSetPrimaryKey = null;

        // Loading Schema and/or Data related optimization
        private Index loadIndex;
        private Index loadIndexwithOriginalAdded = null;
        private Index loadIndexwithCurrentDeleted = null;
        private int _suspendIndexEvents;

        private bool savedEnforceConstraints = false;
        private bool inDataLoad = false;
        private bool initialLoad;
        private bool schemaLoading = false;
        private bool enforceConstraints = true;
        internal bool _suspendEnforceConstraints = false;

        protected internal bool fInitInProgress = false;
        private bool inLoad = false;
        internal bool fInLoadDiffgram = false;

        private byte _isTypedDataTable; // 0 == unknown, 1 = yes, 2 = No
        private DataRow[] EmptyDataRowArray;


        // Property Descriptor Cache for DataBinding
        private PropertyDescriptorCollection propertyDescriptorCollectionCache = null;

        // Cache for relation that has this table as nested child table.
        private static readonly DataRelation[] EmptyArrayDataRelation = new DataRelation[0];
        private DataRelation[] _nestedParentRelations = EmptyArrayDataRelation;

        // Dependent column list for expression evaluation
        internal List<DataColumn> dependentColumns = null;

        // events
        private bool mergingData = false;
        private DataRowChangeEventHandler onRowChangedDelegate;
        private DataRowChangeEventHandler onRowChangingDelegate;
        private DataRowChangeEventHandler onRowDeletingDelegate;
        private DataRowChangeEventHandler onRowDeletedDelegate;
        private DataColumnChangeEventHandler onColumnChangedDelegate;
        private DataColumnChangeEventHandler onColumnChangingDelegate;

        private DataTableClearEventHandler onTableClearingDelegate;
        private DataTableClearEventHandler onTableClearedDelegate;
        private DataTableNewRowEventHandler onTableNewRowDelegate;

        private PropertyChangedEventHandler onPropertyChangingDelegate;

        private System.EventHandler  onInitialized;


        // misc
        private readonly DataRowBuilder rowBuilder;
        private const String KEY_XMLSCHEMA = "XmlSchema";
        private const String KEY_XMLDIFFGRAM = "XmlDiffGram";
        private const String KEY_NAME = "TableName";

        internal readonly List<DataView> delayedViews = new List<DataView>();
        private readonly List<DataViewListener> _dataViewListeners = new List<DataViewListener>();

//        private bool serializeHierarchy = false;
        internal Hashtable rowDiffId = null;
        internal readonly ReaderWriterLock indexesLock = new ReaderWriterLock();
        internal int ukColumnPositionForInference= -1;

        // default remoting format is Xml
        private SerializationFormat _remotingFormat = SerializationFormat.Xml;

        private static int _objectTypeCount; // Bid counter
        private readonly int _objectID = System.Threading.Interlocked.Increment(ref _objectTypeCount);

        /// <devdoc>
        /// <para>Initializes a new instance of the <see cref='System.Data.DataTable'/> class with no arguments.</para>
        /// </devdoc>
        public DataTable() {
            GC.SuppressFinalize(this);
            Bid.Trace("<ds.DataTable.DataTable|API> %d#\n", ObjectID);
            nextRowID = 1;
            recordManager = new RecordManager(this);

            _culture = CultureInfo.CurrentCulture;
            this.columnCollection = new DataColumnCollection(this);
            this.constraintCollection = new ConstraintCollection(this);
            this.rowCollection = new DataRowCollection(this);
            this.indexes = new List<Index>();

            rowBuilder = new DataRowBuilder(this, -1);
        }

        /// <devdoc>
        /// <para>Intitalizes a new instance of the <see cref='System.Data.DataTable'/> class with the specified table
        ///    name.</para>
        /// </devdoc>
        public DataTable(string tableName) : this() {
            this.tableName = tableName == null ? "" : tableName;
        }

        public DataTable(string tableName, string tableNamespace) : this(tableName) {
            this.Namespace = tableNamespace;
        }

//        Deserialize the table from binary/xml stream.
        protected DataTable(SerializationInfo info, StreamingContext context) : this()
        {
            bool isSingleTable = context.Context != null ? Convert.ToBoolean(context.Context, CultureInfo.InvariantCulture) : true;
            SerializationFormat remotingFormat = SerializationFormat.Xml;
            SerializationInfoEnumerator e = info.GetEnumerator();
            while (e.MoveNext()) {
                switch(e.Name) {
                    case "DataTable.RemotingFormat" : //DataTable.RemotingFormat does not exist in V1/V1.1 versions
                    remotingFormat = (SerializationFormat)e.Value;
                    break;
                }
            }

            DeserializeDataTable(info, context, isSingleTable, remotingFormat);
        }

        [System.Security.Permissions.SecurityPermissionAttribute(System.Security.Permissions.SecurityAction.LinkDemand, Flags=System.Security.Permissions.SecurityPermissionFlag.SerializationFormatter)]
        public virtual void GetObjectData(SerializationInfo info, StreamingContext context) {
            SerializationFormat remotingFormat = RemotingFormat;
            bool isSingleTable = context.Context != null ? Convert.ToBoolean(context.Context, CultureInfo.InvariantCulture) : true;
            SerializeDataTable(info, context, isSingleTable, remotingFormat);
        }

//        Serialize the table schema and data.
        private void SerializeDataTable(SerializationInfo info, StreamingContext context, bool isSingleTable, SerializationFormat remotingFormat) {
            info.AddValue("DataTable.RemotingVersion", new Version(2, 0));

            // SqlHotFix 299, SerializationFormat enumeration types don't exist in V1.1 SP1
            if (SerializationFormat.Xml != remotingFormat) {
                info.AddValue("DataTable.RemotingFormat", remotingFormat);
            }

            if (remotingFormat != SerializationFormat.Xml) {//Binary
                SerializeTableSchema(info, context, isSingleTable);
                if (isSingleTable) {
                    SerializeTableData(info, context, 0);
                }
            } else {//XML/V1.0/V1.1
                string tempDSNamespace = "";
                Boolean fCreatedDataSet = false;

                if (dataSet == null) {
                    DataSet ds = new DataSet("tmpDataSet");
                    // if user set values on DataTable, it isn't necessary
                    // to set them on the DataSet because they won't be inherited
                    // but it is simpler to set them in both places

                    // if user did not set values on DataTable, it is required
                    // to set them on the DataSet so the table will inherit
                    // the value already on the Datatable
                    ds.SetLocaleValue(_culture, _cultureUserSet);
                    ds.CaseSensitive = this.CaseSensitive;
                    ds.namespaceURI  = this.Namespace;
                    Debug.Assert(ds.RemotingFormat == SerializationFormat.Xml, "RemotingFormat must be SerializationFormat.Xml");
                    ds.Tables.Add(this);
                    fCreatedDataSet = true;
                } else {
                    tempDSNamespace = this.DataSet.Namespace;
                    this.DataSet.namespaceURI = this.Namespace; //this.DataSet.Namespace = this.Namespace; ??
                }

                info.AddValue(KEY_XMLSCHEMA, dataSet.GetXmlSchemaForRemoting(this));
                info.AddValue(KEY_XMLDIFFGRAM, dataSet.GetRemotingDiffGram(this));

                if (fCreatedDataSet) {
                    dataSet.Tables.Remove(this);
                }
                else{
                    dataSet.namespaceURI  = tempDSNamespace;
                }
            }
        }

//        Deserialize the table schema and data.
        internal void DeserializeDataTable(SerializationInfo info, StreamingContext context, bool isSingleTable, SerializationFormat remotingFormat) {
            if (remotingFormat != SerializationFormat.Xml) {//Binary
                DeserializeTableSchema(info, context, isSingleTable);
                if (isSingleTable) {
                    DeserializeTableData(info, context, 0);
                    this.ResetIndexes();
                }
            } else {//XML/V1.0/V1.1
                string strSchema = (String)info.GetValue(KEY_XMLSCHEMA, typeof(System.String));
                string strData = (String)info.GetValue(KEY_XMLDIFFGRAM, typeof(System.String));

                if (strSchema != null) {
                    DataSet ds = new DataSet();
                    // fxcop: ReadXmlSchema will provide the CaseSensitive, Locale, Namespace information
                    ds.ReadXmlSchema(new XmlTextReader( new StringReader( strSchema ) ) );

                    Debug.Assert(ds.Tables.Count == 1, "There should be exactly 1 table here");
                    DataTable table = ds.Tables[0];
                    table.CloneTo(this, null, false);// WebData 111656
                    //this is to avoid the cascading rules in the namespace
                    this.Namespace = table.Namespace;

                    if (strData != null) {
                        ds.Tables.Remove(ds.Tables[0]);
                        ds.Tables.Add(this);
                        ds.ReadXml(new XmlTextReader( new StringReader( strData ) ), XmlReadMode.DiffGram);
                        ds.Tables.Remove(this);
                    }
                }
            }
        }

//        Serialize the columns
        internal void SerializeTableSchema(SerializationInfo info, StreamingContext context, bool isSingleTable) {
            //DataTable basic  properties
            info.AddValue("DataTable.TableName", TableName);
            info.AddValue("DataTable.Namespace", Namespace);
            info.AddValue("DataTable.Prefix", Prefix);
            info.AddValue("DataTable.CaseSensitive", _caseSensitive);
            info.AddValue("DataTable.caseSensitiveAmbient", !_caseSensitiveUserSet);
            info.AddValue("DataTable.LocaleLCID", Locale.LCID);
            info.AddValue("DataTable.MinimumCapacity", recordManager.MinimumCapacity);
            //info.AddValue("DataTable.DisplayExpression", DisplayExpression);

            //DataTable state internal properties
            info.AddValue("DataTable.NestedInDataSet", fNestedInDataset);
            info.AddValue("DataTable.TypeName", TypeName.ToString());
            info.AddValue("DataTable.RepeatableElement", repeatableElement);


            //ExtendedProperties
            info.AddValue("DataTable.ExtendedProperties", ExtendedProperties);

            //Columns
            info.AddValue("DataTable.Columns.Count", Columns.Count);

            //Check for closure of expression in case of single table.
            if (isSingleTable) {
                List<DataTable> list = new List<DataTable>();
                list.Add(this);
                if (!CheckForClosureOnExpressionTables(list))
                    throw ExceptionBuilder.CanNotRemoteDataTable();
            }

            IFormatProvider formatProvider = CultureInfo.InvariantCulture;
            for (int i = 0; i < Columns.Count; i++) {
                //DataColumn basic properties
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.ColumnName", i), Columns[i].ColumnName);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.Namespace", i), Columns[i]._columnUri);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.Prefix", i), Columns[i].Prefix);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.ColumnMapping", i), Columns[i].ColumnMapping);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.AllowDBNull", i), Columns[i].AllowDBNull);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrement", i), Columns[i].AutoIncrement);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrementStep", i), Columns[i].AutoIncrementStep);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrementSeed", i), Columns[i].AutoIncrementSeed);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.Caption", i), Columns[i].Caption);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.DefaultValue", i), Columns[i].DefaultValue);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.ReadOnly", i), Columns[i].ReadOnly);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.MaxLength", i), Columns[i].MaxLength);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.DataType", i), Columns[i].DataType);

                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.XmlDataType", i), Columns[i].XmlDataType);
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.SimpleType", i), Columns[i].SimpleType);

                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.DateTimeMode", i), Columns[i].DateTimeMode);

                //DataColumn internal state properties
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrementCurrent", i), Columns[i].AutoIncrementCurrent);

                //Expression
                if (isSingleTable) {
                    info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.Expression", i), Columns[i].Expression);
                }

                //ExtendedProperties
                info.AddValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.ExtendedProperties", i), Columns[i].extendedProperties);
            }

            //Constraints
            if (isSingleTable) {
                SerializeConstraints(info, context, 0, false);
            }
        }

//        Deserialize all the Columns
        internal void DeserializeTableSchema(SerializationInfo info, StreamingContext context, bool isSingleTable) {
            //DataTable basic properties
            tableName = info.GetString("DataTable.TableName");
            tableNamespace = info.GetString("DataTable.Namespace");
            tablePrefix = info.GetString("DataTable.Prefix");

            bool caseSensitive = info.GetBoolean("DataTable.CaseSensitive");
            SetCaseSensitiveValue(caseSensitive, true, false);
            _caseSensitiveUserSet = !info.GetBoolean("DataTable.caseSensitiveAmbient");

            int lcid = (int)info.GetValue("DataTable.LocaleLCID", typeof(int));
            CultureInfo culture = new CultureInfo(lcid);
            SetLocaleValue(culture, true, false);
            _cultureUserSet = true;


            MinimumCapacity = info.GetInt32("DataTable.MinimumCapacity");
            //DisplayExpression = info.GetString("DataTable.DisplayExpression");

            //DataTable state internal properties
            fNestedInDataset = (bool) info.GetBoolean("DataTable.NestedInDataSet");
            string tName = info.GetString("DataTable.TypeName");
            typeName =  new XmlQualifiedName(tName);
            repeatableElement = info.GetBoolean("DataTable.RepeatableElement");

            //ExtendedProperties
            extendedProperties = (PropertyCollection) info.GetValue("DataTable.ExtendedProperties", typeof(PropertyCollection));

            //Columns
            int colCount = info.GetInt32("DataTable.Columns.Count");
            string [] expressions = new string[colCount];
            Debug.Assert(Columns.Count == 0, "There is column in Table");

            IFormatProvider formatProvider = CultureInfo.InvariantCulture;
            for (int i = 0; i < colCount; i++) {
                DataColumn dc = new DataColumn();

                //DataColumn public state properties
                dc.ColumnName = info.GetString(String.Format(formatProvider, "DataTable.DataColumn_{0}.ColumnName", i));
                dc._columnUri = info.GetString(String.Format(formatProvider, "DataTable.DataColumn_{0}.Namespace", i));
                dc.Prefix = info.GetString(String.Format(formatProvider, "DataTable.DataColumn_{0}.Prefix", i));

                dc.DataType = (Type) info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.DataType", i), typeof(Type));
                dc.XmlDataType = (string) info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.XmlDataType", i), typeof(string));
                dc.SimpleType = (SimpleType) info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.SimpleType", i), typeof(SimpleType));

                dc.ColumnMapping = (MappingType) info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.ColumnMapping", i), typeof(MappingType));
                dc.DateTimeMode = (DataSetDateTime) info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.DateTimeMode", i), typeof(DataSetDateTime));

                dc.AllowDBNull = info.GetBoolean(String.Format(formatProvider, "DataTable.DataColumn_{0}.AllowDBNull", i));
                dc.AutoIncrement = info.GetBoolean(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrement", i));
                dc.AutoIncrementStep = info.GetInt64(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrementStep", i));
                dc.AutoIncrementSeed = info.GetInt64(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrementSeed", i));
                dc.Caption = info.GetString(String.Format(formatProvider, "DataTable.DataColumn_{0}.Caption", i));
                dc.DefaultValue = info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.DefaultValue", i), typeof(object));
                dc.ReadOnly = info.GetBoolean(String.Format(formatProvider, "DataTable.DataColumn_{0}.ReadOnly", i));
                dc.MaxLength= info.GetInt32(String.Format(formatProvider, "DataTable.DataColumn_{0}.MaxLength", i));

                //DataColumn internal state properties
                dc.AutoIncrementCurrent = info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.AutoIncrementCurrent", i), typeof(object));

                //Expression
                if (isSingleTable) {
                    expressions[i] = info.GetString(String.Format(formatProvider, "DataTable.DataColumn_{0}.Expression", i));
                }

                //ExtendedProperties
                dc.extendedProperties = (PropertyCollection) info.GetValue(String.Format(formatProvider, "DataTable.DataColumn_{0}.ExtendedProperties", i), typeof(PropertyCollection));
                Columns.Add(dc);
            }
            if (isSingleTable) {
                for(int i = 0; i < colCount; i++) {
                    if (expressions[i] != null) {
                        Columns[i].Expression = expressions[i];
                    }
                }
            }

            //Constraints
            if (isSingleTable) {
                DeserializeConstraints(info, context, /*table index */ 0, /* serialize all constraints */false);// since single table, send table index as 0, meanwhile passing
                // false for 'allConstraints' means, handle all the constraint related to the table
            }
        }

/*
        Serialize constraints availabe on the table - note this function is marked internal because it is called by the DataSet deserializer.
        ***Schema for Serializing ArrayList of Constraints***
        Unique Constraint - ["U"]->[constraintName]->[columnIndexes]->[IsPrimaryKey]->[extendedProperties]
        Foriegn Key Constraint - ["F"]->[constraintName]->[parentTableIndex, parentcolumnIndexes]->[childTableIndex, childColumnIndexes]->[AcceptRejectRule, UpdateRule, DeleteRule]->[extendedProperties]
*/
        internal void SerializeConstraints(SerializationInfo info, StreamingContext context, int serIndex, bool allConstraints) {
            if (allConstraints) {
                Debug.Assert(DataSet != null);
            }

            ArrayList constraintList = new ArrayList();

            for (int i = 0; i < Constraints.Count; i++) {
                Constraint c = Constraints[i];

                UniqueConstraint uc = c as UniqueConstraint;
                if (uc != null) {
                    int[] colInfo = new int[uc.Columns.Length];
                    for (int j = 0; j < colInfo.Length; j++) {
                        colInfo[j] = uc.Columns[j].Ordinal;
                    }

                    ArrayList list = new ArrayList();
                    list.Add("U");
                    list.Add(uc.ConstraintName);
                    list.Add(colInfo);
                    list.Add(uc.IsPrimaryKey);
                    list.Add(uc.ExtendedProperties);

                    constraintList.Add(list);
                } else {
                    ForeignKeyConstraint fk = c as ForeignKeyConstraint;
                    Debug.Assert(fk != null);
                    bool shouldSerialize = (allConstraints == true) || (fk.Table == this && fk.RelatedTable == this);

                    if (shouldSerialize) {
                        int[] parentInfo = new int[fk.RelatedColumns.Length + 1];
                        parentInfo[0] = allConstraints ? this.DataSet.Tables.IndexOf(fk.RelatedTable) : 0;
                        for (int j = 1; j < parentInfo.Length; j++) {
                            parentInfo[j] = fk.RelatedColumns[j - 1].Ordinal;
                        }

                        int[] childInfo = new int[fk.Columns.Length + 1];
                        childInfo[0] = allConstraints ? this.DataSet.Tables.IndexOf(fk.Table) : 0 ;   //Since the constraint is on the current table, this is the child table.
                        for (int j = 1; j < childInfo.Length; j++) {
                            childInfo[j] = fk.Columns[j - 1].Ordinal;
                        }

                        ArrayList list = new ArrayList();
                        list.Add("F");
                        list.Add(fk.ConstraintName);
                        list.Add(parentInfo);
                        list.Add(childInfo);
                        list.Add(new int[] { (int) fk.AcceptRejectRule, (int) fk.UpdateRule, (int) fk.DeleteRule });
                        list.Add(fk.ExtendedProperties);

                        constraintList.Add(list);
                    }
                }
            }
            info.AddValue(String.Format(CultureInfo.InvariantCulture, "DataTable_{0}.Constraints", serIndex), constraintList);
        }

/*
        Deserialize the constraints on the table.
        ***Schema for Serializing ArrayList of Constraints***
        Unique Constraint - ["U"]->[constraintName]->[columnIndexes]->[IsPrimaryKey]->[extendedProperties]
        Foriegn Key Constraint - ["F"]->[constraintName]->[parentTableIndex, parentcolumnIndexes]->[childTableIndex, childColumnIndexes]->[AcceptRejectRule, UpdateRule, DeleteRule]->[extendedProperties]
*/
        internal void DeserializeConstraints(SerializationInfo info, StreamingContext context, int serIndex, bool allConstraints) {
            ArrayList constraintList = (ArrayList) info.GetValue(String.Format(CultureInfo.InvariantCulture, "DataTable_{0}.Constraints", serIndex), typeof(ArrayList));

            foreach (ArrayList list in constraintList) {
                string con = (string) list[0];

                if (con.Equals("U")) { //Unique Constraints
                    string constraintName = (string) list[1];

                    int[] keyColumnIndexes = (int[]) list[2];
                    bool isPrimaryKey = (bool) list[3];
                    PropertyCollection extendedProperties = (PropertyCollection) list[4];

                    DataColumn[] keyColumns = new DataColumn[keyColumnIndexes.Length];
                    for (int i = 0; i < keyColumnIndexes.Length; i++) {
                        keyColumns[i] = Columns[keyColumnIndexes[i]];
                    }

                    //Create the constraint.
                    UniqueConstraint uc = new UniqueConstraint(constraintName, keyColumns, isPrimaryKey);
                    uc.extendedProperties = extendedProperties;

                    //Add the unique constraint and it will in turn set the primary keys also if needed.
                    Constraints.Add(uc);
                } else { //ForeignKeyConstraints
                    Debug.Assert(con.Equals("F"));

                    string constraintName = (string) list[1];
                    int[] parentInfo = (int[]) list[2];
                    int[] childInfo = (int[]) list[3];
                    int[] rules = (int[]) list[4];
                    PropertyCollection extendedProperties = (PropertyCollection) list[5];

                    //ParentKey Columns.
                    DataTable parentTable = (allConstraints == false) ? this : this.DataSet.Tables[parentInfo[0]];
                    DataColumn[] parentkeyColumns = new DataColumn[parentInfo.Length - 1];
                    for (int i = 0; i < parentkeyColumns.Length; i++) {
                        parentkeyColumns[i] = parentTable.Columns[parentInfo[i + 1]];
                    }

                    //ChildKey Columns.
                    DataTable childTable = (allConstraints == false) ? this : this.DataSet.Tables[childInfo[0]];
                    DataColumn[] childkeyColumns = new DataColumn[childInfo.Length - 1];
                    for (int i = 0; i < childkeyColumns.Length; i++) {
                        childkeyColumns[i] = childTable.Columns[childInfo[i + 1]];
                    }

                    //Create the Constraint.
                    ForeignKeyConstraint fk = new ForeignKeyConstraint(constraintName, parentkeyColumns, childkeyColumns);
                    fk.AcceptRejectRule = (AcceptRejectRule) rules[0];
                    fk.UpdateRule = (Rule) rules[1];
                    fk.DeleteRule = (Rule) rules[2];
                    fk.extendedProperties = extendedProperties;

                    //Add just the foreign key constraint without creating unique constraint.
                    Constraints.Add(fk, false);
                }
            }
        }

//        Serialize the expressions on the table - Marked internal so that DataSet deserializer can call into this
        internal void SerializeExpressionColumns(SerializationInfo info, StreamingContext context, int serIndex) {
            int colCount = Columns.Count;
            for (int i = 0; i < colCount; i++) {
                info.AddValue(String.Format(CultureInfo.InvariantCulture, "DataTable_{0}.DataColumn_{1}.Expression", serIndex, i), Columns[i].Expression);
            }
        }

//        Deserialize the expressions on the table - Marked internal so that DataSet deserializer can call into this
        internal void DeserializeExpressionColumns(SerializationInfo info, StreamingContext context, int serIndex) {
            int colCount = Columns.Count;
            for (int i = 0; i < colCount; i++) {
                string expr = info.GetString(String.Format(CultureInfo.InvariantCulture, "DataTable_{0}.DataColumn_{1}.Expression", serIndex, i));
                if (0 != expr.Length) {
                    Columns[i].Expression = expr;
                }
            }
        }

//        Serialize all the Rows.
        internal void SerializeTableData(SerializationInfo info, StreamingContext context, int serIndex) {
            //Cache all the column count, row count
            int colCount = Columns.Count;
            int rowCount = Rows.Count;
            int modifiedRowCount = 0;
            int editRowCount = 0;

            //Compute row states and assign the bits accordingly - 00[Unchanged], 01[Added], 10[Modifed], 11[Deleted]
            BitArray rowStates = new BitArray(rowCount * 3, false); //All bit flags are set to false on initialization of the BitArray.
            for (int i = 0; i < rowCount; i++) {
                int bitIndex = i * 3;
                DataRow row = Rows[i];
                DataRowState rowState = row.RowState;
                switch (rowState) {
                    case DataRowState.Unchanged:
                        //rowStates[bitIndex] = false;
                        //rowStates[bitIndex + 1] = false;
                        break;
                    case DataRowState.Added:
                        //rowStates[bitIndex] = false;
                        rowStates[bitIndex + 1] = true;
                        break;
                    case DataRowState.Modified:
                        rowStates[bitIndex] = true;
                        //rowStates[bitIndex + 1] = false;
                        modifiedRowCount++;
                        break;
                    case DataRowState.Deleted:
                        rowStates[bitIndex] = true;
                        rowStates[bitIndex + 1] = true;
                        break;
                    default:
                        throw ExceptionBuilder.InvalidRowState(rowState);
                }
                if (-1 != row.tempRecord) {
                    rowStates[bitIndex + 2] = true;
                    editRowCount++;
                }
            }

            //Compute the actual storage records that need to be created.
            int recordCount = rowCount + modifiedRowCount + editRowCount;

            //Create column storages.
            ArrayList storeList = new ArrayList();
            ArrayList nullbitList = new ArrayList();
            if (recordCount > 0) { //Create the storage only if have records.
                for (int i = 0; i < colCount; i++) {
                    object store = Columns[i].GetEmptyColumnStore(recordCount);
                    storeList.Add(store);
                    BitArray nullbits = new BitArray(recordCount);
                    nullbitList.Add(nullbits);
                }
            }

            //Copy values into column storages
            int recordsConsumed = 0;
            Hashtable rowErrors = new Hashtable();
            Hashtable colErrors = new Hashtable();
            for (int i = 0; i < rowCount; i++) {
                int recordsPerRow = Rows[i].CopyValuesIntoStore(storeList, nullbitList, recordsConsumed);
                GetRowAndColumnErrors(i, rowErrors, colErrors);
                recordsConsumed += recordsPerRow;
            }

            IFormatProvider formatProvider = CultureInfo.InvariantCulture;
            //Serialize all the computed values.
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.Rows.Count", serIndex), rowCount);
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.Records.Count", serIndex), recordCount);
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.RowStates", serIndex), rowStates);
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.Records", serIndex), storeList);
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.NullBits", serIndex), nullbitList);
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.RowErrors", serIndex), rowErrors);
            info.AddValue(String.Format(formatProvider, "DataTable_{0}.ColumnErrors", serIndex), colErrors);
        }

//        Deserialize all the Rows.
        internal void DeserializeTableData(SerializationInfo info, StreamingContext context, int serIndex) {
            bool enforceConstraintsOrg = enforceConstraints;
            bool inDataLoadOrg = inDataLoad;


            try {
                enforceConstraints = false;
                inDataLoad = true;
                IFormatProvider formatProvider = CultureInfo.InvariantCulture;
                int rowCount = info.GetInt32(String.Format(formatProvider, "DataTable_{0}.Rows.Count", serIndex));
                int recordCount = info.GetInt32(String.Format(formatProvider, "DataTable_{0}.Records.Count", serIndex));
                BitArray rowStates = (BitArray) info.GetValue(String.Format(formatProvider, "DataTable_{0}.RowStates", serIndex), typeof(BitArray));
                ArrayList storeList = (ArrayList) info.GetValue(String.Format(formatProvider, "DataTable_{0}.Records", serIndex), typeof(ArrayList));
                ArrayList nullbitList = (ArrayList) info.GetValue(String.Format(formatProvider, "DataTable_{0}.NullBits", serIndex), typeof(ArrayList));
                Hashtable rowErrors = (Hashtable) info.GetValue(String.Format(formatProvider, "DataTable_{0}.RowErrors", serIndex), typeof(Hashtable));
                rowErrors.OnDeserialization(this);//OnDeSerialization must be called since the hashtable gets deserialized after the whole graph gets deserialized
                Hashtable colErrors = (Hashtable) info.GetValue(String.Format(formatProvider, "DataTable_{0}.ColumnErrors", serIndex), typeof(Hashtable));
                colErrors.OnDeserialization(this);//OnDeSerialization must be called since the hashtable gets deserialized after the whole graph gets deserialized


                if (recordCount <= 0) { //No need for deserialization of the storage and errors if there are no records.
                    return;
                }

                //Point the record manager storage to the deserialized values.
                for (int i = 0; i < Columns.Count; i++) {
                    Columns[i].SetStorage(storeList[i], (BitArray) nullbitList[i]);
                }

                //Create rows and set the records appropriately.
                int recordIndex = 0;
                DataRow[] rowArr = new DataRow[recordCount];
                for (int i = 0; i < rowCount; i++) {
                    //Create a new row which sets old and new records to -1.
                    DataRow row = NewEmptyRow();
                    rowArr[recordIndex] = row;
                    int bitIndex = i * 3;
                    switch (ConvertToRowState(rowStates, bitIndex)) {
                        case DataRowState.Unchanged:
                            row.oldRecord = recordIndex;
                            row.newRecord = recordIndex;
                            recordIndex += 1;
                            break;
                        case DataRowState.Added:
                            row.oldRecord = -1;
                            row.newRecord = recordIndex;
                            recordIndex += 1;
                            break;
                        case DataRowState.Modified:
                            row.oldRecord = recordIndex;
                            row.newRecord = recordIndex + 1;
                            rowArr[recordIndex + 1] = row;
                            recordIndex += 2;
                            break;
                        case DataRowState.Deleted:
                            row.oldRecord = recordIndex;
                            row.newRecord = -1;
                            recordIndex += 1;
                            break;
                    }
                    if (rowStates[bitIndex + 2]) {
                        row.tempRecord = recordIndex;
                        rowArr[recordIndex] = row;
                        recordIndex += 1;
                    } else {
                        row.tempRecord = -1;
                    }
                    Rows.ArrayAdd(row);
                    row.rowID = nextRowID;
                    nextRowID++;
                    ConvertToRowError(i, rowErrors, colErrors);
                }
                recordManager.SetRowCache(rowArr);
                ResetIndexes();
            } finally {
                enforceConstraints = enforceConstraintsOrg;
                inDataLoad = inDataLoadOrg;
            }
        }

//        Constructs the RowState from the two bits in the bitarray.
        private DataRowState ConvertToRowState(BitArray bitStates, int bitIndex) {
            Debug.Assert(bitStates != null);
            Debug.Assert(bitStates.Length > bitIndex);

            bool b1 = bitStates[bitIndex];
            bool b2 = bitStates[bitIndex + 1];

            if (!b1 && !b2) {
                return DataRowState.Unchanged;
            } else if (!b1 && b2) {
                return DataRowState.Added;
            } else if (b1 && !b2) {
                return DataRowState.Modified;
            } else if (b1 && b2) {
                return DataRowState.Deleted;
            } else {
                throw ExceptionBuilder.InvalidRowBitPattern();
            }
        }

//        Get the error on the row and columns - Marked internal so that DataSet deserializer can call into this
        internal void GetRowAndColumnErrors(int rowIndex, Hashtable rowErrors, Hashtable colErrors) {
            Debug.Assert(Rows.Count > rowIndex);
            Debug.Assert(rowErrors != null);
            Debug.Assert(colErrors != null);

            DataRow row = Rows[rowIndex];

            if (row.HasErrors) {
                rowErrors.Add(rowIndex, row.RowError);
                DataColumn[] dcArr = row.GetColumnsInError();
                if (dcArr.Length > 0) {
                    int[] columnsInError = new int[dcArr.Length];
                    string[] columnErrors = new string[dcArr.Length];
                    for (int i = 0; i < dcArr.Length; i++) {
                        columnsInError[i] = dcArr[i].Ordinal;
                        columnErrors[i] = row.GetColumnError(dcArr[i]);
                    }
                    ArrayList list = new ArrayList();
                    list.Add(columnsInError);
                    list.Add(columnErrors);
                    colErrors.Add(rowIndex, list);
                }
            }
        }

//        Set the row and columns in error..
        private void ConvertToRowError(int rowIndex, Hashtable rowErrors, Hashtable colErrors) {
            Debug.Assert(Rows.Count > rowIndex);
            Debug.Assert(rowErrors != null);
            Debug.Assert(colErrors != null);

            DataRow row = Rows[rowIndex];

            if (rowErrors.ContainsKey(rowIndex)) {
                row.RowError = (string) rowErrors[rowIndex];
            }
            if (colErrors.ContainsKey(rowIndex)) {
                ArrayList list = (ArrayList) colErrors[rowIndex];
                int[] columnsInError = (int[]) list[0];
                string[] columnErrors = (string[]) list[1];
                Debug.Assert(columnsInError.Length == columnErrors.Length);
                for (int i = 0; i < columnsInError.Length; i++) {
                    row.SetColumnError(columnsInError[i], columnErrors[i]);
                }
            }
        }

        /// <devdoc>
        ///    <para>Indicates whether string comparisons within the table are case-sensitive.</para>
        /// </devdoc>
        [ResDescriptionAttribute(Res.DataTableCaseSensitiveDescr)]
        public bool CaseSensitive {
            get {
                //The following assert is valid except when calling DataSet.set_CaseSensitive which Validates constraints and failing here
                //Debug.Assert(_caseSensitiveUserSet || (null == dataSet) || (dataSet.CaseSensitive == _caseSensitive), "CaseSensitive mismatch");
                return _caseSensitive;
            }
            set {
                if (_caseSensitive != value) {
                    bool oldValue = _caseSensitive;
                    bool oldUserSet = _caseSensitiveUserSet;
                    _caseSensitive = value;
                    _caseSensitiveUserSet = true;

                    if (DataSet != null && !DataSet.ValidateCaseConstraint()) {
                        _caseSensitive = oldValue;
                        _caseSensitiveUserSet = oldUserSet;
                        throw ExceptionBuilder.CannotChangeCaseLocale();
                    }
                    SetCaseSensitiveValue(value, true, true);
                }
                _caseSensitiveUserSet = true;
            }
        }

        internal bool AreIndexEventsSuspended {
            get { return (0 < _suspendIndexEvents); }
        }

        internal void RestoreIndexEvents(bool forceReset) {
            Bid.Trace("<ds.DataTable.RestoreIndexEvents|Info> %d#, %d\n", ObjectID, _suspendIndexEvents);
            if (0 < _suspendIndexEvents) {
                _suspendIndexEvents--;
                if (0 == _suspendIndexEvents) {
                    Exception first = null;
                    SetShadowIndexes();
                    try{
                    // the length of shadowIndexes will not change
                    // but the array instance may change during
                    // events during Index.Reset
                        int numIndexes = shadowIndexes.Count;
                        for (int i = 0; i < numIndexes; i++) {
                            Index ndx = shadowIndexes[i];// shadowindexes may change, see ShadowIndexCopy()
                            try {
                                if (forceReset || ndx.HasRemoteAggregate) {
                                    ndx.Reset(); // resets & fires
                                }
                                else {
                                    ndx.FireResetEvent(); // fire the Reset event we were firing
                                }
                            }
                            catch(Exception e) {
                                if (!ADP.IsCatchableExceptionType (e)) {
                                    throw;
                                }
                                ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                                if (null == first) {
                                    first = e;
                                }
                            }
                        }
                        if (null != first) {
                            throw first;
                        }
                    }
                   finally {
                       RestoreShadowIndexes();
                   }
                }
            }
        }

        internal void SuspendIndexEvents() {
            Bid.Trace("<ds.DataTable.SuspendIndexEvents|Info> %d#, %d\n", ObjectID, _suspendIndexEvents);
            _suspendIndexEvents++;
        }

        [Browsable(false)]
        public bool IsInitialized {
            get {
                return !fInitInProgress;
            }
        }

        private bool IsTypedDataTable {
            get {
                switch (_isTypedDataTable) {
                case 0:
                    _isTypedDataTable = (byte)((this.GetType() != typeof(DataTable))? 1 : 2);
                    return (1 == _isTypedDataTable);
                case 1:
                    return true;
                default:
                    return false;
                }
            }
        }

        internal bool SetCaseSensitiveValue(bool isCaseSensitive, bool userSet, bool resetIndexes) {
            if (userSet || (!_caseSensitiveUserSet && (_caseSensitive != isCaseSensitive))) {
                _caseSensitive = isCaseSensitive;
                if (isCaseSensitive) {
                    _compareFlags = CompareOptions.None;
                }
                else {
                    _compareFlags = CompareOptions.IgnoreCase | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreWidth;
                }
                if (resetIndexes) {
                    ResetIndexes();
                    foreach (Constraint constraint in Constraints) {
                       constraint.CheckConstraint();
                    }
                }
                return true;
            }
            return false;
        }


        private void ResetCaseSensitive() {
            // this method is used design-time scenarios via reflection
            //   by the property grid context menu to show the Reset option or not
            SetCaseSensitiveValue((null != dataSet) && dataSet.CaseSensitive, true, true);
            _caseSensitiveUserSet = false;
        }

        internal bool ShouldSerializeCaseSensitive() {
            // this method is used design-time scenarios via reflection
            //   by the property grid to show the CaseSensitive property in bold or not
            //   by the code dom for persisting the CaseSensitive property or not
            return _caseSensitiveUserSet;
        }

        internal bool SelfNested {
            get {
                // Is this correct? if ((top[i].nestedParentRelation!= null) && (top[i].nestedParentRelation.ParentTable == top[i]))
                foreach(DataRelation rel in ParentRelations) {
                    if (rel.Nested && rel.ParentTable == this) {
                        return true;
                    }
                }
                return false;
            }
        }
/*        internal bool SelfNestedWithOneRelation {
            get {
                return (this.ParentRelations.Count == 1 && (this.ParentRelations[0].ParentTable == this));
            }
        }
*/

        [DebuggerBrowsable(DebuggerBrowsableState.Never)] // don't have debugger view expand this
        internal List<Index> LiveIndexes {
            get {
                if (!AreIndexEventsSuspended) {
                    for (int i = indexes.Count-1; 0 <= i; --i) {
                        Index index = indexes[i];
                        if (index.RefCount <= 1) {
                            index.RemoveRef();
                        }
                    }
                }
                return indexes;
            }
        }

        [
        DefaultValue(SerializationFormat.Xml)
        ]
        public SerializationFormat RemotingFormat {
            get {
                return _remotingFormat;
            }
            set {
                if (value != SerializationFormat.Binary && value != SerializationFormat.Xml) {
                    throw ExceptionBuilder.InvalidRemotingFormat(value);
                }
                // table can not have different format than its dataset, unless it is stand alone datatable
                if (this.DataSet != null && value != this.DataSet.RemotingFormat) {
                    throw ExceptionBuilder.CanNotSetRemotingFormat();
                }
                _remotingFormat = value;
            }
        }

// used to keep temporary state of unique Key posiotion to be added for inference only
        internal int UKColumnPositionForInference {
            get {
                return ukColumnPositionForInference;
            }
            set{
                ukColumnPositionForInference= value;
            }
        }

        /// <devdoc>
        /// <para>Gets the collection of child relations for this <see cref='System.Data.DataTable'/>.</para>
        /// </devdoc>
        [
        Browsable(false),
        ResDescriptionAttribute(Res.DataTableChildRelationsDescr),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public DataRelationCollection ChildRelations {
            get {
                if (childRelationsCollection == null)
                    childRelationsCollection = new DataRelationCollection.DataTableRelationCollection(this, false);
                return childRelationsCollection;
            }
        }

        /// <devdoc>
        ///    <para>Gets the collection of columns that belong to this table.</para>
        /// </devdoc>
        [
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableColumnsDescr)
        ]
        public DataColumnCollection Columns {
            get {
                return columnCollection;
            }
        }

        private void ResetColumns() {
            // this method is used design-time scenarios via reflection
            //   by the property grid context menu to show the Reset option or not
            Columns.Clear();
        }

        private CompareInfo CompareInfo {
            get {
                if (null == _compareInfo) {
                    _compareInfo = Locale.CompareInfo;
                }
                return _compareInfo;
            }
        }

        /// <devdoc>
        ///    <para>Gets the collection of constraints maintained by this table.</para>
        /// </devdoc>
        [
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableConstraintsDescr)
        ]
        public ConstraintCollection Constraints {
            get {
                return constraintCollection;
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Resets the <see cref='System.Data.DataTable.Constraints'/> property to its default state.
        ///    </para>
        /// </devdoc>
        private void ResetConstraints() {
            Constraints.Clear();
        }

        /// <devdoc>
        /// <para>Gets the <see cref='System.Data.DataSet'/> that this table belongs to.</para>
        /// </devdoc>
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(false), ResDescriptionAttribute(Res.DataTableDataSetDescr)]
        public DataSet DataSet {
            get {
                return dataSet;
            }
        }

        /// <devdoc>
        /// Internal method for setting the DataSet pointer.
        /// </devdoc>
        internal void SetDataSet(DataSet dataSet) {
            if (this.dataSet != dataSet) {
                this.dataSet = dataSet;

                // Inform all the columns of the dataset being set.
                DataColumnCollection   cols = Columns;
                for (int i = 0; i < cols.Count; i++)
                    cols[i].OnSetDataSet();

                if (this.DataSet != null) {
                    defaultView = null;
                }
                //Set the remoting format variable directly
                if (dataSet != null) {
                    _remotingFormat = dataSet.RemotingFormat;
                }
            }
        }

        /// <devdoc>
        ///    <para>Gets a customized view of the table which may include a
        ///       filtered view, or a cursor position.</para>
        /// </devdoc>
        [Browsable(false), ResDescriptionAttribute(Res.DataTableDefaultViewDescr)]
        public DataView DefaultView {
            get {
                DataView view = defaultView;
                if (null == view) {
                    if (null != dataSet) {
                        view = dataSet.DefaultViewManager.CreateDataView(this);
                    }
                    else {
                        view = new DataView(this, true);
                        view.SetIndex2("", DataViewRowState.CurrentRows, null, true);
                    }
                    // avoid HostProtectionAttribute(Synchronization=true) by not calling virtual methods from inside a lock
                    view = Interlocked.CompareExchange<DataView>(ref defaultView, view, null);
                    if (null == view) {
                        view = defaultView;
                    }
                }
                return view;
            }
        }

        /// <devdoc>
        ///    <para>Gets or sets the expression that will return a value used to represent
        ///       this table in UI.</para>
        /// </devdoc>
        [
        DefaultValue(""),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableDisplayExpressionDescr)
        ]
        public string DisplayExpression {
            get {
                return DisplayExpressionInternal;
            }
            set {
                if (value != null && value.Length > 0) {
                    this.displayExpression = new DataExpression(this, value);
                }
                else {
                    this.displayExpression = null;
                }
            }
        }
        internal string DisplayExpressionInternal {
            get {
                return(displayExpression != null ? displayExpression.Expression : "");
            }
        }

        internal bool EnforceConstraints {
            get {
                if (SuspendEnforceConstraints) {
                    return false;
                }
                if (dataSet != null)
                    return dataSet.EnforceConstraints;

                return this.enforceConstraints;
            }
            set {
                if (dataSet == null && this.enforceConstraints != value) {
                    if (value)
                        EnableConstraints();

                    this.enforceConstraints = value;
                }
            }
        }

        internal bool SuspendEnforceConstraints {
            get {
                return _suspendEnforceConstraints ;
            }
            set {
                _suspendEnforceConstraints = value;
            }
        }

        internal void EnableConstraints()
        {
            bool errors = false;
            foreach (Constraint constr in Constraints)
            {
                if (constr is UniqueConstraint)
                    errors |= constr.IsConstraintViolated();
            }

            foreach (DataColumn column in Columns) {
                if (!column.AllowDBNull) {
                    errors |= column.IsNotAllowDBNullViolated();
                }
                if (column.MaxLength >= 0) {
                    errors |= column.IsMaxLengthViolated();
                }
            }

            if (errors) {
                this.EnforceConstraints = false;
                throw ExceptionBuilder.EnforceConstraint();
            }
        }

        /// <devdoc>
        ///    <para>Gets the collection of customized user information.</para>
        /// </devdoc>
        [
        ResCategoryAttribute(Res.DataCategory_Data),
        Browsable(false),
        ResDescriptionAttribute(Res.ExtendedPropertiesDescr)
        ]
        public PropertyCollection ExtendedProperties {
            get {
                if (extendedProperties == null) {
                    extendedProperties = new PropertyCollection();
                }
                return extendedProperties;
            }
        }

        internal IFormatProvider FormatProvider {
            get {
                // used for Formating/Parsing
                // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemglobalizationcultureinfoclassisneutralculturetopic.asp
                if (null == _formatProvider) {
                    CultureInfo culture = Locale;
                    if (culture.IsNeutralCulture) {
                        culture = CultureInfo.InvariantCulture;
                    }
                    _formatProvider = (IFormatProvider)culture;
                }
                return _formatProvider;
            }
        }

        /// <devdoc>
        ///    <para>Gets a value indicating whether there are errors in any of the rows in any of
        ///       the tables of the <see cref='System.Data.DataSet'/> to which the table belongs.</para>
        /// </devdoc>
        [Browsable(false), ResDescriptionAttribute(Res.DataTableHasErrorsDescr)]
        public bool HasErrors {
            get {
                for (int i = 0; i < Rows.Count; i++) {
                    if (Rows[i].HasErrors) {
                        return true;
                    }
                }
                return false;
            }
        }

        /// <devdoc>
        ///    <para>Gets or sets the locale information used to compare strings within the table.</para>
        ///    <para>Also used for locale sensitive, case,kana,width insensitive column name lookups</para>
        ///    <para>Also used for converting values to and from string</para>
        /// </devdoc>
        [ResDescriptionAttribute(Res.DataTableLocaleDescr)]
        public CultureInfo Locale {
            get {
                // used for Comparing not Formatting/Parsing
                Debug.Assert(null != _culture, "null culture");
                Debug.Assert(_cultureUserSet || (null == dataSet) || _culture.Equals(dataSet.Locale), "Locale mismatch");
                return _culture;
            }
            set {
                IntPtr hscp;
                Bid.ScopeEnter(out hscp, "<ds.DataTable.set_Locale|API> %d#\n", ObjectID);
                try {
                    bool userSet = true;
                    if (null == value)  {
                        // reset Locale to inherit from DataSet
                        userSet = false;
                        value = (null != dataSet) ? dataSet.Locale : _culture;
                    }
                    if (_culture != value && !_culture.Equals(value)) {
                        bool flag = false;
                        bool exceptionThrown = false;
                        CultureInfo oldLocale = _culture;
                        bool oldUserSet = _cultureUserSet;
                        try {
                            _cultureUserSet = true;
                            SetLocaleValue(value, true, false);
                            if ((null == DataSet) || DataSet.ValidateLocaleConstraint()) {
                                flag = false;
                                SetLocaleValue(value, true, true);
                                flag = true;
                            }
                        }
                        catch {
                            exceptionThrown = true;
                            throw;
                        }
                        finally {
                            if (!flag) { // reset old locale if ValidationFailed or exception thrown
                                try {
                                    SetLocaleValue(oldLocale, true, true);
                                }
                                catch(Exception e) { // failed to reset all indexes for all constraints
                                    if (!Common.ADP.IsCatchableExceptionType(e)) {
                                        throw;
                                    }
                                    Common.ADP.TraceExceptionWithoutRethrow(e);
                                }
                                _cultureUserSet = oldUserSet;
                                if (!exceptionThrown) {
                                    throw ExceptionBuilder.CannotChangeCaseLocale(null);
                                }
                            }
                        }
                        SetLocaleValue(value, true, true);
                    }
                    _cultureUserSet = userSet;
                }
                finally{
                    Bid.ScopeLeave(ref hscp);
                }
            }
        }

        internal bool SetLocaleValue(CultureInfo culture, bool userSet, bool resetIndexes) {
            Debug.Assert(null != culture, "SetLocaleValue: no locale");
            if (userSet || resetIndexes || (!_cultureUserSet && !_culture.Equals(culture))) {
                _culture = culture;
                _compareInfo = null;
                _formatProvider = null;
                _hashCodeProvider = null;

                foreach(DataColumn column in Columns) {
                    column._hashCode = GetSpecialHashCode(column.ColumnName);
                }
                if (resetIndexes) {
                    ResetIndexes();
                    foreach (Constraint constraint in Constraints) {
                        constraint.CheckConstraint();
                    }
                }
                return true;
            }
            return false;
        }

        internal bool ShouldSerializeLocale() {
            // this method is used design-time scenarios via reflection
            //   by the property grid to show the Locale property in bold or not
            //   by the code dom for persisting the Locale property or not

            // we always want the locale persisted if set by user or different the current thread if standalone table
            // but that logic should by performed by the serializion code
            return _cultureUserSet;
        }

        /// <devdoc>
        ///    <para>Gets or sets the initial starting size for this table.</para>
        /// </devdoc>
        [
        DefaultValue(50),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableMinimumCapacityDescr)
        ]
        public int MinimumCapacity {
            get {
                return recordManager.MinimumCapacity;
            }
            set {
                if (value != recordManager.MinimumCapacity) {
                    recordManager.MinimumCapacity = value;
                }
            }
        }

        internal int RecordCapacity {
            get {
                return recordManager.RecordCapacity;
            }
        }


        internal int ElementColumnCount {
            get {
                return elementColumnCount;
            }
            set {
                if ((value > 0) && (xmlText != null))
                    throw ExceptionBuilder.TableCannotAddToSimpleContent();
                else elementColumnCount = value;
            }
        }

        /// <devdoc>
        /// <para>Gets the collection of parent relations for this <see cref='System.Data.DataTable'/>.</para>
        /// </devdoc>
        [
        Browsable(false),
        ResDescriptionAttribute(Res.DataTableParentRelationsDescr),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
        ]
        public DataRelationCollection ParentRelations {
            get {
                if (parentRelationsCollection == null)
                    parentRelationsCollection = new DataRelationCollection.DataTableRelationCollection(this, true);
                return parentRelationsCollection;
            }
        }

        internal bool MergingData {
            get {
                return mergingData;
            }
            set {
                mergingData = value;
            }
        }

        internal DataRelation[] NestedParentRelations {
            get {
#if DEBUG
                DataRelation[] nRel = FindNestedParentRelations();
                Debug.Assert(nRel.Length == _nestedParentRelations.Length, "nestedParent cache is broken");
                for(int i = 0; i < nRel.Length; i++) {
                    Debug.Assert(null != nRel[i], "null relation");
                    Debug.Assert(null != _nestedParentRelations[i], "null relation");
                    Debug.Assert(nRel[i] == _nestedParentRelations[i], "unequal relations");
                }
#endif
                return _nestedParentRelations;
            }
        }

        internal bool SchemaLoading {
            get {
                return schemaLoading;
            }
        }


        internal void CacheNestedParent() {
            _nestedParentRelations = FindNestedParentRelations();
        }

        private DataRelation[] FindNestedParentRelations() {
            List<DataRelation> nestedParents = null;
            foreach(DataRelation relation in this.ParentRelations) {
                if(relation.Nested) {
                    if (null == nestedParents) {
                        nestedParents = new List<DataRelation>();
                    }
                    nestedParents.Add(relation);
                }
            }
            if ((null == nestedParents) || (nestedParents.Count == 0)) {
                return EmptyArrayDataRelation;
            }
            return nestedParents.ToArray();
        }


        internal int NestedParentsCount {
            get {
                int count = 0;
                foreach(DataRelation relation in this.ParentRelations) {
                    if(relation.Nested)
                        count++;
                }
                return count;
            }
        }

        /// <devdoc>
        ///    <para>Gets or sets an array of columns that function as primary keys for the data
        ///       table.</para>
        /// </devdoc>
        [
        TypeConverter(typeof(PrimaryKeyTypeConverter)),
        ResDescriptionAttribute(Res.DataTablePrimaryKeyDescr),
        ResCategoryAttribute(Res.DataCategory_Data),
        Editor("Microsoft.VSDesigner.Data.Design.PrimaryKeyEditor, " + AssemblyRef.MicrosoftVSDesigner, "System.Drawing.Design.UITypeEditor, " + AssemblyRef.SystemDrawing)
        ]
        public DataColumn[] PrimaryKey {
            get {
                UniqueConstraint primayKeyConstraint = primaryKey;
                if (null != primayKeyConstraint) {
                    Debug.Assert(2 <= primaryKey.ConstraintIndex.RefCount, "bad primaryKey index RefCount");
                    return primayKeyConstraint.Key.ToArray();
                }
                return zeroColumns;
            }
            set {
                UniqueConstraint key = null;
                UniqueConstraint existingKey = null;

                // Loading with persisted property
                if (fInitInProgress && value != null) {
                    delayedSetPrimaryKey = value;
                    return;
                }

                if ((value != null) && (value.Length != 0)) {
                    int count = 0;
                    for (int i = 0; i < value.Length; i++) {
                        if (value[i] != null)
                            count++;
                        else
                            break;
                    }

                    if (count != 0) {
                        DataColumn[] newValue = value;
                        if (count != value.Length) {
                            newValue = new DataColumn[count];
                            for (int i = 0; i < count; i++)
                                newValue[i] = value[i];
                        }
                        key = new UniqueConstraint(newValue);
                        if (key.Table != this)
                            throw ExceptionBuilder.TableForeignPrimaryKey();
                    }
                }

                if (key == primaryKey || (key != null && key.Equals(primaryKey)))
                    return;

                // Use an existing UniqueConstraint that matches if one exists
                if ((existingKey = (UniqueConstraint)Constraints.FindConstraint(key)) != null) {
                    key.ColumnsReference.CopyTo(existingKey.Key.ColumnsReference, 0);
                    key = existingKey;
                }

                UniqueConstraint oldKey = primaryKey;
                primaryKey = null;
                if (oldKey != null) {
                    oldKey.ConstraintIndex.RemoveRef();

                    // SQLBU 429176: if PrimaryKey is removed, reset LoadDataRow indexes
                    if (null != loadIndex) {
                        loadIndex.RemoveRef();
                        loadIndex = null;
                    }
                    if (null != loadIndexwithOriginalAdded) {
                        loadIndexwithOriginalAdded.RemoveRef();
                        loadIndexwithOriginalAdded = null;
                    }
                    if (null != loadIndexwithCurrentDeleted) {
                        loadIndexwithCurrentDeleted.RemoveRef();
                        loadIndexwithCurrentDeleted = null;
                    }
                    Constraints.Remove(oldKey);
                }

                // Add the key if there isnt an existing matching key in collection
                if (key != null && existingKey == null)
                    Constraints.Add(key);

                primaryKey = key;

                Debug.Assert(Constraints.FindConstraint(primaryKey) == primaryKey, "PrimaryKey is not in ConstraintCollection");
                _primaryIndex = (key != null) ? key.Key.GetIndexDesc() : zeroIndexField;

                if (primaryKey != null) {
                    // must set index for DataView.Sort before setting AllowDBNull which can fail
                    key.ConstraintIndex.AddRef();

                    for (int i = 0; i < key.ColumnsReference.Length; i++)
                        key.ColumnsReference[i].AllowDBNull = false;
                }
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Indicates whether the <see cref='System.Data.DataTable.PrimaryKey'/> property should be persisted.
        ///    </para>
        /// </devdoc>
        private bool ShouldSerializePrimaryKey() {
            return(primaryKey != null);
        }

        /// <devdoc>
        ///    <para>
        ///       Resets the <see cref='System.Data.DataTable.PrimaryKey'/> property to its default state.
        ///    </para>
        /// </devdoc>
        private void ResetPrimaryKey() {
            PrimaryKey = null;
        }

        /// <devdoc>
        ///    <para>Gets the collection of rows that belong to this table.</para>
        /// </devdoc>
        [Browsable(false), ResDescriptionAttribute(Res.DataTableRowsDescr)]
        public DataRowCollection Rows {
            get {
                return rowCollection;
            }
        }

        /// <devdoc>
        ///    <para>Gets or sets the name of the table.</para>
        /// </devdoc>
        [
        RefreshProperties(RefreshProperties.All),
        DefaultValue(""),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableTableNameDescr)
        ]
        public string TableName {
            get {
                return tableName;
            }
            set {
                IntPtr hscp;
                Bid.ScopeEnter(out hscp, "<ds.DataTable.set_TableName|API> %d#, value='%ls'\n", ObjectID, value);
                try {
                    if (value == null) {
                        value = "";
                    }
                    CultureInfo currentLocale = this.Locale;
                    if (String.Compare(tableName, value, true, currentLocale) != 0) {
                        if (dataSet != null) {
                            if (value.Length == 0)
                                throw ExceptionBuilder.NoTableName();
                            if ((0 == String.Compare(value, dataSet.DataSetName, true, dataSet.Locale)) && !fNestedInDataset)
                               throw ExceptionBuilder.DatasetConflictingName(dataSet.DataSetName);

                            DataRelation[] nestedRelations = NestedParentRelations;
                            if (nestedRelations.Length == 0) {
                                dataSet.Tables.RegisterName(value, this.Namespace);
                            }
                            else {
                                foreach(DataRelation rel in nestedRelations) {
                                    if (!rel.ParentTable.Columns.CanRegisterName(value)) {
                                        throw ExceptionBuilder.CannotAddDuplicate2(value);
                                    }
                                }
                                // if it cannot register the following line will throw exception
                                dataSet.Tables.RegisterName(value, this.Namespace);

                                foreach(DataRelation rel in nestedRelations) {
                                    rel.ParentTable.Columns.RegisterColumnName(value, null);
                                    rel.ParentTable.Columns.UnregisterName(this.TableName);
                                }
                            }

                            if (tableName.Length != 0) {
                                dataSet.Tables.UnregisterName(tableName);
                            }
                        }
                        RaisePropertyChanging("TableName");
                        tableName = value;
                        encodedTableName = null;
                    }
                    else if (String.Compare(tableName, value, false, currentLocale) != 0) {
                        RaisePropertyChanging("TableName");
                        tableName = value;
                        encodedTableName = null;
                    }
                }
                finally {
                    Bid.ScopeLeave(ref hscp);
                }
            }
        }


        internal string EncodedTableName {
            get {
                string encodedTblName = this.encodedTableName;
                if (null == encodedTblName) {
                    encodedTblName = XmlConvert.EncodeLocalName( this.TableName );
                    this.encodedTableName = encodedTblName;
                }
                return encodedTblName;
            }
        }
        private string GetInheritedNamespace(List<DataTable> visitedTables){
            // if there is nested relation: ie: this table is nested child of a another table and
            // if it is not self nested, return parent tables NS: Meanwhile make sure SQLBUDT 240219 is FIXED
            DataRelation[] nestedRelations = NestedParentRelations;
            if (nestedRelations.Length > 0) {
                for(int i =0; i < nestedRelations.Length; i++) {
                    DataRelation rel = nestedRelations[i];
                    if (rel.ParentTable.tableNamespace != null) {
                        return rel.ParentTable.tableNamespace; // if parent table has a non-null NS, return it
                    }
                }
                // Assumption, in hierarchy of multiple nested relation, a child table with no NS, has DataRelation
                // only and only with parent DataTable witin the same namespace
                int j = 0;
                while(j < nestedRelations.Length &&((nestedRelations[j].ParentTable == this)||(visitedTables.Contains(nestedRelations[j].ParentTable)))) {
                    j++;
                }
                if (j < nestedRelations.Length) {
                    DataTable parentTable = nestedRelations[j].ParentTable;
                    if (!visitedTables.Contains(parentTable))
                        visitedTables.Add(parentTable);
                        return parentTable.GetInheritedNamespace(visitedTables);// this is the same as return parentTable.Namespace
                }
            } // dont put else
            if (DataSet != null) { // if it cant return from parent tables, return NS from dataset, if exists
                return  DataSet.Namespace;
            }
            else {
                return string.Empty;
            }

        }

        /// <devdoc>
        ///    <para>
        ///       Gets or sets the namespace for the <see cref='System.Data.DataTable'/>.
        ///    </para>
        /// </devdoc>
        [
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableNamespaceDescr)
        ]
        public string Namespace {
            get {
                if (tableNamespace == null) {
                    return GetInheritedNamespace(new List<DataTable>());
                }
                return tableNamespace;
            }
            set {
                IntPtr hscp;
                Bid.ScopeEnter(out hscp, "<ds.DataTable.set_Namespace|API> %d#, value='%ls'\n", ObjectID, value);
                try {
                    if(value != tableNamespace) {
                        if (dataSet != null) {
                            string realNamespace = (value == null ? GetInheritedNamespace(new List<DataTable>()) : value);
                            if (realNamespace != Namespace) {
                                // do this extra check only if the namespace is really going to change
                                // inheritance-wise.
                                if (dataSet.Tables.Contains( this.TableName, realNamespace, true, true))
                                    throw ExceptionBuilder.DuplicateTableName2(this.TableName, realNamespace);

                                CheckCascadingNamespaceConflict(realNamespace);
                            }
                        }
                        CheckNamespaceValidityForNestedRelations(value);
                        DoRaiseNamespaceChange();
                    }
                    tableNamespace = value;
                }
                finally{
                    Bid.ScopeLeave(ref hscp);
                }
            }
        }
        internal bool IsNamespaceInherited() {
            return (null == tableNamespace);
        }

        internal void CheckCascadingNamespaceConflict(string realNamespace){
            foreach (DataRelation rel in ChildRelations)
                if ((rel.Nested) && (rel.ChildTable != this) && (rel.ChildTable.tableNamespace == null)) {
                    DataTable childTable = rel.ChildTable;
                    if (dataSet.Tables.Contains( childTable.TableName, realNamespace, false, true))
                        throw ExceptionBuilder.DuplicateTableName2(this.TableName, realNamespace);

                    childTable.CheckCascadingNamespaceConflict(realNamespace);
                }

        }

        internal void CheckNamespaceValidityForNestedRelations(string realNamespace){
            foreach(DataRelation rel in ChildRelations) {
                if (rel.Nested) {
                    if (realNamespace != null) {
                        rel.ChildTable.CheckNamespaceValidityForNestedParentRelations(realNamespace, this);
                    }
                    else{
                        rel.ChildTable.CheckNamespaceValidityForNestedParentRelations(GetInheritedNamespace(new List<DataTable>()), this);
                    }
                }
            }
            if (realNamespace == null) { // this will affect this table if it has parent relations
                this.CheckNamespaceValidityForNestedParentRelations(GetInheritedNamespace(new List<DataTable>()), this);
            }

        }
        internal void CheckNamespaceValidityForNestedParentRelations(string ns, DataTable parentTable) {
            foreach(DataRelation rel in ParentRelations){
                if (rel.Nested) {
                    if (rel.ParentTable != parentTable && rel.ParentTable.Namespace != ns) {
                        throw ExceptionBuilder.InValidNestedRelation(this.TableName);
                    }
                }
            }

        }

        internal void DoRaiseNamespaceChange(){
            RaisePropertyChanging("Namespace");
            // raise column Namespace change

            foreach (DataColumn col in Columns)
                if (col._columnUri == null)
                    col.RaisePropertyChanging("Namespace");

            foreach (DataRelation rel in ChildRelations)
                if ((rel.Nested) && (rel.ChildTable != this)) {
                    DataTable childTable = rel.ChildTable;

                    rel.ChildTable.DoRaiseNamespaceChange();
                }
        }
        /// <devdoc>
        ///    <para>
        ///       Indicates whether the <see cref='System.Data.DataTable.Namespace'/> property should be persisted.
        ///    </para>
        /// </devdoc>
        private bool ShouldSerializeNamespace() {
            return(tableNamespace != null);
        }

        /// <devdoc>
        ///    <para>
        ///       Resets the <see cref='System.Data.DataTable.Namespace'/> property to its default state.
        ///    </para>
        /// </devdoc>
        private void ResetNamespace() {
            this.Namespace = null;
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        virtual public void BeginInit() {
            fInitInProgress = true;
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        virtual public void EndInit() {
            if (dataSet == null || !dataSet.fInitInProgress) {
                Columns.FinishInitCollection();
                Constraints.FinishInitConstraints();
                foreach(DataColumn dc in Columns){
                    if (dc.Computed) {
                        dc.Expression = dc.Expression;
                    }
                }
            }
            fInitInProgress = false; // [....] : 77890. It is must that we set off this flag after calling FinishInitxxx();
            if (delayedSetPrimaryKey != null) {
                PrimaryKey = delayedSetPrimaryKey;
                delayedSetPrimaryKey = null;
            }
            if (delayedViews.Count > 0) {
                foreach(DataView dv in delayedViews) {
                    dv.EndInit();
                }
                delayedViews.Clear();
            }
            OnInitialized();
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [
        DefaultValue(""),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTablePrefixDescr)
        ]
        public string Prefix {
            get { return tablePrefix;}
            set {
                if (value == null) {
                    value = "";
                }
                Bid.Trace("<ds.DataTable.set_Prefix|API> %d#, value='%ls'\n", ObjectID, value);
                if ((XmlConvert.DecodeName(value) == value) &&
                    (XmlConvert.EncodeName(value) != value))
                    throw ExceptionBuilder.InvalidPrefix(value);


                tablePrefix = value;
            }
        }

        internal DataColumn XmlText {
            get {
                return xmlText;
            }
            set {
                if (xmlText != value) {
                    if (xmlText != null) {
                        if (value != null) {
                            throw ExceptionBuilder.MultipleTextOnlyColumns();
                        }
                        Columns.Remove(xmlText);
                    }
                    else {
                        Debug.Assert(value != null, "Value shoud not be null ??");
                        Debug.Assert(value.ColumnMapping == MappingType.SimpleContent, "should be text node here");
                        if (value != Columns[value.ColumnName])
                            Columns.Add(value);
                    }
                    xmlText = value;
                }
            }
        }

        internal decimal MaxOccurs {
            get {
                return maxOccurs;
            }
            set {
                maxOccurs = value;
            }
        }

        internal decimal MinOccurs {
            get {
                return minOccurs;
            }
            set {
                minOccurs = value;
            }
        }

        internal void SetKeyValues(DataKey key, object[] keyValues, int record) {
            for (int i = 0; i < keyValues.Length; i++) {
                key.ColumnsReference[i][record] = keyValues[i];
            }
        }

        internal DataRow FindByIndex(Index ndx, object[] key) {
            Range range = ndx.FindRecords(key);
            if (range.IsNull) {
                return null;
            }
            return this.recordManager[ndx.GetRecord(range.Min)];
        }

        internal DataRow FindMergeTarget(DataRow row, DataKey key, Index ndx) {
            DataRow targetRow = null;

            // Primary key match
            if (key.HasValue) {
                Debug.Assert(ndx != null);
                int   findRecord = (row.oldRecord == -1) ? row.newRecord : row.oldRecord;
                object[] values = key.GetKeyValues(findRecord);
                targetRow = FindByIndex(ndx, values);
            }
            return targetRow;
        }

        private void SetMergeRecords(DataRow row, int newRecord, int oldRecord, DataRowAction action) {
            if (newRecord != -1) {
                SetNewRecord(row, newRecord, action, true, true);
                SetOldRecord(row, oldRecord);
            }
            else {
                SetOldRecord(row, oldRecord);
                if (row.newRecord != -1) {
                    Debug.Assert(action == DataRowAction.Delete, "Unexpected SetNewRecord action in merge function.");
                    SetNewRecord(row, newRecord, action, true, true);
                }
            }
        }

        internal DataRow MergeRow(DataRow row, DataRow targetRow, bool preserveChanges, Index idxSearch) {
             if (targetRow == null) {
                targetRow = this.NewEmptyRow();
                targetRow.oldRecord = recordManager.ImportRecord(row.Table, row.oldRecord);
                targetRow.newRecord = targetRow.oldRecord;
                if(row.oldRecord != row.newRecord) {
                    targetRow.newRecord = recordManager.ImportRecord(row.Table, row.newRecord);
                }
                InsertRow(targetRow, -1);
            }
            else {
                // SQLBU 500789: Record Manager corruption during Merge when target row in edit state
                // the newRecord would be freed and overwrite tempRecord (which became the newRecord)
                // this would leave the DataRow referencing a freed record and leaking memory for the now lost record
                int proposedRecord = targetRow.tempRecord; // by saving off the tempRecord, EndEdit won't free newRecord
                targetRow.tempRecord = -1;
                try {
                    DataRowState saveRowState = targetRow.RowState;
                    int saveIdxRecord = (saveRowState == DataRowState.Added) ? targetRow.newRecord : saveIdxRecord = targetRow.oldRecord;
                     int newRecord;
                     int oldRecord;
                    if (targetRow.RowState == DataRowState.Unchanged && row.RowState == DataRowState.Unchanged) {
                        // unchanged row merging with unchanged row
                        oldRecord = targetRow.oldRecord;
                        newRecord = (preserveChanges) ? recordManager.CopyRecord(this, oldRecord, -1) : targetRow.newRecord;
                        oldRecord = recordManager.CopyRecord(row.Table, row.oldRecord, targetRow.oldRecord);
                        SetMergeRecords(targetRow, newRecord, oldRecord, DataRowAction.Change);
                    }
                    else if (row.newRecord == -1) {
                        // Incoming row is deleted
                        oldRecord = targetRow.oldRecord;
                        if (preserveChanges) {
                          newRecord = (targetRow.RowState == DataRowState.Unchanged)? recordManager.CopyRecord(this, oldRecord, -1) : targetRow.newRecord;
                        }
                        else
                            newRecord = -1;
                        oldRecord = recordManager.CopyRecord(row.Table, row.oldRecord, oldRecord);

                        // Change index record, need to update index
                        if (saveIdxRecord != ((saveRowState == DataRowState.Added) ? newRecord : oldRecord)) {
                            SetMergeRecords(targetRow, newRecord, oldRecord, (newRecord == -1) ? DataRowAction.Delete : DataRowAction.Change);
                            idxSearch.Reset();
                            saveIdxRecord = ((saveRowState == DataRowState.Added) ? newRecord : oldRecord);
                        } else {
                            SetMergeRecords(targetRow, newRecord, oldRecord, (newRecord == -1) ? DataRowAction.Delete : DataRowAction.Change);
                        }
                    }
                    else {
                        // incoming row is added, modified or unchanged (targetRow is not unchanged)
                        oldRecord = targetRow.oldRecord;
                        newRecord = targetRow.newRecord;
                        if (targetRow.RowState == DataRowState.Unchanged) {
                            newRecord = recordManager.CopyRecord(this, oldRecord, -1);
                        }
                        oldRecord = recordManager.CopyRecord(row.Table, row.oldRecord, oldRecord);

                        if (!preserveChanges) {
                            newRecord = recordManager.CopyRecord(row.Table, row.newRecord, newRecord);
                        }
                        SetMergeRecords(targetRow, newRecord, oldRecord, DataRowAction.Change);
                    }

                    if (saveRowState == DataRowState.Added && targetRow.oldRecord != -1)
                        idxSearch.Reset();
                    Debug.Assert(saveIdxRecord == ((saveRowState == DataRowState.Added) ? targetRow.newRecord : targetRow.oldRecord), "oops, you change index record without noticing it");
                }
                finally {
                    targetRow.tempRecord = proposedRecord;
                }
            }

            // Merge all errors
            if (row.HasErrors) {
                if (targetRow.RowError.Length == 0) {
                    targetRow.RowError = row.RowError;
                } else {
                    targetRow.RowError += " ]:[ " + row.RowError;
                }
                DataColumn[] cols = row.GetColumnsInError();

                for (int i = 0; i < cols.Length; i++) {
                    DataColumn col = targetRow.Table.Columns[cols[i].ColumnName];
                    targetRow.SetColumnError(col, row.GetColumnError(cols[i]));
                }
            }else {
                if (!preserveChanges) {
                    targetRow.ClearErrors();
                }
            }

            return targetRow;
        }

        /// <devdoc>
        /// <para>Commits all the changes made to this table since the last time <see cref='System.Data.DataTable.AcceptChanges'/> was called.</para>
        /// </devdoc>
        public void AcceptChanges() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.AcceptChanges|API> %d#\n", ObjectID);
            try {
                DataRow[] oldRows = new DataRow[Rows.Count];
                Rows.CopyTo(oldRows, 0);

                // delay updating of indexes until after all
                // AcceptChange calls have been completed
                SuspendIndexEvents();
                try {
                    for (int i = 0; i < oldRows.Length; ++i) {
                        if (oldRows[i].rowID != -1) {
                            oldRows[i].AcceptChanges();
                        }
                    }
                }
                finally {
                    RestoreIndexEvents(false);
                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
             }
        }

        // Prevent inlining so that reflection calls are not moved to caller that may be in a different assembly that may have a different grant set.
        [MethodImpl(MethodImplOptions.NoInlining)] 
        protected virtual DataTable CreateInstance() {
            return (DataTable) Activator.CreateInstance(this.GetType(), true);
        }

        public virtual DataTable Clone() {
            return Clone(null);
        }

        internal DataTable Clone(DataSet cloneDS) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Clone|INFO> %d#, cloneDS=%d\n", ObjectID, (cloneDS != null) ? cloneDS.ObjectID : 0);
            try {
                DataTable clone = CreateInstance();
                if (clone.Columns.Count > 0) // [....] : To clean up all the schema in strong typed dataset.
                    clone.Reset();
                return CloneTo(clone, cloneDS, false);
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }


        private DataTable IncrementalCloneTo (DataTable sourceTable, DataTable targetTable) {
            foreach(DataColumn dc in sourceTable.Columns) {
                if (targetTable.Columns[dc.ColumnName] == null) {
                    targetTable.Columns.Add(dc.Clone());
                }
            }

            return targetTable;
        }

        private DataTable CloneHierarchy (DataTable sourceTable, DataSet ds, Hashtable visitedMap) {
            if (visitedMap == null)
                visitedMap = new Hashtable();
            if (visitedMap.Contains(sourceTable))
                return ((DataTable)visitedMap[sourceTable]);


            DataTable destinationTable = ds.Tables[sourceTable.TableName, sourceTable.Namespace];

            if ((destinationTable != null && destinationTable.Columns.Count > 0)) {
                destinationTable = IncrementalCloneTo(sourceTable,destinationTable);
                   // get extra columns from source into destination , increamental read
            }
            else {
                if (destinationTable == null) {
                    destinationTable = new DataTable();
                    // fxcop: new DataTable values for CaseSensitive, Locale, Namespace will come from CloneTo
                    ds.Tables.Add(destinationTable);
                }
                destinationTable = sourceTable.CloneTo(destinationTable, ds, true);
            }
            visitedMap[sourceTable] = destinationTable;


            // start cloning relation
            foreach( DataRelation r in sourceTable.ChildRelations ) {
                DataTable childTable = CloneHierarchy((DataTable)r.ChildTable, ds, visitedMap);
             }

            return destinationTable;
         }


        private DataTable CloneTo(DataTable clone, DataSet cloneDS, bool skipExpressionColumns) {
// we do clone datatables while we do readxmlschema, so we do not want to clone columnexpressions if we call this from ReadXmlSchema
// it will cause exception to be thrown in cae expression refers to a table that is not in hirerachy or not created yet
            Debug.Assert(clone != null, "The table passed in has to be newly created empty DataTable.");

            // set All properties
            clone.tableName = tableName;

            clone.tableNamespace = tableNamespace;
            clone.tablePrefix = tablePrefix;
            clone.fNestedInDataset = fNestedInDataset;

            clone._culture = _culture;
            clone._cultureUserSet = _cultureUserSet;
            clone._compareInfo = _compareInfo;
            clone._compareFlags = _compareFlags;
            clone._formatProvider = _formatProvider;
            clone._hashCodeProvider = _hashCodeProvider;
            clone._caseSensitive = _caseSensitive;
            clone._caseSensitiveUserSet = _caseSensitiveUserSet;

            clone.displayExpression = displayExpression;
            clone.typeName = typeName; //[....]
            clone.repeatableElement = repeatableElement; //[....]
            clone.MinimumCapacity = MinimumCapacity;
            clone.RemotingFormat = RemotingFormat;
//            clone.SerializeHierarchy = SerializeHierarchy;

            // add all columns
            DataColumnCollection clmns = this.Columns;
            for (int i = 0; i < clmns.Count; i++) {
                clone.Columns.Add(clmns[i].Clone());
            }

            // add all expressions if Clone is invoked only on DataTable otherwise DataSet.Clone will assign expressions after creating all relationships.
            if (!skipExpressionColumns && cloneDS == null) {
                for (int i = 0; i < clmns.Count; i++) {
                    clone.Columns[clmns[i].ColumnName].Expression = clmns[i].Expression;
                }
            }

            // Create PrimaryKey
            DataColumn[] pkey = PrimaryKey;
            if (pkey.Length > 0) {
                DataColumn[] key = new DataColumn[pkey.Length];
                for (int i = 0; i < pkey.Length; i++) {
                    key[i] = clone.Columns[pkey[i].Ordinal];
                }
                clone.PrimaryKey = key;
            }

            // now clone all unique constraints
            // Rename first
            for (int j = 0; j < Constraints.Count; j++)  {
                ForeignKeyConstraint foreign = Constraints[j] as ForeignKeyConstraint;
                UniqueConstraint unique = Constraints[j] as UniqueConstraint;
                if (foreign  != null) {
                    if (foreign.Table == foreign.RelatedTable) {
                        ForeignKeyConstraint clonedConstraint = foreign.Clone(clone);
                        Constraint oldConstraint = clone.Constraints.FindConstraint(clonedConstraint);
                        if (oldConstraint != null) {
                            oldConstraint.ConstraintName = Constraints[j].ConstraintName;
                        }
                    }
                }
                else if (unique != null) {
                    UniqueConstraint clonedConstraint = unique.Clone(clone);
                    Constraint oldConstraint = clone.Constraints.FindConstraint(clonedConstraint);
                    if (oldConstraint != null) {
                        oldConstraint.ConstraintName = Constraints[j].ConstraintName;
                        foreach (Object key in clonedConstraint.ExtendedProperties.Keys) {
                            oldConstraint.ExtendedProperties[key] = clonedConstraint.ExtendedProperties[key];
                        }
                    }
                }
            }

            // then add
            for (int j = 0; j < Constraints.Count; j++)  {
                if (! clone.Constraints.Contains(Constraints[j].ConstraintName, true)) {
                    ForeignKeyConstraint foreign = Constraints[j] as ForeignKeyConstraint;
                    UniqueConstraint unique = Constraints[j] as UniqueConstraint;
                    if (foreign  != null) {
                        if (foreign.Table == foreign.RelatedTable) {
                            ForeignKeyConstraint newforeign = foreign.Clone(clone);
                            if (newforeign != null) { // we cant make sure that we recieve a cloned FKC,since it depends if table and relatedtable be the same
                                clone.Constraints.Add(newforeign);
                            }
                        }
                    }
                    else if (unique != null) {
                        clone.Constraints.Add(unique.Clone(clone));
                    }
                 }
            }

            // ...Extended Properties...

            if (this.extendedProperties != null) {
                foreach(Object key in this.extendedProperties.Keys) {
                    clone.ExtendedProperties[key]=this.extendedProperties[key];
                }
            }

            return clone;
        }


        public DataTable Copy(){
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Copy|API> %d#\n", ObjectID);
            try {
                DataTable destTable = this.Clone();

                foreach (DataRow row in Rows)
                    CopyRow(destTable, row);

                return destTable;
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        /// <devdoc>
        ///    <para>Occurs when a value has been submitted for this column.</para>
        /// </devdoc>
        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableColumnChangingDescr)]
        public event DataColumnChangeEventHandler ColumnChanging {
            add {
                Bid.Trace("<ds.DataTable.add_ColumnChanging|API> %d#\n", ObjectID);
                onColumnChangingDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_ColumnChanging|API> %d#\n", ObjectID);
                onColumnChangingDelegate -= value;
            }
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableColumnChangedDescr)]
        public event DataColumnChangeEventHandler ColumnChanged {
            add  {
                Bid.Trace("<ds.DataTable.add_ColumnChanged|API> %d#\n", ObjectID);
                onColumnChangedDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_ColumnChanged|API> %d#\n", ObjectID);
                onColumnChangedDelegate -= value;
            }
        }

        [
            ResCategoryAttribute(Res.DataCategory_Action),
            ResDescriptionAttribute(Res.DataSetInitializedDescr)
        ]
        public event System.EventHandler  Initialized {
            add {
                onInitialized += value;
            }
            remove {
                onInitialized -= value;
            }
        }

        internal event PropertyChangedEventHandler PropertyChanging {
            add {
                Bid.Trace("<ds.DataTable.add_PropertyChanging|INFO> %d#\n", ObjectID);
                onPropertyChangingDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_PropertyChanging|INFO> %d#\n", ObjectID);
                onPropertyChangingDelegate -= value;
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Occurs after a row in the table has been successfully edited.
        ///    </para>
        /// </devdoc>
        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowChangedDescr)]
        public event DataRowChangeEventHandler RowChanged {
            add {
                Bid.Trace("<ds.DataTable.add_RowChanged|API> %d#\n", ObjectID);
                onRowChangedDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_RowChanged|API> %d#\n", ObjectID);
                onRowChangedDelegate -= value;
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Occurs when the <see cref='System.Data.DataRow'/> is changing.
        ///    </para>
        /// </devdoc>
        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowChangingDescr)]
        public event DataRowChangeEventHandler RowChanging {
            add {
                Bid.Trace("<ds.DataTable.add_RowChanging|API> %d#\n", ObjectID);
                onRowChangingDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_RowChanging|API> %d#\n", ObjectID);
                onRowChangingDelegate -= value;
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Occurs before a row in the table is
        ///       about to be deleted.
        ///    </para>
        /// </devdoc>
        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowDeletingDescr)]
        public event DataRowChangeEventHandler RowDeleting {
            add {
                Bid.Trace("<ds.DataTable.add_RowDeleting|API> %d#\n", ObjectID);
                onRowDeletingDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_RowDeleting|API> %d#\n", ObjectID);
                onRowDeletingDelegate -= value;
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Occurs after a row in the
        ///       table has been deleted.
        ///    </para>
        /// </devdoc>
        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowDeletedDescr)]
        public event DataRowChangeEventHandler RowDeleted {
            add {
                Bid.Trace("<ds.DataTable.add_RowDeleted|API> %d#\n", ObjectID);
                onRowDeletedDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_RowDeleted|API> %d#\n", ObjectID);
                onRowDeletedDelegate -= value;
            }
        }

        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowsClearingDescr)]
        public event DataTableClearEventHandler TableClearing {
            add {
                Bid.Trace("<ds.DataTable.add_TableClearing|API> %d#\n", ObjectID);
                onTableClearingDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_TableClearing|API> %d#\n", ObjectID);
                onTableClearingDelegate -= value;
            }
        }

        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowsClearedDescr)]
        public event DataTableClearEventHandler TableCleared {
            add {
                Bid.Trace("<ds.DataTable.add_TableCleared|API> %d#\n", ObjectID);
                onTableClearedDelegate += value;
            }
            remove {
                Bid.Trace("<ds.DataTable.remove_TableCleared|API> %d#\n", ObjectID);
                onTableClearedDelegate -= value;
            }
        }

        [ResCategoryAttribute(Res.DataCategory_Data), ResDescriptionAttribute(Res.DataTableRowsNewRowDescr)]
        public event DataTableNewRowEventHandler TableNewRow {
            add {
                onTableNewRowDelegate += value;
            }
            remove {
                onTableNewRowDelegate -= value;
            }
        }

        [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override ISite Site {
            get {
                return base.Site;
            }
            set {
                ISite oldSite = Site;
                if (value == null && oldSite != null) {
                    IContainer cont = oldSite.Container;

                    if (cont != null) {
                        for (int i = 0; i < Columns.Count; i++) {
                            if (Columns[i].Site != null) {
                                cont.Remove(Columns[i]);
                            }
                        }
                    }
                }
                base.Site = value;
            }
        }

        internal DataRow AddRecords(int oldRecord, int newRecord) {
            DataRow row;
            if (oldRecord == -1 && newRecord == -1)
            {
                row = NewRow(-1);
                AddRow(row);
            }
            else
            {
                row = NewEmptyRow();
                row.oldRecord = oldRecord;
                row.newRecord = newRecord;
                InsertRow(row, -1);
            }
            return row;
        }

        internal void AddRow(DataRow row) {
            AddRow(row, -1);
        }

        internal void AddRow(DataRow row, int proposedID) {
            InsertRow(row, proposedID, -1);
        }

        internal void InsertRow(DataRow row, int proposedID, int pos) {
            InsertRow(row, proposedID, pos, /*fireEvent*/true);
        }

        internal void InsertRow(DataRow row, long proposedID, int pos, bool fireEvent) {
            Exception deferredException = null;

            if (row == null) {
                throw ExceptionBuilder.ArgumentNull("row");
            }
            if (row.Table != this) {
                throw ExceptionBuilder.RowAlreadyInOtherCollection();
            }
            if (row.rowID != -1) {
                throw ExceptionBuilder.RowAlreadyInTheCollection();
            }
            row.BeginEdit(); // ensure something's there.            

            int record = row.tempRecord;
            row.tempRecord = -1;

            if (proposedID == -1) {
                proposedID = this.nextRowID;
            }

            bool rollbackOnException;
            if (rollbackOnException = (nextRowID <= proposedID)) { // WebData 109005
                nextRowID = checked(proposedID + 1);
            }

            try {
                try {
                    row.rowID = proposedID;
                    // this method may cause DataView.OnListChanged in which another row may be added
                    SetNewRecordWorker(row, record, DataRowAction.Add, false, false, pos, fireEvent, out deferredException); // now we do add the row to collection before OnRowChanged (RaiseRowChanged)
                }
                catch {
                    if (rollbackOnException && (nextRowID == proposedID+1)) {
                        nextRowID = proposedID;
                    }
                    row.rowID = -1;
                    row.tempRecord = record;
                    throw;
                }

                // since expression evaluation occurred in SetNewRecordWorker, there may have been a problem that
                // was deferred to this point.  If so, throw now since row has already been added.
                if (deferredException != null)
                    throw deferredException;

                if (EnforceConstraints && !inLoad ) { // if we are evaluating expression, we need to validate constraints
                    int columnCount = columnCollection.Count;
                    for (int i = 0; i < columnCount; ++i) {
                        DataColumn column = columnCollection[i];
                        if (column.Computed) {
                            column.CheckColumnConstraint(row, DataRowAction.Add);
                        }
                    }
                }
            }
            finally {
                row.ResetLastChangedColumn();// if expression is evaluated while adding, before  return, we want to clear it
            }
        }

        internal void CheckNotModifying(DataRow row) {
            if (row.tempRecord != -1) {
                row.EndEdit();
                //throw ExceptionBuilder.ModifyingRow();
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Clears the table of all data.</para>
        /// </devdoc>

        public void Clear() {
            Clear(true);
        }
        internal void Clear(bool clearAll) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Clear|INFO> %d#, clearAll=%d{bool}\n", ObjectID, clearAll);

            try {
                Debug.Assert(null == rowDiffId, "wasn't previously cleared");
                rowDiffId = null;

                if (dataSet != null)
                    dataSet.OnClearFunctionCalled(this);
                bool shouldFireClearEvents = (this.Rows.Count != 0); // if Rows is already empty, this is noop

                DataTableClearEventArgs e = null;
                if (shouldFireClearEvents) {
                    e = new DataTableClearEventArgs (this);
                    OnTableClearing(e);
                }

                if (dataSet != null && dataSet.EnforceConstraints) {

                    for (ParentForeignKeyConstraintEnumerator constraints = new ParentForeignKeyConstraintEnumerator(dataSet, this); constraints.GetNext();) {
                        ForeignKeyConstraint constraint = constraints.GetForeignKeyConstraint();
                        constraint.CheckCanClearParentTable(this);
                    }
                }

                recordManager.Clear(clearAll);

                // SQLBU 415729: Serious performance issue when calling Clear()
                // this improves performance by iterating over rows instead of computing by index
                foreach(DataRow row in Rows) {
                    row.oldRecord = -1;
                    row.newRecord = -1;
                    row.tempRecord = -1;
                    row.rowID = -1;
                    row.RBTreeNodeId = 0;
                }
                Rows.ArrayClear();

                ResetIndexes();

                if (shouldFireClearEvents) {
                    OnTableCleared(e);
                }

                // SQLBU 501916 - DataTable internal index is corrupted:'5'
                foreach(DataColumn column in Columns) {
                    EvaluateDependentExpressions(column);
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        internal void CascadeAll(DataRow row, DataRowAction action) {
            if (DataSet != null && DataSet.fEnableCascading) {
                for (ParentForeignKeyConstraintEnumerator constraints = new ParentForeignKeyConstraintEnumerator(dataSet, this); constraints.GetNext();) {
                    constraints.GetForeignKeyConstraint().CheckCascade(row, action);
                }
            }
        }

        internal void CommitRow(DataRow row) {
            // Fire Changing event
            DataRowChangeEventArgs drcevent = OnRowChanging(null, row, DataRowAction.Commit);

            if (!inDataLoad)
                CascadeAll(row, DataRowAction.Commit);

            SetOldRecord(row, row.newRecord);

            OnRowChanged(drcevent, row, DataRowAction.Commit);
        }

        internal int Compare(string s1, string s2) {
            return Compare(s1, s2, null);
        }

        internal int Compare(string s1, string s2, CompareInfo comparer) {
            object obj1 = s1;
            object obj2 = s2;
            if (obj1 == obj2)
                return 0;
            if (obj1 == null)
                return -1;
            if (obj2 == null)
                return 1;

            int leng1 = s1.Length;
            int leng2 = s2.Length;

            for (; leng1 > 0; leng1--) {
                if (s1[leng1-1] != 0x20 && s1[leng1-1] != 0x3000) // 0x3000 is Ideographic Whitespace
                    break;
            }
            for (; leng2 > 0; leng2--) {
                if (s2[leng2-1] != 0x20 && s2[leng2-1] != 0x3000)
                    break;
            }

            return (comparer ?? this.CompareInfo).Compare(s1, 0, leng1, s2, 0, leng2, _compareFlags);
        }

        internal int IndexOf(string s1, string s2) {
            return CompareInfo.IndexOf(s1, s2, _compareFlags);
        }

        internal bool IsSuffix(string s1, string s2) {
            return CompareInfo.IsSuffix(s1, s2, _compareFlags);
        }

        /// <devdoc>
        ///    <para>Computes the given expression on the current rows that pass the filter criteria.</para>
        /// </devdoc>
        public object Compute(string expression, string filter) {
            DataRow[] rows = Select(filter, "", DataViewRowState.CurrentRows);
            DataExpression expr = new DataExpression(this, expression);
            return expr.Evaluate(rows);
        }

        bool System.ComponentModel.IListSource.ContainsListCollection {
            get {
                return false;
            }
        }

        internal void CopyRow(DataTable table, DataRow row)
        {
            int oldRecord = -1, newRecord = -1;

            if (row == null)
                return;

            if (row.oldRecord != -1) {
                oldRecord = table.recordManager.ImportRecord(row.Table, row.oldRecord);
            }
            if (row.newRecord != -1) {
                if (row.newRecord != row.oldRecord) {
                    newRecord = table.recordManager.ImportRecord(row.Table, row.newRecord);
                }
                else
                    newRecord = oldRecord;
            }

            DataRow targetRow = table.AddRecords(oldRecord, newRecord);

            if (row.HasErrors) {
                targetRow.RowError = row.RowError;

                DataColumn[] cols = row.GetColumnsInError();

                for (int i = 0; i < cols.Length; i++) {
                    DataColumn col = targetRow.Table.Columns[cols[i].ColumnName];
                    targetRow.SetColumnError(col, row.GetColumnError(cols[i]));
                }
            }

       }


        internal void DeleteRow(DataRow row) {
            if (row.newRecord == -1) {
                throw ExceptionBuilder.RowAlreadyDeleted();
            }

            // Store.PrepareForDelete(row);
            SetNewRecord(row, -1, DataRowAction.Delete, false, true);
        }

        private void CheckPrimaryKey() {
            if (primaryKey == null) throw ExceptionBuilder.TableMissingPrimaryKey();
        }

        internal DataRow FindByPrimaryKey(object[] values) {
            CheckPrimaryKey();
            return FindRow(primaryKey.Key, values);
        }

        internal DataRow FindByPrimaryKey(object value) {
            CheckPrimaryKey();
            return FindRow(primaryKey.Key, value);
        }

        private DataRow FindRow(DataKey key, object[] values) {
            Index index = GetIndex(NewIndexDesc(key));
            Range range = index.FindRecords(values);
            if (range.IsNull)
                return null;
            return recordManager[index.GetRecord(range.Min)];
        }

        private DataRow FindRow(DataKey key, object value) {
            Index index = GetIndex(NewIndexDesc(key));
            Range range = index.FindRecords(value);
            if (range.IsNull)
                return null;
            return recordManager[index.GetRecord(range.Min)];
        }

        internal string FormatSortString(IndexField[] indexDesc) {
            StringBuilder builder = new StringBuilder();
            foreach (IndexField field in indexDesc) {
                if (0 < builder.Length) {
                    builder.Append(", ");
                }
                builder.Append(field.Column.ColumnName);
                if (field.IsDescending) {
                    builder.Append(" DESC");
                }
            }
            return builder.ToString();
        }

        internal void FreeRecord(ref int record) {
            recordManager.FreeRecord(ref record);
        }

        public DataTable GetChanges() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.GetChanges|API> %d#\n", ObjectID);
            try {
                DataTable dtChanges = this.Clone();
                DataRow row = null;

                for (int i = 0; i < Rows.Count; i++) {
                    row = Rows[i];
                    if (row.oldRecord != row.newRecord)
                        dtChanges.ImportRow(row);
                }

                if (dtChanges.Rows.Count == 0)
                    return null;

                return dtChanges;
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        public DataTable GetChanges(DataRowState rowStates)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.GetChanges|API> %d#, rowStates=%d{ds.DataRowState}\n", ObjectID, (int)rowStates);
            try {
                DataTable dtChanges = this.Clone();
                DataRow row = null;

                // check that rowStates is valid DataRowState
                Debug.Assert(Enum.GetUnderlyingType(typeof(DataRowState)) == typeof(Int32), "Invalid DataRowState type");

                for (int i = 0; i < Rows.Count; i++) {
                    row = Rows[i];
                    if ((row.RowState & rowStates) != 0)
                        dtChanges.ImportRow(row);
                }

                if (dtChanges.Rows.Count == 0)
                    return null;

                return dtChanges;
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        /// <devdoc>
        /// <para>Returns an array of <see cref='System.Data.DataRow'/> objects that contain errors.</para>
        /// </devdoc>
        public DataRow[] GetErrors() {
            List<DataRow> errorList = new List<DataRow>();

            for (int i = 0; i < Rows.Count; i++) {
                DataRow row = Rows[i];
                if (row.HasErrors) {
                    errorList.Add(row);
                }
            }
            DataRow[] temp = NewRowArray(errorList.Count);
            errorList.CopyTo(temp);
            return temp;
        }

        internal Index GetIndex(IndexField[] indexDesc) {
            return GetIndex(indexDesc, DataViewRowState.CurrentRows, (IFilter)null);
        }

        internal Index GetIndex(string sort, DataViewRowState recordStates, IFilter rowFilter) {
            return GetIndex(ParseSortString(sort), recordStates, rowFilter);
        }

        internal Index GetIndex(IndexField[] indexDesc, DataViewRowState recordStates, IFilter rowFilter) {
            indexesLock.AcquireReaderLock(-1);
            try {
                for (int i = 0; i < indexes.Count; i++) {
                    Index index = indexes[i];
                    if (index != null) {
                        if (index.Equal(indexDesc, recordStates, rowFilter)) {
                            return index;
                        }
                    }
                }
            }
            finally {
                indexesLock.ReleaseReaderLock();
            }
            Index ndx = new Index(this, indexDesc, recordStates, rowFilter);
            ndx.AddRef();
            return ndx;
        }

        IList System.ComponentModel.IListSource.GetList() {
            return DefaultView;
        }


        internal List<DataViewListener> GetListeners() {
            return _dataViewListeners;
        }

        // We need a HashCodeProvider for Case, Kana and Width insensitive
        internal int GetSpecialHashCode(string name) {
            int i;
            for (i = 0; (i < name.Length) && (0x3000 > name[i]); ++i);

            if (name.Length == i) {
                if (null == _hashCodeProvider) {
                    // it should use the CaseSensitive property, but V1 shipped this way
                    _hashCodeProvider = StringComparer.Create(Locale, true);
                }
                return _hashCodeProvider.GetHashCode(name);
            }
            else {
                return 0;
            }
        }

        public void ImportRow(DataRow row)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.ImportRow|API> %d#\n", ObjectID);
            try {
                int oldRecord = -1, newRecord = -1;

                if (row == null)
                    return;

                if (row.oldRecord != -1) {
                    oldRecord = recordManager.ImportRecord(row.Table, row.oldRecord);
                }
                if (row.newRecord != -1) {  // row not deleted
                    if (row.RowState != DataRowState.Unchanged) { // not unchanged, it means Added or modified
                        newRecord = recordManager.ImportRecord(row.Table, row.newRecord);
                    }
                    else
                        newRecord = oldRecord;
                }

                if (oldRecord != -1 || newRecord != -1) {
                    DataRow targetRow = AddRecords(oldRecord, newRecord);

                    if (row.HasErrors) {
                        targetRow.RowError = row.RowError;

                        DataColumn[] cols = row.GetColumnsInError();

                        for (int i = 0; i < cols.Length; i++) {
                            DataColumn col = targetRow.Table.Columns[cols[i].ColumnName];
                            targetRow.SetColumnError(col, row.GetColumnError(cols[i]));
                        }
                    }
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }

       }

        internal void InsertRow(DataRow row, long proposedID) {
            IntPtr hscp;

            Bid.ScopeEnter(out hscp, "<ds.DataTable.InsertRow|INFO> %d#, row=%d\n", ObjectID, row.ObjectID);
            try {
                if (row.Table != this) {
                    throw ExceptionBuilder.RowAlreadyInOtherCollection();
                }
                if (row.rowID != -1) {
                    throw ExceptionBuilder.RowAlreadyInTheCollection();
                }
                if (row.oldRecord == -1 && row.newRecord == -1) {
                    throw ExceptionBuilder.RowEmpty();
                }

                if (proposedID == -1)
                    proposedID = nextRowID;

                row.rowID = proposedID;
                if (nextRowID <= proposedID)
                    nextRowID = checked(proposedID + 1);

                DataRowChangeEventArgs drcevent = null;


                if (row.newRecord != -1) {
                    row.tempRecord = row.newRecord;
                    row.newRecord = -1;

                    try {
                        drcevent = RaiseRowChanging(null, row, DataRowAction.Add, true);
                    }
                    catch {
                        row.tempRecord = -1;
                        throw;
                    }

                    row.newRecord = row.tempRecord;
                    row.tempRecord = -1;
                }

                if (row.oldRecord != -1)
                    recordManager[row.oldRecord] = row;

                if (row.newRecord != -1)
                    recordManager[row.newRecord] = row;

                Rows.ArrayAdd(row); // SQL BU Defect Tracking 247738, 323482 row should be in the
                                    // collection when maintaining the indexes                  

                if (row.RowState == DataRowState.Unchanged){ //  how about row.oldRecord == row.newRecord both == -1
                    RecordStateChanged(row.oldRecord, DataViewRowState.None, DataViewRowState.Unchanged);
                }
                else {
                    RecordStateChanged(row.oldRecord, DataViewRowState.None, row.GetRecordState(row.oldRecord),
                                       row.newRecord, DataViewRowState.None, row.GetRecordState(row.newRecord));
                }

                if (dependentColumns != null && dependentColumns.Count > 0)
                    EvaluateExpressions(row, DataRowAction.Add, null);

                RaiseRowChanged(drcevent, row, DataRowAction.Add);
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        private IndexField [] NewIndexDesc(DataKey key) {
            Debug.Assert(key.HasValue);
            IndexField[] indexDesc = key.GetIndexDesc();
            IndexField[] newIndexDesc = new IndexField[indexDesc.Length];
            Array.Copy(indexDesc, 0, newIndexDesc, 0, indexDesc.Length);
            return newIndexDesc;
        }

        internal int NewRecord() {
            return NewRecord(-1);
        }

        internal int NewUninitializedRecord() {
            return recordManager.NewRecordBase();
        }

        internal int NewRecordFromArray(object[] value) {
            int colCount = columnCollection.Count; // Perf: use the readonly columnCollection field directly
            if (colCount < value.Length) {
                throw ExceptionBuilder.ValueArrayLength();
            }
            int record = recordManager.NewRecordBase();
            try {
                for (int i = 0; i < value.Length; i++) {
                    if (null != value[i]) {
                        columnCollection[i][record] = value[i];
                    }
                    else {
                        columnCollection[i].Init(record);  // Increase AutoIncrementCurrent
                    }
                }
                for (int i = value.Length; i < colCount; i++) {
                    columnCollection[i].Init(record);
                }
                return record;
            }
            catch (Exception e) {
                // 
                if (Common.ADP.IsCatchableOrSecurityExceptionType (e)) {
                    FreeRecord(ref record); // WebData 104246
                }
                throw;
            }
        }

        internal int NewRecord(int sourceRecord) {
            int record = recordManager.NewRecordBase();

            int count = columnCollection.Count;
            if (-1 == sourceRecord) {
                for (int i = 0; i < count; ++i) {
                    columnCollection[i].Init(record);
                }
            }
            else {
                for (int i = 0; i < count; ++i) {
                    columnCollection[i].Copy(sourceRecord, record);
                }
            }
            return record;
        }

        internal DataRow NewEmptyRow() {
            rowBuilder._record = -1;
            DataRow dr = NewRowFromBuilder( rowBuilder );
            if (dataSet != null) {
                DataSet.OnDataRowCreated( dr );
            }
            return dr;
        }

        private DataRow NewUninitializedRow() {
            DataRow dr = NewRow(NewUninitializedRecord());
            return dr;
        }

        /// <devdoc>
        /// <para>Creates a new <see cref='System.Data.DataRow'/>
        /// with the same schema as the table.</para>
        /// </devdoc>
        public DataRow NewRow() {
            DataRow dr = NewRow(-1);
            NewRowCreated(dr); // this is the only API we want this event to be fired
            return dr;
        }

        // Only initialize DataRelation mapping columns (approximately hidden columns)
        internal DataRow CreateEmptyRow() {
            DataRow row = this.NewUninitializedRow();

            foreach( DataColumn c in this.Columns ) {
                if (!XmlToDatasetMap.IsMappedColumn(c)) {
                    if (!c.AutoIncrement) {
                        if (c.AllowDBNull) {
                            row[c] = DBNull.Value;
                        }
                        else if(c.DefaultValue!=null){
                            row[c] = c.DefaultValue;
                        }
                    }
                    else {
                        c.Init(row.tempRecord);
                    }
                }
            }
            return row;
        }

        private void NewRowCreated(DataRow row) {
            if (null != onTableNewRowDelegate) {
                DataTableNewRowEventArgs eventArg =  new DataTableNewRowEventArgs(row);
                OnTableNewRow(eventArg);
            }
        }

        internal DataRow NewRow(int record) {
            if (-1 == record) {
                record = NewRecord(-1);
            }

            rowBuilder._record = record;
            DataRow row = NewRowFromBuilder( rowBuilder );
            recordManager[record] = row;

            if (dataSet != null)
                DataSet.OnDataRowCreated( row );

            return row;
        }

        // This is what a subclassed dataSet overrides to create a new row.
        protected virtual DataRow NewRowFromBuilder(DataRowBuilder builder) {
            return new DataRow(builder);
        }

        /// <devdoc>
        ///    <para>Gets the row type.</para>
        /// </devdoc>
        protected virtual Type GetRowType() {
            return typeof(DataRow);
        }

        // Prevent inlining so that reflection calls are not moved to caller that may be in a different assembly that may have a different grant set.
        [MethodImpl(MethodImplOptions.NoInlining)] 
        protected internal DataRow[] NewRowArray(int size) {
            if (IsTypedDataTable) {
                if (0 == size) {
                    if (null == EmptyDataRowArray) {
                        EmptyDataRowArray = (DataRow[]) Array.CreateInstance(GetRowType(), 0);
                    }
                    return EmptyDataRowArray;
                }
                return (DataRow[]) Array.CreateInstance(GetRowType(), size);
            }
            else {
                return ((0 == size) ? DataTable.zeroRows : new DataRow[size]);
            }
        }

        internal bool NeedColumnChangeEvents {
            get {
                return (IsTypedDataTable || (null != onColumnChangingDelegate) || (null != onColumnChangedDelegate));
            }
        }

        protected internal virtual void OnColumnChanging(DataColumnChangeEventArgs e) {
            // intentionally allow exceptions to bubble up.  We haven't committed anything yet.
            Debug.Assert(e != null, "e should not be null");
            if (onColumnChangingDelegate != null) {
                Bid.Trace("<ds.DataTable.OnColumnChanging|INFO> %d#\n", ObjectID);
                onColumnChangingDelegate(this, e);
            }
        }

        protected internal virtual void OnColumnChanged(DataColumnChangeEventArgs e) {
            Debug.Assert(e != null, "e should not be null");
            if (onColumnChangedDelegate != null) {
                Bid.Trace("<ds.DataTable.OnColumnChanged|INFO> %d#\n", ObjectID);
                onColumnChangedDelegate(this, e);
            }
        }

        protected virtual void OnPropertyChanging(PropertyChangedEventArgs pcevent) {
            if (onPropertyChangingDelegate != null) {
                Bid.Trace("<ds.DataTable.OnPropertyChanging|INFO> %d#\n", ObjectID);
                onPropertyChangingDelegate(this, pcevent);
            }
        }

        internal void OnRemoveColumnInternal(DataColumn column) {
            OnRemoveColumn(column);
        }

        /// <devdoc>
        /// <para>Notifies the <see cref='System.Data.DataTable'/> that a <see cref='System.Data.DataColumn'/> is
        ///    being removed.</para>
        /// </devdoc>
        protected virtual void OnRemoveColumn(DataColumn column) {
        }

        private DataRowChangeEventArgs OnRowChanged(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction) {
            if ((null != onRowChangedDelegate) || IsTypedDataTable) {
                if (null == args) {
                    args = new DataRowChangeEventArgs(eRow, eAction);
                }
                OnRowChanged(args);
            }
            return args;
        }

        private DataRowChangeEventArgs OnRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction) {
            if ((null != onRowChangingDelegate) || IsTypedDataTable) {
                if (null == args) {
                    args = new DataRowChangeEventArgs(eRow, eAction);
                }
                OnRowChanging(args);
            }
            return args;
        }

        /// <devdoc>
        ///    <para>
        ///       Raises the <see cref='System.Data.DataTable.RowChanged'/> event.
        ///    </para>
        /// </devdoc>
        protected virtual void OnRowChanged(DataRowChangeEventArgs e) {
            Debug.Assert((null != e) && ((null != onRowChangedDelegate) || IsTypedDataTable), "OnRowChanged arguments");
            if (onRowChangedDelegate != null) {
                Bid.Trace("<ds.DataTable.OnRowChanged|INFO> %d#\n", ObjectID);
                onRowChangedDelegate(this, e);
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Raises the <see cref='System.Data.DataTable.RowChanging'/> event.
        ///    </para>
        /// </devdoc>
        protected virtual void OnRowChanging(DataRowChangeEventArgs e) {
            Debug.Assert((null != e) && ((null != onRowChangingDelegate) || IsTypedDataTable), "OnRowChanging arguments");
            if (onRowChangingDelegate != null) {
                Bid.Trace("<ds.DataTable.OnRowChanging|INFO> %d#\n", ObjectID);
                onRowChangingDelegate(this, e);
           }
        }

        /// <devdoc>
        ///    <para>
        ///       Raises the <see cref='System.Data.DataTable.OnRowDeleting'/> event.
        ///    </para>
        /// </devdoc>
        protected virtual void OnRowDeleting(DataRowChangeEventArgs e) {
            Debug.Assert((null != e) && ((null != onRowDeletingDelegate) || IsTypedDataTable), "OnRowDeleting arguments");
            if (onRowDeletingDelegate != null) {
                Bid.Trace("<ds.DataTable.OnRowDeleting|INFO> %d#\n", ObjectID);
                onRowDeletingDelegate(this, e);
            }
        }

        /// <devdoc>
        ///    <para>
        ///       Raises the <see cref='System.Data.DataTable.OnRowDeleted'/> event.
        ///    </para>
        /// </devdoc>
        protected virtual void OnRowDeleted(DataRowChangeEventArgs e) {
            Debug.Assert((null != e) && ((null != onRowDeletedDelegate) || IsTypedDataTable), "OnRowDeleted arguments");
            if (onRowDeletedDelegate != null) {
                Bid.Trace("<ds.DataTable.OnRowDeleted|INFO> %d#\n", ObjectID);
                onRowDeletedDelegate(this, e);
            }
        }

        protected virtual void OnTableCleared(DataTableClearEventArgs e) {
            if (onTableClearedDelegate != null) {
                Bid.Trace("<ds.DataTable.OnTableCleared|INFO> %d#\n", ObjectID);
                onTableClearedDelegate(this, e);
            }
        }

        protected virtual void OnTableClearing(DataTableClearEventArgs e) {
            if (onTableClearingDelegate != null) {
                Bid.Trace("<ds.DataTable.OnTableClearing|INFO> %d#\n", ObjectID);
                onTableClearingDelegate(this, e);
            }
        }

        protected virtual void OnTableNewRow(DataTableNewRowEventArgs  e) {
            if (onTableNewRowDelegate != null) {
                Bid.Trace("<ds.DataTable.OnTableNewRow|INFO> %d#\n", ObjectID);
                onTableNewRowDelegate(this, e);
            }
        }

        private void OnInitialized() {
            if (onInitialized != null) {
                Bid.Trace("<ds.DataTable.OnInitialized|INFO> %d#\n", ObjectID);
                onInitialized(this, EventArgs.Empty);
            }
        }


        internal IndexField[] ParseSortString(string sortString) {
            IndexField[] indexDesc = zeroIndexField;
            if ((null != sortString) && (0 < sortString.Length)) {
                string[] split = sortString.Split(new char[] { ','});
                indexDesc = new IndexField[split.Length];

                for (int i = 0; i < split.Length; i++) {
                    string current = split[i].Trim();

                    // handle ASC and DESC.
                    int length = current.Length;
                    bool descending = false;
                    if (length >= 5 && String.Compare(current, length - 4, " ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0) {
                        current = current.Substring(0, length - 4).Trim();
                    }
                    else if (length >= 6 && String.Compare(current, length - 5, " DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0) {
                        descending = true;
                        current = current.Substring(0, length - 5).Trim();
                    }

                    // handle brackets.
                    if (current.StartsWith("[", StringComparison.Ordinal)) {
                        if (current.EndsWith("]", StringComparison.Ordinal)) {
                            current = current.Substring(1, current.Length - 2);
                        }
                        else {
                            throw ExceptionBuilder.InvalidSortString(split[i]);
                        }
                    }

                    // find the column.
                    DataColumn column = Columns[current];
                    if(column == null) {
                        throw ExceptionBuilder.ColumnOutOfRange(current);
                    }
                    indexDesc[i] = new IndexField(column, descending);
                }
            }
            return indexDesc;
        }

        internal void RaisePropertyChanging(string name) {
            OnPropertyChanging(new PropertyChangedEventArgs(name));
        }

        // Notify all indexes that record changed.
        // Only called when Error was changed.
        internal void RecordChanged(int record) {
            Debug.Assert (record != -1, "Record number must be given");
            SetShadowIndexes(); // how about new assert?
            try {
                int numIndexes = shadowIndexes.Count;
                for (int i = 0; i < numIndexes; i++) {
                    Index ndx = shadowIndexes[i];// shadowindexes may change, see ShadowIndexCopy()
                    if (0 < ndx.RefCount) {
                        ndx.RecordChanged(record);
                    }
                }
            }
            finally{
                RestoreShadowIndexes();
            }
        }

// for each index in liveindexes invok RecordChanged
// oldIndex and newIndex keeps  position of record before delete and after insert in each index in order
// LiveIndexes[n-m] will have its information in oldIndex[n-m] and  newIndex[n-m]
        internal void RecordChanged(int[] oldIndex, int[] newIndex) {
            SetShadowIndexes();
            Debug.Assert (oldIndex.Length == newIndex.Length,  "Size oldIndexes and newIndexes should be the same");
            Debug.Assert (oldIndex.Length == shadowIndexes.Count, "Size of OldIndexes should be the same as size of Live indexes");
            try{
                int numIndexes = shadowIndexes.Count;
                for (int i = 0; i < numIndexes; i++) {
                    Index ndx = shadowIndexes[i];// shadowindexes may change, see ShadowIndexCopy()
                    if (0 < ndx.RefCount) {
                        ndx.RecordChanged(oldIndex[i], newIndex[i]);
                    }
                }

            }
            finally{
                RestoreShadowIndexes();
            }
        }

        internal void RecordStateChanged(int record, DataViewRowState oldState, DataViewRowState newState) {
            SetShadowIndexes();
            try{
                int numIndexes = shadowIndexes.Count;
                for (int i = 0; i < numIndexes; i++) {
                    Index ndx = shadowIndexes[i];// shadowindexes may change, see ShadowIndexCopy()
                    if (0 < ndx.RefCount) {
                        ndx.RecordStateChanged(record, oldState, newState);
                    }
                }
            }
            finally{
                RestoreShadowIndexes();
            }
            // System.Data.XML.Store.Store.OnROMChanged(record, oldState, newState);
        }


        internal void RecordStateChanged(int record1, DataViewRowState oldState1, DataViewRowState newState1,
                                         int record2, DataViewRowState oldState2, DataViewRowState newState2) {
            SetShadowIndexes();
            try{
                int numIndexes = shadowIndexes.Count;
                for (int i = 0; i < numIndexes; i++) {
                    Index ndx = shadowIndexes[i];// shadowindexes may change, see ShadowIndexCopy()
                    if (0 < ndx.RefCount) {
                        if (record1 != -1 && record2 != -1)
                            ndx.RecordStateChanged(record1, oldState1, newState1,
                                                   record2, oldState2, newState2);
                        else if (record1 != -1)
                            ndx.RecordStateChanged(record1, oldState1, newState1);
                        else if (record2 != -1)
                            ndx.RecordStateChanged(record2, oldState2, newState2);
                    }
                }
            }
            finally {
                RestoreShadowIndexes();
            }
            // System.Data.XML.Store.Store.OnROMChanged(record1, oldState1, newState1, record2, oldState2, newState2);
        }


// RemoveRecordFromIndexes removes the given record (using row and version) from all indexes and it  stores and returns the position of deleted
// record from each index
// IT SHOULD NOT CAUSE ANY EVENT TO BE FIRED
        internal int[] RemoveRecordFromIndexes(DataRow row, DataRowVersion  version) {
            int    indexCount          =  LiveIndexes.Count;
            int [] positionIndexes =  new int[indexCount];

            int recordNo = row.GetRecordFromVersion(version);
            DataViewRowState states = row.GetRecordState(recordNo);

            while (--indexCount >= 0) {
                if (row.HasVersion(version) && ((states & indexes[indexCount].RecordStates) != DataViewRowState.None)) {
                    int index = indexes[indexCount].GetIndex(recordNo);
                    if (index > -1) {
                        positionIndexes [indexCount] = index;
                        indexes[indexCount].DeleteRecordFromIndex(index); // this will delete the record from index and MUSt not fire event
                    }
                    else {
                        positionIndexes [indexCount] = -1; // this means record was not in index
                    }
                }
                else {
                    positionIndexes [indexCount] = -1; // this means record was not in index
                }
            }
            return positionIndexes;
        }

// InsertRecordToIndexes inserts the given record (using row and version) to all indexes and it  stores and returns the position of inserted
// record to each index
// IT SHOULD NOT CAUSE ANY EVENT TO BE FIRED
        internal int[] InsertRecordToIndexes(DataRow row, DataRowVersion  version) {
            int    indexCount          =  LiveIndexes.Count;
            int [] positionIndexes =  new int[indexCount];

            int recordNo = row.GetRecordFromVersion(version);
            DataViewRowState states = row.GetRecordState(recordNo);

            while (--indexCount >= 0) {
                if (row.HasVersion(version)) {
                    if ((states & indexes[indexCount].RecordStates) != DataViewRowState.None) {
                        positionIndexes [indexCount] = indexes[indexCount].InsertRecordToIndex(recordNo);
                    }
                    else {
                        positionIndexes [indexCount] = -1;
                    }
                }
            }
            return positionIndexes;
        }

        internal void SilentlySetValue(DataRow dr, DataColumn dc, DataRowVersion version, object newValue) {
            // get record for version
            int record = dr.GetRecordFromVersion(version);

            bool equalValues = false;
            if (DataStorage.IsTypeCustomType(dc.DataType) && newValue != dc[record]) {
                // if UDT storage, need to check if reference changed. See bug 385182
                equalValues = false;
            }
            else {
                equalValues = dc.CompareValueTo(record, newValue, true);
            }

            // if expression has changed
            if (!equalValues) {
                int[] oldIndex = dr.Table.RemoveRecordFromIndexes(dr, version);// conditional, if it exists it will try to remove with no event fired
                dc.SetValue(record, newValue);
                int[] newIndex = dr.Table.InsertRecordToIndexes(dr, version);// conditional, it will insert if it qualifies, no event will be fired
                if (dr.HasVersion(version)) {
                    if (version != DataRowVersion.Original) {
                        dr.Table.RecordChanged(oldIndex, newIndex);
                    }
                    if (dc.dependentColumns != null) {
                        //BugBug - passing in null for cachedRows.  This means expression columns as keys does not work when key changes.
                        dc.Table.EvaluateDependentExpressions(dc.dependentColumns, dr, version, null);
                    }
                }
             }
             dr.ResetLastChangedColumn();
        }

        /// <devdoc>
        ///    <para>Rolls back all changes that have been made to the table
        ///       since it was loaded, or the last time <see cref='System.Data.DataTable.AcceptChanges'/> was called.</para>
        /// </devdoc>
        public void RejectChanges() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.RejectChanges|API> %d#\n", ObjectID);
            try{
                DataRow[] oldRows = new DataRow[Rows.Count];
                Rows.CopyTo(oldRows, 0);

                for (int i = 0; i < oldRows.Length; i++) {
                    RollbackRow(oldRows[i]);
                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }

        }

        internal void RemoveRow(DataRow row, bool check) {
            if (row.rowID == -1) {
                throw ExceptionBuilder.RowAlreadyRemoved();
            }

            if (check && dataSet != null) {
                for (ParentForeignKeyConstraintEnumerator constraints = new ParentForeignKeyConstraintEnumerator(dataSet, this); constraints.GetNext();) {
                    constraints.GetForeignKeyConstraint().CheckCanRemoveParentRow(row);
                }
            }

            int oldRecord = row.oldRecord;
            int newRecord = row.newRecord;

            DataViewRowState oldRecordStatePre = row.GetRecordState(oldRecord);
            DataViewRowState newRecordStatePre = row.GetRecordState(newRecord);

            row.oldRecord = -1;
            row.newRecord = -1;

            if (oldRecord == newRecord) {
                oldRecord = -1;
            }

            RecordStateChanged(oldRecord, oldRecordStatePre, DataViewRowState.None,
                               newRecord, newRecordStatePre, DataViewRowState.None);

            FreeRecord(ref oldRecord);
            FreeRecord(ref newRecord);

            row.rowID = -1;
            Rows.ArrayRemove(row);
        }

        // Resets the table back to its original state.
        public virtual void Reset() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Reset|API> %d#\n", ObjectID);
            try {
                Clear();
                ResetConstraints();

                DataRelationCollection dr = this.ParentRelations;
                int count = dr.Count;
                  while (count > 0) {
                   count--;
                   dr.RemoveAt(count);
                }

                dr = this.ChildRelations;
                count = dr.Count;
                  while (count > 0) {
                   count--;
                   dr.RemoveAt(count);
                }

                Columns.Clear();
                indexes.Clear();
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        internal void ResetIndexes() {
            ResetInternalIndexes(null);
        }

        internal void ResetInternalIndexes(DataColumn column) {
            Debug.Assert(null != indexes, "unexpected null indexes");
            SetShadowIndexes();
            try{
                // the length of shadowIndexes will not change
                // but the array instance may change during
                // events during Index.Reset
                int numIndexes = shadowIndexes.Count;
                for (int i = 0; i < numIndexes; i++) {
                    Index ndx = shadowIndexes[i];// shadowindexes may change, see ShadowIndexCopy()
                    if (0 < ndx.RefCount) {
                        if (null == column) {
                            ndx.Reset();
                        }
                        else {
                            // SQLBU 501916: DataTable internal index is corrupted:'5'
                            bool found = false;
                            foreach(IndexField field in ndx.IndexFields) {
                                if (Object.ReferenceEquals(column, field.Column)) {
                                    found = true;
                                    break;
                                    
                                }
                            }
                            if (found) {
                                ndx.Reset();
                            }
                        }
                    }
                }
            }
            finally {
                RestoreShadowIndexes();
            }
        }

        internal void RollbackRow(DataRow row) {
            row.CancelEdit();
            SetNewRecord(row, row.oldRecord, DataRowAction.Rollback, false, true);
        }

        private DataRowChangeEventArgs RaiseRowChanged(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction) {
            try {
                if (UpdatingCurrent(eRow, eAction) && (IsTypedDataTable || (null != onRowChangedDelegate))) {
                    args = OnRowChanged(args, eRow, eAction);
                }
                // check if we deleting good row
                else if (DataRowAction.Delete == eAction && eRow.newRecord == -1 && (IsTypedDataTable || (null != onRowDeletedDelegate))) {
                    if (null == args) {
                        args = new DataRowChangeEventArgs(eRow, eAction);
                    }
                    OnRowDeleted(args);
                }
            }
            catch (Exception f) {
               // 
               if (!Common.ADP.IsCatchableExceptionType(f)) {
                 throw;
               }
               ExceptionBuilder.TraceExceptionWithoutRethrow(f);
               // ignore the exception
            }
            return args;
        }

        private DataRowChangeEventArgs RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction) {
            if (UpdatingCurrent(eRow, eAction) && (IsTypedDataTable || (null != onRowChangingDelegate))) {
                eRow.inChangingEvent = true;

                // don't catch
                try {
                    args = OnRowChanging(args, eRow, eAction);
                }
                finally {
                    eRow.inChangingEvent = false;
                }
            }
            // check if we deleting good row
            else if (DataRowAction.Delete == eAction && eRow.newRecord != -1 && (IsTypedDataTable || (null != onRowDeletingDelegate))) {
                eRow.inDeletingEvent = true;
                // don't catch
                try {
                    if (null == args) {
                        args = new DataRowChangeEventArgs(eRow, eAction);
                    }
                    OnRowDeleting(args);
                }
                finally {
                    eRow.inDeletingEvent = false;
                }
            }
            return args;
        }

        private DataRowChangeEventArgs RaiseRowChanging(DataRowChangeEventArgs args, DataRow eRow, DataRowAction eAction, bool fireEvent) {

            // check all constraints
            if (EnforceConstraints && !inLoad ) {
                int columnCount = columnCollection.Count;
                for(int i = 0; i < columnCount; ++i) {
                    DataColumn column = columnCollection[i];
                    if (!column.Computed || eAction != DataRowAction.Add) {
                        column.CheckColumnConstraint(eRow, eAction);
                    }
                }

                int constraintCount = constraintCollection.Count;
                for(int i = 0; i < constraintCount; ++i) {
                    constraintCollection[i].CheckConstraint(eRow, eAction);
                }
            }

            // $$anandra.  Check this event out. May be an issue.
            if (fireEvent) {
                args = RaiseRowChanging(args, eRow, eAction);
            }

            if (!inDataLoad) {
                // cascade things...
                if (!MergingData && eAction != DataRowAction.Nothing && eAction != DataRowAction.ChangeOriginal) {
                    CascadeAll(eRow, eAction);
                }
            }
            return args;
        }

        /// <devdoc>
        /// <para>Returns an array of all <see cref='System.Data.DataRow'/> objects.</para>
        /// </devdoc>
        public DataRow[] Select() {
            Bid.Trace("<ds.DataTable.Select|API> %d#\n", ObjectID);
            return new Select(this, "", "", DataViewRowState.CurrentRows).SelectRows();
        }

        /// <devdoc>
        /// <para>Returns an array of all <see cref='System.Data.DataRow'/> objects that match the filter criteria in order of
        ///    primary key (or lacking one, order of addition.)</para>
        /// </devdoc>
        public DataRow[] Select(string filterExpression) {
            Bid.Trace("<ds.DataTable.Select|API> %d#, filterExpression='%ls'\n", ObjectID, filterExpression);
            return new Select(this, filterExpression, "", DataViewRowState.CurrentRows).SelectRows();
        }

        /// <devdoc>
        /// <para>Returns an array of all <see cref='System.Data.DataRow'/> objects that match the filter criteria, in the the
        ///    specified sort order.</para>
        /// </devdoc>
        public DataRow[] Select(string filterExpression, string sort) {
            Bid.Trace("<ds.DataTable.Select|API> %d#, filterExpression='%ls', sort='%ls'\n", ObjectID, filterExpression, sort);
            return new Select(this, filterExpression, sort, DataViewRowState.CurrentRows).SelectRows();
        }

        /// <devdoc>
        /// <para>Returns an array of all <see cref='System.Data.DataRow'/> objects that match the filter in the order of the
        ///    sort, that match the specified state.</para>
        /// </devdoc>
        public DataRow[] Select(string filterExpression, string sort, DataViewRowState recordStates) {
            Bid.Trace("<ds.DataTable.Select|API> %d#, filterExpression='%ls', sort='%ls', recordStates=%d{ds.DataViewRowState}\n", ObjectID, filterExpression, sort, (int)recordStates);
            return new Select(this, filterExpression, sort, recordStates).SelectRows();
        }

        internal void SetNewRecord(DataRow row, int proposedRecord, DataRowAction action = DataRowAction.Change, bool isInMerge = false, bool fireEvent = true, bool suppressEnsurePropertyChanged = false) {
            Exception deferredException = null;
            SetNewRecordWorker(row, proposedRecord, action, isInMerge, suppressEnsurePropertyChanged, -1, fireEvent, out deferredException); // we are going to call below overload from insert
            if (deferredException != null) {
                throw deferredException;
            }
        }

        private void SetNewRecordWorker(DataRow row, int proposedRecord, DataRowAction action, bool isInMerge, bool suppressEnsurePropertyChanged,
            int position, bool fireEvent, out Exception deferredException) {

            // this is the event workhorse... it will throw the changing/changed events
            // and update the indexes. Used by change, add, delete, revert.

            // order of execution is as follows
            //
            // 1) set temp record
            // 2) Check constraints for non-expression columns
            // 3) Raise RowChanging/RowDeleting with temp record
            // 4) set the new record in storage
            // 5) Update indexes with recordStateChanges - this will fire ListChanged & PropertyChanged events on associated views
            // 6) Evaluate all Expressions (exceptions are deferred)- this will fire ListChanged & PropertyChanged events on associated views
            // 7) Raise RowChanged/ RowDeleted
            // 8) Check constraints for expression columns

            Debug.Assert(row != null, "Row can't be null.");
            deferredException = null;
            
            if (row.tempRecord != proposedRecord) {
                // $HACK: for performance reasons, EndUpdate calls SetNewRecord with tempRecord == proposedRecord
                if (!inDataLoad) {
                    row.CheckInTable();
                    CheckNotModifying(row);
                }
                if (proposedRecord == row.newRecord) {
                    if (isInMerge) {
                        Debug.Assert(fireEvent, "SetNewRecord is called with wrong parameter");
                        RaiseRowChanged(null, row, action);
                    }
                    return;
                }

                Debug.Assert(!row.inChangingEvent, "How can this row be in an infinite loop?");

                row.tempRecord = proposedRecord;
            }
            DataRowChangeEventArgs drcevent = null;

            try {
                row._action = action;
                drcevent = RaiseRowChanging(null, row, action, fireEvent);
            }
            catch {
                row.tempRecord = -1;
                throw;
            }
            finally {
                row._action = DataRowAction.Nothing;
            }

            row.tempRecord = -1;

            int currentRecord = row.newRecord;

            // if we're deleting, then the oldRecord value will change, so need to track that if it's distinct from the newRecord.
            int secondRecord = (proposedRecord != -1 ?
                                proposedRecord :
                                (row.RowState != DataRowState.Unchanged ?
                                 row.oldRecord :
                                 -1));

            if (action == DataRowAction.Add) { //if we come here from insert we do insert the row to collection
                if (position == -1)
                    Rows.ArrayAdd(row);
                else
                    Rows.ArrayInsert(row, position);
            }

            List<DataRow> cachedRows = null;
            if ((action == DataRowAction.Delete || action == DataRowAction.Change)
                && dependentColumns != null && dependentColumns.Count > 0) {
                // if there are expression columns, need to cache related rows for deletes and updates (key changes)
                // before indexes are modified.
                cachedRows = new List<DataRow>();
                for (int j = 0; j < ParentRelations.Count; j++) {
                    DataRelation relation = ParentRelations[j];
                    if (relation.ChildTable != row.Table) {
                        continue;
                    }
                    cachedRows.InsertRange(cachedRows.Count, row.GetParentRows(relation));
                }

                for (int j = 0; j < ChildRelations.Count; j++) {
                    DataRelation relation = ChildRelations[j];
                    if (relation.ParentTable != row.Table) {
                        continue;
                    }
                    cachedRows.InsertRange(cachedRows.Count, row.GetChildRows(relation));
                }
            }

            // Dev10 Bug 688779: DataRowView.PropertyChanged are not raised on RejectChanges
            // if the newRecord is changing, the propertychanged event should be allowed to triggered for ListChangedType.Changed or .Moved
            // unless the specific condition is known that no data has changed, like DataRow.SetModified()
            if (!suppressEnsurePropertyChanged && !row.HasPropertyChanged && (row.newRecord != proposedRecord)
                && (-1 != proposedRecord) // explictly not fixing Dev10 Bug 692044: DataRowView.PropertyChanged are not raised on DataTable.Delete when mixing current and original records in RowStateFilter
                && (-1 != row.newRecord)) // explictly not fixing parts of Dev10 Bug 697909: when mixing current and original records in RowStateFilter
            {
                // DataRow will believe multiple edits occured and
                // DataView.ListChanged event w/ ListChangedType.ItemChanged will raise DataRowView.PropertyChanged event and
                // PropertyChangedEventArgs.PropertyName will now be empty string so
                // WPF will refresh the entire row
                row.LastChangedColumn = null;
                row.LastChangedColumn = null;
            }

                // Check whether we need to update indexes
                if (LiveIndexes.Count != 0) {

                    // Dev10 bug #463087: DataTable internal index is currupted: '5'
                    if ((-1 == currentRecord) && (-1 != proposedRecord) && (-1 != row.oldRecord) && (proposedRecord != row.oldRecord)) {
                        // the transition from DataRowState.Deleted -> DataRowState.Modified
                        // with same orginal record but new current record
                        // needs to raise an ItemChanged or ItemMoved instead of ItemAdded in the ListChanged event.
                        // for indexes/views listening for both DataViewRowState.Deleted | DataViewRowState.ModifiedCurrent
                        currentRecord = row.oldRecord;
                    }

                    DataViewRowState currentRecordStatePre = row.GetRecordState(currentRecord);
                    DataViewRowState secondRecordStatePre = row.GetRecordState(secondRecord);

                    row.newRecord = proposedRecord;
                    if (proposedRecord != -1)
                        this.recordManager[proposedRecord] = row;

                    DataViewRowState currentRecordStatePost = row.GetRecordState(currentRecord);
                    DataViewRowState secondRecordStatePost = row.GetRecordState(secondRecord);

                    // may raise DataView.ListChanged event
                    RecordStateChanged(currentRecord, currentRecordStatePre, currentRecordStatePost,
                        secondRecord, secondRecordStatePre, secondRecordStatePost);
                }
                else {
                    row.newRecord = proposedRecord;
                    if (proposedRecord != -1)
                        this.recordManager[proposedRecord] = row;
                }

                // Dev10 Bug 461199 - reset the last changed column here, after all
                // DataViews have raised their DataRowView.PropertyChanged event
                row.ResetLastChangedColumn();

                // SQLBU 278737: Record manager corruption when reentrant write operations
                // free the 'currentRecord' only after all the indexes have been updated.
                // Corruption! { if (currentRecord != row.oldRecord) { FreeRecord(ref currentRecord); } }
                // RecordStateChanged raises ListChanged event at which time user may do work
                if (-1 != currentRecord) {
                    if (currentRecord != row.oldRecord)
                    {
                        if ((currentRecord != row.tempRecord) &&   // Delete, AcceptChanges, BeginEdit
                            (currentRecord != row.newRecord) &&    // RejectChanges & SetAdded
                            (row == recordManager[currentRecord])) // AcceptChanges, NewRow
                        {
                            FreeRecord(ref currentRecord);
                        }
                    }
                }

            if (row.RowState == DataRowState.Detached && row.rowID != -1) {
                RemoveRow(row, false);
            }

            if (dependentColumns != null && dependentColumns.Count > 0) {
                try {
                    EvaluateExpressions(row, action, cachedRows);
                }
                catch (Exception exc) {
                    // For DataRows being added, throwing of exception from expression evaluation is
                    // deferred until after the row has been completely added.
                    if (action != DataRowAction.Add) {
                        throw exc;
                    }
                    else {
                        deferredException = exc;
                    }
                }
            }

            try {
                if (fireEvent) {
                    RaiseRowChanged(drcevent, row, action);
                }
            }
            catch (Exception e) {
                // 
                if (!Common.ADP.IsCatchableExceptionType(e)) {
                    throw;
                }
                ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                // ignore the exception
            }
        }

        // this is the event workhorse... it will throw the changing/changed events
        // and update the indexes.
        internal void SetOldRecord(DataRow row, int proposedRecord) {
            if (!inDataLoad) {
                row.CheckInTable();
                CheckNotModifying(row);
            }

            if (proposedRecord == row.oldRecord) {
                return;
            }

            int originalRecord = row.oldRecord; // cache old record after potential RowChanging event
            try {
                // Check whether we need to update indexes
                if (LiveIndexes.Count != 0) {

                    // Dev10 bug #463087: DataTable internal index is currupted: '5'
                    if ((-1 == originalRecord) && (-1 != proposedRecord) && (-1 != row.newRecord) && (proposedRecord != row.newRecord)) {
                        // the transition from DataRowState.Added -> DataRowState.Modified
                        // with same current record but new original record
                        // needs to raise an ItemChanged or ItemMoved instead of ItemAdded in the ListChanged event.
                        // for indexes/views listening for both DataViewRowState.Added | DataViewRowState.ModifiedOriginal
                        originalRecord = row.newRecord;
                    }

                    DataViewRowState originalRecordStatePre = row.GetRecordState(originalRecord);
                    DataViewRowState proposedRecordStatePre = row.GetRecordState(proposedRecord);

                    row.oldRecord = proposedRecord;
                    if (proposedRecord != -1)
                        this.recordManager[proposedRecord] = row;

                    DataViewRowState originalRecordStatePost = row.GetRecordState(originalRecord);
                    DataViewRowState proposedRecordStatePost = row.GetRecordState(proposedRecord);

                    RecordStateChanged(originalRecord, originalRecordStatePre, originalRecordStatePost,
                                       proposedRecord, proposedRecordStatePre, proposedRecordStatePost);
                }
                else {
                    row.oldRecord = proposedRecord;
                    if (proposedRecord != -1)
                        this.recordManager[proposedRecord] = row;
                }
            }
            finally {
                if ((originalRecord != -1) && (originalRecord != row.tempRecord) &&
                    (originalRecord != row.oldRecord) && (originalRecord != row.newRecord)) {

                    FreeRecord(ref originalRecord);
                }
                // else during an event 'row.AcceptChanges(); row.BeginEdit(); row.EndEdit();'

                if (row.RowState == DataRowState.Detached && row.rowID != -1) {
                    RemoveRow(row, false);
                }
            }
        }

        private void RestoreShadowIndexes() {
            Debug.Assert(1 <= shadowCount, "unexpected negative shadow count");
            shadowCount--;
            if (0 == shadowCount) {
                shadowIndexes = null;
            }
        }

        private void SetShadowIndexes() {
            if (null == shadowIndexes) {
                Debug.Assert(0 == shadowCount, "unexpected count");
                shadowIndexes = LiveIndexes;
                shadowCount = 1;
            }
            else {
                Debug.Assert(1 <= shadowCount, "unexpected negative shadow count");
                shadowCount++;
            }
        }

        internal void ShadowIndexCopy(){
            if (shadowIndexes == indexes) {
                Debug.Assert(0 < indexes.Count, "unexpected");
                shadowIndexes = new List<Index>(indexes);
            }
        }

        /// <devdoc>
        /// <para>Returns the <see cref='System.Data.DataTable.TableName'/> and <see cref='System.Data.DataTable.DisplayExpression'/>, if there is one as a concatenated string.</para>
        /// </devdoc>
        public override string ToString() {
            if (this.displayExpression == null)
                return this.TableName;
            else
                return this.TableName + " + " + this.DisplayExpressionInternal;
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void BeginLoadData() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.BeginLoadData|API> %d#\n", ObjectID);
            try {
                if (inDataLoad)
                    return;

                inDataLoad = true;
                Debug.Assert(null == loadIndex, "loadIndex should already be null");
                loadIndex  = null;
                // LoadDataRow may have been called before BeginLoadData and already
                // initialized loadIndexwithOriginalAdded & loadIndexwithCurrentDeleted 

                initialLoad = (Rows.Count == 0);
                if(initialLoad) {
                    SuspendIndexEvents();
                } else {
                    if (primaryKey != null) {
                        loadIndex = primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows);
                    }
                    if(loadIndex != null) {
                        loadIndex.AddRef();
                    }
                }

                if (DataSet != null) {
                    savedEnforceConstraints = DataSet.EnforceConstraints;
                    DataSet.EnforceConstraints = false;
                }
                else {
                    this.EnforceConstraints = false;
                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public void EndLoadData() {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.EndLoadData|API> %d#\n", ObjectID);
            try {
                if (!inDataLoad)
                    return;

                if(loadIndex != null) {
                    loadIndex.RemoveRef();
                }
                if (loadIndexwithOriginalAdded  != null) {
                    loadIndexwithOriginalAdded.RemoveRef();
                }
                if (loadIndexwithCurrentDeleted  != null) {
                    loadIndexwithCurrentDeleted.RemoveRef();
                }

                loadIndex  = null;
                loadIndexwithOriginalAdded = null;
                loadIndexwithCurrentDeleted = null;

                inDataLoad = false;

                RestoreIndexEvents(false);

                if (DataSet != null)
                    DataSet.EnforceConstraints = savedEnforceConstraints;
                else
                    this.EnforceConstraints = true;
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        /// <devdoc>
        ///    <para>Finds and updates a specific row. If no matching
        ///       row is found, a new row is created using the given values.</para>
        /// </devdoc>
        public DataRow LoadDataRow(object[] values, bool fAcceptChanges) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.LoadDataRow|API> %d#, fAcceptChanges=%d{bool}\n", ObjectID, fAcceptChanges);
            try {
                DataRow row;
                if (inDataLoad) {
                    int record = NewRecordFromArray(values);
                    if (loadIndex != null) {
                        // not expecting LiveIndexes to clear the index we use between calls to LoadDataRow
                        Debug.Assert(2 <= loadIndex.RefCount, "bad loadIndex.RefCount");

                        int result = loadIndex.FindRecord(record);
                        if (result != -1) {
                            int resultRecord = loadIndex.GetRecord(result);
                            row = recordManager[resultRecord];
                            Debug.Assert (row != null, "Row can't be null for index record");
                            row.CancelEdit();
                            if (row.RowState == DataRowState.Deleted)
                                SetNewRecord(row, row.oldRecord, DataRowAction.Rollback, false, true);
                            SetNewRecord(row, record, DataRowAction.Change, false, true);
                            if (fAcceptChanges)
                                row.AcceptChanges();
                            return row;
                        }
                    }
                    row = NewRow(record);
                    AddRow(row);
                    if (fAcceptChanges)
                        row.AcceptChanges();
                    return row;
                }
                else {
                    // In case, BeginDataLoad is not called yet
                    row = UpdatingAdd(values);
                    if (fAcceptChanges)
                        row.AcceptChanges();
                    return row;
                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        /// <devdoc>
        ///    <para>Finds and updates a specific row. If no matching
        ///       row is found, a new row is created using the given values.</para>
        /// </devdoc>
        public DataRow LoadDataRow(object[] values, LoadOption loadOption) {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.LoadDataRow|API> %d#, loadOption=%d{ds.LoadOption}\n", ObjectID,  (int)loadOption);
            try {
                Index indextoUse = null;
                if (this.primaryKey != null) {
                    if (loadOption == LoadOption.Upsert) { // CurrentVersion, and Deleted
                        if (loadIndexwithCurrentDeleted == null) {
                            loadIndexwithCurrentDeleted = this.primaryKey.Key.GetSortIndex(DataViewRowState.CurrentRows |DataViewRowState.Deleted);
                            Debug.Assert(loadIndexwithCurrentDeleted != null, "loadIndexwithCurrentDeleted should not be null" );
                            if (loadIndexwithCurrentDeleted != null) {
                                loadIndexwithCurrentDeleted.AddRef();
                            }
                        }
                        indextoUse = loadIndexwithCurrentDeleted;
                    }
                    else {// CurrentVersion, and Deleted : OverwriteRow, PreserveCurrentValues
                        if (loadIndexwithOriginalAdded == null) {
                            loadIndexwithOriginalAdded  = this.primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows |DataViewRowState.Added);
                            Debug.Assert(loadIndexwithOriginalAdded != null, "loadIndexwithOriginalAdded should not be null");
                            if (loadIndexwithOriginalAdded != null) {
                                loadIndexwithOriginalAdded.AddRef();
                            }
                        }
                        indextoUse = loadIndexwithOriginalAdded;
                    }
                    // not expecting LiveIndexes to clear the index we use between calls to LoadDataRow
                    Debug.Assert(2 <= indextoUse.RefCount, "bad indextoUse.RefCount");
                }
                if(inDataLoad && !AreIndexEventsSuspended) { // we do not want to fire any listchanged in new Load/Fill
                    SuspendIndexEvents();// so suspend events here(not suspended == table already has some rows initially)
                }

                DataRow dataRow = LoadRow(values, loadOption, indextoUse);// if indextoUse == null, it means we dont have PK,
                                                                          // so LoadRow will take care of just adding the row to end

                return dataRow;
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        internal DataRow UpdatingAdd(object[] values) {
            Index index = null;
            if (this.primaryKey != null) {
                index = this.primaryKey.Key.GetSortIndex(DataViewRowState.OriginalRows);
            }

            if (index != null) {
                int record = NewRecordFromArray(values);
                int result = index.FindRecord(record);
                if (result != -1) {
                    int resultRecord = index.GetRecord(result);
                    DataRow row = this.recordManager[resultRecord];
                    Debug.Assert (row != null, "Row can't be null for index record");
                    row.RejectChanges();
                    this.SetNewRecord(row, record);
                    return row;
                }
                DataRow row2 = NewRow(record);
                Rows.Add(row2);
                return row2;
            }

            return Rows.Add(values);
        }

        internal bool UpdatingCurrent(DataRow row, DataRowAction action) {
            return(action == DataRowAction.Add || action == DataRowAction.Change ||
                   action == DataRowAction.Rollback || action == DataRowAction.ChangeOriginal ||
                   action == DataRowAction.ChangeCurrentAndOriginal);
//                (action == DataRowAction.Rollback && row.tempRecord != -1));
}

        internal DataColumn AddUniqueKey(int position) {
            if (_colUnique != null)
                return _colUnique;

            // check to see if we can use already existant PrimaryKey
            DataColumn[] pkey = PrimaryKey;
            if (pkey.Length == 1)
                // We have one-column primary key, so we can use it in our heirarchical relation
                return pkey[0];

            // add Unique, but not primaryKey to the table

            string keyName = XMLSchema.GenUniqueColumnName(TableName + "_Id", this);
            DataColumn key = new DataColumn(keyName, typeof(Int32), null, MappingType.Hidden);
            key.Prefix = tablePrefix;
            key.AutoIncrement = true;
            key.AllowDBNull = false;
            key.Unique = true;

            if (position == -1)
                Columns.Add(key);
            else { // we do have a problem and Imy idea is it is bug. Ask Enzo while Code review. Why we do not set ordinal when we call AddAt?
                for(int i = Columns.Count -1; i >= position; i--) {
                    this.Columns[i].SetOrdinalInternal(i+1);
                }
                Columns.AddAt(position, key);
                key.SetOrdinalInternal(position);
            }

            if (pkey.Length == 0)
                PrimaryKey = new DataColumn[] {
                    key
                };

            _colUnique = key;
            return _colUnique;
        }

        internal DataColumn AddUniqueKey() {
            return AddUniqueKey(-1);
        }

        internal DataColumn AddForeignKey(DataColumn parentKey) {
            Debug.Assert(parentKey != null, "AddForeignKey: Invalid paramter.. related primary key is null");

            string      keyName = XMLSchema.GenUniqueColumnName(parentKey.ColumnName, this);
            DataColumn  foreignKey = new DataColumn(keyName, parentKey.DataType, null, MappingType.Hidden);
            Columns.Add(foreignKey);

            return foreignKey;
        }

        internal void UpdatePropertyDescriptorCollectionCache() {
            propertyDescriptorCollectionCache = null;
        }

        /// <devdoc>
        ///     Retrieves an array of properties that the given component instance
        ///     provides.  This may differ from the set of properties the class
        ///     provides.  If the component is sited, the site may add or remove
        ///     additional properties.  The returned array of properties will be
        ///     filtered by the given set of attributes.
        /// </devdoc>
        internal PropertyDescriptorCollection GetPropertyDescriptorCollection(Attribute[] attributes) {
            if (propertyDescriptorCollectionCache == null) {
                int columnsCount   = Columns.Count;
                int relationsCount = ChildRelations.Count;
                PropertyDescriptor[] props = new PropertyDescriptor[columnsCount + relationsCount]; {
                    for (int i = 0; i < columnsCount; i++) {
                        props[i] = new DataColumnPropertyDescriptor(Columns[i]);
                    }
                    for (int i = 0; i < relationsCount; i++) {
                        props[columnsCount + i] = new DataRelationPropertyDescriptor(ChildRelations[i]);
                    }
                }
                propertyDescriptorCollectionCache = new PropertyDescriptorCollection(props);
            }
            return propertyDescriptorCollectionCache;
        }

        internal XmlQualifiedName TypeName {
            get {
                return ((typeName == null) ? XmlQualifiedName.Empty : (XmlQualifiedName)typeName);
            }
            set {
                typeName = value;
            }
        }

        public void Merge(DataTable table)
        {
            Merge(table, false, MissingSchemaAction.Add);
        }

        public void Merge(DataTable table, bool preserveChanges)
        {
            Merge(table, preserveChanges, MissingSchemaAction.Add);
        }

        public void Merge(DataTable table, bool preserveChanges, MissingSchemaAction missingSchemaAction)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Merge|API> %d#, table=%d, preserveChanges=%d{bool}, missingSchemaAction=%d{ds.MissingSchemaAction}\n", ObjectID, (table != null) ? table.ObjectID : 0, preserveChanges, (int)missingSchemaAction);
            try{
                if (table == null)
                    throw ExceptionBuilder.ArgumentNull("table");

                switch(missingSchemaAction) { // @perfnote: Enum.IsDefined
                case MissingSchemaAction.Add:
                case MissingSchemaAction.Ignore:
                case MissingSchemaAction.Error:
                case MissingSchemaAction.AddWithKey:
                    Merger merger = new Merger(this, preserveChanges, missingSchemaAction);
                    merger.MergeTable(table);
                    break;
                default:
                    throw Common.ADP.InvalidMissingSchemaAction(missingSchemaAction);
                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        public void Load (IDataReader reader){
            Load(reader, LoadOption.PreserveChanges, null);
        }

        public void Load (IDataReader reader, LoadOption loadOption) {
            Load(reader, loadOption, null);
        }

        public virtual void Load (IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler){
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.Load|API> %d#, loadOption=%d{ds.LoadOption}\n", ObjectID, (int)loadOption);
            try {
                if (this.PrimaryKey.Length == 0) {
                    DataTableReader dtReader = reader as DataTableReader;
                    if (dtReader != null && dtReader.CurrentDataTable == this)
                        return; // if not return, it will go to infinite loop
                }
                Common.LoadAdapter adapter = new Common.LoadAdapter();
                adapter.FillLoadOption = loadOption;
                adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                if (null != errorHandler) {
                    adapter.FillError += errorHandler;
                }
                adapter.FillFromReader(new DataTable[] { this }, reader, 0, 0);

                if (!reader.IsClosed && !reader.NextResult()) { // 
                    reader.Close();
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        private DataRow LoadRow(object[] values, LoadOption loadOption, Index searchIndex) {
            int recordNo;
            DataRow dataRow = null;

            if (searchIndex != null) {
                int[] primaryKeyIndex = new int[0];
                if (this.primaryKey != null) { // I do check above for PK, but in case if someone else gives me some index unrelated to PK
                    primaryKeyIndex = new int[this.primaryKey.ColumnsReference.Length];
                    for(int i = 0; i < this.primaryKey.ColumnsReference.Length; i++) {
                        primaryKeyIndex[i] = this.primaryKey.ColumnsReference[i].Ordinal;
                    }
                }

                object[] keys = new object[primaryKeyIndex.Length];
                for(int i = 0; i < primaryKeyIndex.Length; i++) {
                    keys[i] = values[primaryKeyIndex[i]];
                }

                Range result = searchIndex.FindRecords(keys);

                if (!result.IsNull) {
                    int deletedRowUpsertCount = 0;
                    for(int i = result.Min; i <= result.Max; i++) {
                        int resultRecord = searchIndex.GetRecord(i);
                        dataRow = this.recordManager[resultRecord];
                        recordNo = NewRecordFromArray(values);

                        //SQLBU DT 33648
                        // values array is being reused by DataAdapter, do not modify the values array
                        for(int count = 0; count < values.Length; count++) {
                            if (null == values[count]) {
                                columnCollection[count].Copy(resultRecord, recordNo);
                            }
                        }
                        for(int count = values.Length; count < columnCollection.Count ; count++) {
                            columnCollection[count].Copy(resultRecord, recordNo); // if there are missing values
                        }

                        if (loadOption != LoadOption.Upsert || dataRow.RowState != DataRowState.Deleted) {
                            SetDataRowWithLoadOption(dataRow , recordNo, loadOption, true);
                        }
                        else {
                            deletedRowUpsertCount++;
                        }
                    }
                    if (0 == deletedRowUpsertCount) {
                        return dataRow;
                    }
                }
            }

            recordNo = NewRecordFromArray(values);
            dataRow = NewRow(recordNo);
            // fire rowChanging event here
            DataRowAction action;
            DataRowChangeEventArgs drcevent = null;
            switch(loadOption) {
                case LoadOption.OverwriteChanges:
                case LoadOption.PreserveChanges:
                    action = DataRowAction.ChangeCurrentAndOriginal;
                    break;
                case LoadOption.Upsert:
                    action = DataRowAction.Add;
                    break;
                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }

            drcevent = RaiseRowChanging(null, dataRow, action);

            this.InsertRow (dataRow, -1, -1, false);
            switch(loadOption) {
                case LoadOption.OverwriteChanges:
                case LoadOption.PreserveChanges:
                    this.SetOldRecord(dataRow,  recordNo);
                    break;
                case LoadOption.Upsert:
                    break;
                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }
            RaiseRowChanged(drcevent, dataRow, action);

            return dataRow;
        }

        private void SetDataRowWithLoadOption (DataRow dataRow, int recordNo, LoadOption loadOption, bool checkReadOnly) {
            bool hasError = false;
            if (checkReadOnly) {
                foreach(DataColumn dc in this.Columns) {
                    if (dc.ReadOnly && !dc.Computed) {
                        switch(loadOption) {
                            case LoadOption.OverwriteChanges:
                                if ((dataRow[dc, DataRowVersion.Current] != dc[recordNo]) ||(dataRow[dc, DataRowVersion.Original] != dc[recordNo]))
                                    hasError = true;
                                break;
                            case LoadOption.Upsert:
                                if (dataRow[dc, DataRowVersion.Current] != dc[recordNo])
                                    hasError = true;
                                break;
                            case LoadOption.PreserveChanges:
                                if (dataRow[dc, DataRowVersion.Original] != dc[recordNo])
                                    hasError = true;
                                break;
                        }
                    }
                }
            } // No Event should be fired  in SenNewRecord and SetOldRecord
            // fire rowChanging event here

            DataRowChangeEventArgs drcevent = null;
            DataRowAction action = DataRowAction.Nothing;
            int cacheTempRecord = dataRow.tempRecord;
            dataRow.tempRecord = recordNo;

            switch(loadOption) {
                case LoadOption.OverwriteChanges:
                    action = DataRowAction.ChangeCurrentAndOriginal;
                    break;
                case LoadOption.Upsert:
                    switch(dataRow.RowState) {
                        case DataRowState.Unchanged:
                            // let see if the incomming value has the same values as existing row, so compare records
                            foreach(DataColumn dc in dataRow.Table.Columns) {
                                if (0 != dc.Compare(dataRow.newRecord, recordNo)) {
                                    action = DataRowAction.Change;
                                    break;
                                }
                            }
                            break;
                        case DataRowState.Deleted:
                            Debug.Assert(false, "LoadOption.Upsert with deleted row, should not be here");
                            break;
                        default :
                            action = DataRowAction.Change;
                            break;
                    }
                    break;
                case LoadOption.PreserveChanges:
                    switch(dataRow.RowState) {
                        case DataRowState.Unchanged:
                            action = DataRowAction.ChangeCurrentAndOriginal;
                            break;
                        default:
                            action = DataRowAction.ChangeOriginal;
                            break;
                    }
                    break;
                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }

            try {
                drcevent = RaiseRowChanging(null, dataRow, action);
                if (action == DataRowAction.Nothing) { // RaiseRowChanging does not fire for DataRowAction.Nothing
                    dataRow.inChangingEvent = true;
                    try {
                        drcevent = OnRowChanging(drcevent, dataRow, action);
                    }
                    finally {
                        dataRow.inChangingEvent = false;
                    }
                }
            }
            finally {
                Debug.Assert(dataRow.tempRecord == recordNo, "tempRecord has been changed in event handler");
                if (DataRowState.Detached == dataRow.RowState) {
                    // 'row.Table.Remove(row);'
                    if (-1 != cacheTempRecord) {
                        FreeRecord(ref cacheTempRecord);
                    }
                }
                else {
                    if (dataRow.tempRecord != recordNo) {
                        // 'row.EndEdit(); row.BeginEdit(); '
                        if (-1 != cacheTempRecord) {
                            FreeRecord(ref cacheTempRecord);
                        }
                        if (-1 != recordNo) {
                            FreeRecord(ref recordNo);
                        }
                        recordNo = dataRow.tempRecord;
                    }
                    else {
                        dataRow.tempRecord = cacheTempRecord;
                    }
                }
            }
            if (dataRow.tempRecord != -1) {
                dataRow.CancelEdit();
            }

            switch(loadOption) {
                case LoadOption.OverwriteChanges:
                     this.SetNewRecord(dataRow,  recordNo, DataRowAction.Change, false, false);
                     this.SetOldRecord(dataRow,  recordNo);
                     break;
                case LoadOption.Upsert:
                     if (dataRow.RowState == DataRowState.Unchanged) {
                         this.SetNewRecord(dataRow,  recordNo, DataRowAction.Change, false, false);
                         if (!dataRow.HasChanges()) {
                             this.SetOldRecord(dataRow, recordNo);
                         }
                     }
                     else {
                         if (dataRow.RowState == DataRowState.Deleted)
                             dataRow.RejectChanges();
                         this.SetNewRecord(dataRow,  recordNo, DataRowAction.Change, false, false);
                     }
                     break;
                case LoadOption.PreserveChanges:
                     if (dataRow.RowState == DataRowState.Unchanged) {
                         // SQLBU 500706: DataTable internal index is corrupted: '8'
                         // if ListChanged event deletes dataRow
                         this.SetOldRecord(dataRow,  recordNo); // do not fire event
                         this.SetNewRecord(dataRow, recordNo, DataRowAction.Change, false, false);
                     }
                     else { // if modified/ added / deleted we want this operation to fire event (just for LoadOption.PreserveCurrentValues)
                        this.SetOldRecord(dataRow,  recordNo);
                     }
                     break;
                default:
                    throw ExceptionBuilder.ArgumentOutOfRange("LoadOption");
            }

            if (hasError) {
                string error = Res.GetString(Res.Load_ReadOnlyDataModified);
                if (dataRow.RowError.Length == 0) { // WebData 112272, append the row error
                    dataRow.RowError = error;
                }
                else {
                    dataRow.RowError += " ]:[ " + error ;
                }

                foreach(DataColumn dc in this.Columns) {
                    if (dc.ReadOnly && !dc.Computed)
                        dataRow.SetColumnError(dc, error);
                }
            }

            drcevent = RaiseRowChanged(drcevent, dataRow, action);
            if (action == DataRowAction.Nothing) { // RaiseRowChanged does not fire for DataRowAction.Nothing
                dataRow.inChangingEvent = true;
                try {
                    OnRowChanged(drcevent, dataRow, action);
                }
                finally {
                    dataRow.inChangingEvent = false;
                }
            }

        }

        public  DataTableReader CreateDataReader() {
            return new DataTableReader(this);
        }


        public void WriteXml(Stream stream)
        {
            WriteXml(stream, XmlWriteMode.IgnoreSchema, false);
        }

        public void WriteXml(Stream stream, bool writeHierarchy)
        {
            WriteXml(stream, XmlWriteMode.IgnoreSchema, writeHierarchy);
        }

        public void WriteXml(TextWriter writer)
        {
            WriteXml(writer, XmlWriteMode.IgnoreSchema, false);
        }

        public void WriteXml(TextWriter writer, bool writeHierarchy)
        {
            WriteXml(writer, XmlWriteMode.IgnoreSchema, writeHierarchy);
        }

        public void WriteXml(XmlWriter writer)
        {
            WriteXml(writer, XmlWriteMode.IgnoreSchema, false);
        }

        public void WriteXml(XmlWriter writer, bool writeHierarchy)
        {
            WriteXml(writer, XmlWriteMode.IgnoreSchema, writeHierarchy);
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void WriteXml(String fileName)
        {
            WriteXml(fileName, XmlWriteMode.IgnoreSchema, false);
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void WriteXml(String fileName, bool writeHierarchy)
        {
            WriteXml(fileName, XmlWriteMode.IgnoreSchema, writeHierarchy);
        }

        public void WriteXml(Stream stream, XmlWriteMode mode)
        {
            WriteXml(stream, mode, false);
        }

        public void WriteXml(Stream stream, XmlWriteMode mode, bool writeHierarchy)
        {
            if (stream != null) {
                XmlTextWriter w =  new XmlTextWriter(stream, null) ;
                w.Formatting = Formatting.Indented;

                WriteXml( w, mode, writeHierarchy);
            }
        }

        public void WriteXml(TextWriter writer, XmlWriteMode mode)
        {
            WriteXml(writer, mode, false);
        }

        public void WriteXml(TextWriter writer, XmlWriteMode mode, bool writeHierarchy)
        {
            if (writer != null) {
                XmlTextWriter w =  new XmlTextWriter(writer) ;
                w.Formatting = Formatting.Indented;

                WriteXml(w, mode, writeHierarchy);
            }
        }

        public void WriteXml(XmlWriter writer, XmlWriteMode mode)
        {
            WriteXml(writer, mode, false);
        }
        public void WriteXml(XmlWriter writer, XmlWriteMode mode, bool writeHierarchy)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.WriteXml|API> %d#, mode=%d{ds.XmlWriteMode}\n", ObjectID,  (int)mode);
            try{
                if (this.tableName.Length == 0) {
                    throw ExceptionBuilder.CanNotSerializeDataTableWithEmptyName();
                }
                // Generate SchemaTree and write it out
                if (writer != null) {

                    if (mode == XmlWriteMode.DiffGram) { // FIX THIS
                        // Create and save the updates
                        new NewDiffgramGen(this, writeHierarchy).Save(writer, this);
                    }
                    else {
                        // Create and save xml data
                        if (mode == XmlWriteMode.WriteSchema) {
                            DataSet ds = null;
                            string tablenamespace = this.tableNamespace;
                            if (null == this.DataSet) {
                                ds = new DataSet();
                                // if user set values on DataTable, it isn't necessary
                                // to set them on the DataSet because they won't be inherited
                                // but it is simpler to set them in both places

                                // if user did not set values on DataTable, it is required
                                // to set them on the DataSet so the table will inherit
                                // the value already on the Datatable
                                ds.SetLocaleValue(_culture, _cultureUserSet);
                                ds.CaseSensitive = this.CaseSensitive;
                                ds.Namespace = this.Namespace;
                                ds.RemotingFormat = this.RemotingFormat;
                                ds.Tables.Add(this);
                            }

                            if (writer != null) {
                                XmlDataTreeWriter xmldataWriter = new XmlDataTreeWriter(this, writeHierarchy);
                                xmldataWriter.Save(writer, /*mode == XmlWriteMode.WriteSchema*/true);
                            }
                            if (null != ds) {
                                ds.Tables.Remove(this);
                                this.tableNamespace = tablenamespace;
                            }
                        }
                        else {
                            XmlDataTreeWriter xmldataWriter = new XmlDataTreeWriter(this, writeHierarchy);
                            xmldataWriter.Save(writer,/*mode == XmlWriteMode.WriteSchema*/ false);
                        }
                    }
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void WriteXml(String fileName, XmlWriteMode mode)
        {
            WriteXml(fileName, mode, false);
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void WriteXml(String fileName, XmlWriteMode mode, bool writeHierarchy)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.WriteXml|API> %d#, fileName='%ls', mode=%d{ds.XmlWriteMode}\n", ObjectID, fileName, (int)mode);
            try {
                using(XmlTextWriter xw = new XmlTextWriter( fileName, null )) {
                    xw.Formatting = Formatting.Indented;
                    xw.WriteStartDocument(true);

                    WriteXml(xw, mode, writeHierarchy);

                    xw.WriteEndDocument();
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        public void WriteXmlSchema(Stream stream) {
            WriteXmlSchema(stream, false);
        }

        public void WriteXmlSchema(Stream stream, bool writeHierarchy)
        {
            if (stream == null)
                return;

            XmlTextWriter w =  new XmlTextWriter(stream, null) ;
            w.Formatting = Formatting.Indented;

            WriteXmlSchema( w, writeHierarchy );
        }

        public void WriteXmlSchema( TextWriter writer ) {
            WriteXmlSchema( writer, false );
        }

        public void WriteXmlSchema( TextWriter writer, bool writeHierarchy )
        {

            if (writer == null)
                return;

            XmlTextWriter w =  new XmlTextWriter(writer);
            w.Formatting = Formatting.Indented;

            WriteXmlSchema( w, writeHierarchy );
        }

        private  bool CheckForClosureOnExpressions(DataTable dt, bool writeHierarchy) {
            List<DataTable> tableList = new List<DataTable>();
            tableList.Add(dt);
            if (writeHierarchy) { // WebData 112161
                CreateTableList(dt, tableList);
            }
            return CheckForClosureOnExpressionTables(tableList);
        }

        private bool CheckForClosureOnExpressionTables(List<DataTable> tableList) {
            Debug.Assert(tableList != null, "tableList shouldnot be null");

            foreach(DataTable datatable in tableList) {
                foreach(DataColumn dc in datatable.Columns) {
                    if (dc.Expression.Length != 0)  {
                        DataColumn[] dependency = dc.DataExpression.GetDependency();
                        for (int j = 0; j < dependency.Length; j++) {
                            if (!(tableList.Contains(dependency[j].Table))) {
                                return false;
                            }
                        }
                    }
                }
            }
            return true;
        }


        public void WriteXmlSchema(XmlWriter writer) {
            WriteXmlSchema(writer, false);
        }

        public void WriteXmlSchema(XmlWriter writer, bool writeHierarchy)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.WriteXmlSchema|API> %d#\n", ObjectID);
            try{
                if (this.tableName.Length == 0) {
                    throw ExceptionBuilder.CanNotSerializeDataTableWithEmptyName();
                }

                if (!CheckForClosureOnExpressions(this, writeHierarchy)) {
                    throw ExceptionBuilder.CanNotSerializeDataTableHierarchy();
                }

                DataSet ds = null;
                string tablenamespace = this.tableNamespace;//SQL BU Defect Tracking 286968

                // Generate SchemaTree and write it out
                if (null == this.DataSet) {
                    ds = new DataSet();
                    // if user set values on DataTable, it isn't necessary
                    // to set them on the DataSet because they won't be inherited
                    // but it is simpler to set them in both places

                    // if user did not set values on DataTable, it is required
                    // to set them on the DataSet so the table will inherit
                    // the value already on the Datatable
                    ds.SetLocaleValue(_culture, _cultureUserSet);
                    ds.CaseSensitive = this.CaseSensitive;
                    ds.Namespace = this.Namespace;
                    ds.RemotingFormat = this.RemotingFormat;
                    ds.Tables.Add(this);
                }

                if (writer != null) {
                    XmlTreeGen treeGen = new XmlTreeGen(SchemaFormat.Public);
                    treeGen.Save(null, this, writer, writeHierarchy);
                }
                if (null != ds) {
                    ds.Tables.Remove(this);
                    this.tableNamespace = tablenamespace;
                }
            }
            finally {
                Bid.ScopeLeave(ref hscp);
            }
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void WriteXmlSchema(String fileName) {
            WriteXmlSchema(fileName, false);
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void WriteXmlSchema(String fileName, bool writeHierarchy)
        {
            XmlTextWriter xw = new XmlTextWriter( fileName, null );
            try {
                xw.Formatting = Formatting.Indented;
                xw.WriteStartDocument(true);
                WriteXmlSchema(xw, writeHierarchy);
                xw.WriteEndDocument();
            }
            finally {
                xw.Close();
            }
        }

        public XmlReadMode ReadXml(Stream stream)
        {
            if (stream == null)
                return XmlReadMode.Auto;

            return ReadXml( new XmlTextReader(stream), false);
        }

        public XmlReadMode ReadXml(TextReader reader)
        {
            if (reader == null)
                return XmlReadMode.Auto;

            return ReadXml( new XmlTextReader(reader), false);
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public XmlReadMode ReadXml(string fileName)
        {
            XmlTextReader xr = new XmlTextReader(fileName);
            try {
                return ReadXml( xr , false);
            }
            finally {
                xr.Close();
            }
        }

        public XmlReadMode ReadXml(XmlReader reader)
        {
            return ReadXml(reader, false);
        }

        private void RestoreConstraint(bool originalEnforceConstraint) {
            if (this.DataSet != null) {
                this.DataSet.EnforceConstraints = originalEnforceConstraint;
            }
            else {
                this.EnforceConstraints = originalEnforceConstraint;
            }
        }

        private bool IsEmptyXml(XmlReader reader) {
            if (reader.IsEmptyElement) {
                if (reader.AttributeCount == 0 || (reader.LocalName == Keywords.DIFFGRAM && reader.NamespaceURI == Keywords.DFFNS)) {
                    return true;
                }
                if (reader.AttributeCount == 1) {
                    reader.MoveToAttribute(0);
                    if ((this.Namespace == reader.Value) &&
                        (this.Prefix == reader.LocalName) &&
                        (reader.Prefix == Keywords.XMLNS) &&
                        (reader.NamespaceURI == Keywords.XSD_XMLNS_NS))
                    return true;
                }
            }
            return false;
        }

        internal XmlReadMode ReadXml(XmlReader reader, bool denyResolving)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.ReadXml|INFO> %d#, denyResolving=%d{bool}\n", ObjectID, denyResolving);
            try {

              DataTable.RowDiffIdUsageSection rowDiffIdUsage = new DataTable.RowDiffIdUsageSection();
              try {
                bool fDataFound = false;
                bool fSchemaFound = false;
                bool fDiffsFound = false;
                bool fIsXdr = false;
                int iCurrentDepth = -1;
                XmlReadMode ret = XmlReadMode.Auto;

                // clear the hashtable to avoid conflicts between diffgrams, SqlHotFix 782
                rowDiffIdUsage.Prepare(this);

                if (reader == null)
                    return ret;
                bool originalEnforceConstraint = false;
                if (this.DataSet != null) {
                    originalEnforceConstraint = this.DataSet.EnforceConstraints;
                    this.DataSet.EnforceConstraints = false;
                }
                else {
                    originalEnforceConstraint = this.EnforceConstraints;
                    this.EnforceConstraints = false;
                }

                if (reader is XmlTextReader)
                    ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;

                XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema
                XmlDataLoader xmlload = null;


                reader.MoveToContent();
                if (Columns.Count == 0) {
                    if (IsEmptyXml(reader)) {
                        reader.Read();
                        return ret;
                    }
                }

                if (reader.NodeType == XmlNodeType.Element) {
                    iCurrentDepth = reader.Depth;

                    if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                        if (Columns.Count == 0) {
                            if (reader.IsEmptyElement) {
                                reader.Read();
                                return XmlReadMode.DiffGram;
                            }
                            throw ExceptionBuilder.DataTableInferenceNotSupported();
                        }
                        this.ReadXmlDiffgram(reader);
                        // read the closing tag of the current element
                        ReadEndElement(reader);

                        RestoreConstraint(originalEnforceConstraint);
                        return XmlReadMode.DiffGram;
                    }

                    // if reader points to the schema load it
                    if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                        // load XDR schema and exit
                        ReadXDRSchema(reader);

                        RestoreConstraint(originalEnforceConstraint);
                        return XmlReadMode.ReadSchema; //since the top level element is a schema return
                    }

                    if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                        // load XSD schema and exit
                        ReadXmlSchema(reader, denyResolving);
                        RestoreConstraint(originalEnforceConstraint);
                        return XmlReadMode.ReadSchema; //since the top level element is a schema return
                    }

                    if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal)) {
                        if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                            this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                        }
                        else {
                            this.enforceConstraints = originalEnforceConstraint;
                        }

                        throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                    }

                    // now either the top level node is a table and we load it through dataReader...

                    // ... or backup the top node and all its attributes because we may need to InferSchema
                    XmlElement topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    if (reader.HasAttributes) {
                        int attrCount = reader.AttributeCount;
                        for (int i=0;i<attrCount;i++) {
                            reader.MoveToAttribute(i);
                            if (reader.NamespaceURI.Equals(Keywords.XSD_XMLNS_NS))
                                topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                            else {
                                XmlAttribute attr = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                attr.Prefix = reader.Prefix;
                                attr.Value = reader.GetAttribute(i);
                            }
                        }
                    }
                    reader.Read();

                    while(MoveToElement(reader, iCurrentDepth)) {

                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            this.ReadXmlDiffgram(reader);
                            // read the closing tag of the current element
                            ReadEndElement(reader);
                            RestoreConstraint(originalEnforceConstraint);
                            return XmlReadMode.DiffGram;
                        }

                        // if reader points to the schema load it...


                        if (!fSchemaFound && !fDataFound && reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                            // load XDR schema and exit
                            ReadXDRSchema(reader);
                            fSchemaFound = true;
                            fIsXdr = true;
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                            // load XSD schema and exit
                            ReadXmlSchema(reader, denyResolving);
                            fSchemaFound = true;
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))  {
                            if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                                this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                            }
                            else {
                                this.enforceConstraints = originalEnforceConstraint;
                            }
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                        }

                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            this.ReadXmlDiffgram(reader);
                            fDiffsFound = true;
                            ret = XmlReadMode.DiffGram;
                        }
                        else {
                            // we found data here
                            fDataFound = true;

                            if (!fSchemaFound && Columns.Count == 0) {
                                XmlNode node = xdoc.ReadNode(reader);
                                topNode.AppendChild(node);
                            }
                            else {
                                if (xmlload == null)
                                    xmlload = new XmlDataLoader(this, fIsXdr, topNode, false);
                                xmlload.LoadData(reader);
                                if (fSchemaFound)
                                    ret = XmlReadMode.ReadSchema;
                                else
                                    ret = XmlReadMode.IgnoreSchema;
                            }
                        }

                    }
                    // read the closing tag of the current element
                    ReadEndElement(reader);

                    // now top node contains the data part
                    xdoc.AppendChild(topNode);

                    if (!fSchemaFound && Columns.Count == 0) {
                        if (IsEmptyXml(reader)) {
                            reader.Read();
                            return ret;
                        }
                        throw ExceptionBuilder.DataTableInferenceNotSupported();
                    }

                    if (xmlload == null)
                        xmlload = new XmlDataLoader(this, fIsXdr, false);

                    // so we InferSchema
                    if (!fDiffsFound) {// we need to add support for inference here
                    }
                }
                RestoreConstraint(originalEnforceConstraint);
                return ret;
              }
              finally {
                rowDiffIdUsage.Cleanup();
              }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        internal XmlReadMode ReadXml(XmlReader reader, XmlReadMode mode, bool denyResolving)
        {
            DataTable.RowDiffIdUsageSection rowDiffIdUsage = new DataTable.RowDiffIdUsageSection();
            try {
                bool fSchemaFound = false;
                bool fDataFound = false;
                bool fIsXdr = false;
                int iCurrentDepth = -1;
                XmlReadMode ret = mode;

                // Dev11 904428: prepare and cleanup rowDiffId hashtable
                rowDiffIdUsage.Prepare(this);

                if (reader == null)
                    return ret;

                bool originalEnforceConstraint  = false;
                if (this.DataSet != null) {
                    originalEnforceConstraint  = this.DataSet.EnforceConstraints;
                    this.DataSet.EnforceConstraints = false;
                }
                else {
                    originalEnforceConstraint  = this.EnforceConstraints;
                    this.EnforceConstraints = false;
                }

                if (reader is XmlTextReader)
                    ((XmlTextReader) reader).WhitespaceHandling = WhitespaceHandling.Significant;

                XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema

                if ((mode != XmlReadMode.Fragment) && (reader.NodeType == XmlNodeType.Element))
                    iCurrentDepth = reader.Depth;

                reader.MoveToContent();
                if (Columns.Count == 0) {
                    if (IsEmptyXml(reader)) {
                        reader.Read();
                        return ret;
                    }
                }

                XmlDataLoader xmlload = null;

                if (reader.NodeType == XmlNodeType.Element) {
                    XmlElement topNode = null;
                    if (mode == XmlReadMode.Fragment) {
                        xdoc.AppendChild(xdoc.CreateElement("ds_sqlXmlWraPPeR"));
                        topNode = xdoc.DocumentElement;
                    }
                    else { //handle the top node
                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema)) {
                                if (Columns.Count == 0) {
                                    if(reader.IsEmptyElement) {
                                        reader.Read();
                                        return XmlReadMode.DiffGram;
                                    }
                                    throw ExceptionBuilder.DataTableInferenceNotSupported();
                                }
                                this.ReadXmlDiffgram(reader);
                                // read the closing tag of the current element
                                ReadEndElement(reader);
                            }
                            else {
                                reader.Skip();
                            }
                            RestoreConstraint(originalEnforceConstraint);
                            return ret;
                        }

                        if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                            // load XDR schema and exit
                            if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema)) {
                                ReadXDRSchema(reader);
                            }
                            else {
                                reader.Skip();
                            }
                            RestoreConstraint(originalEnforceConstraint);
                            return ret; //since the top level element is a schema return
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                            // load XSD schema and exit
                            if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))  {
                                ReadXmlSchema(reader, denyResolving);
                            }
                            else
                                reader.Skip();
                            RestoreConstraint(originalEnforceConstraint);
                            return ret; //since the top level element is a schema return
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))  {
                             if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                                 this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                             }
                             else {
                                this.enforceConstraints = originalEnforceConstraint;
                            }
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                        }

                        // now either the top level node is a table and we load it through dataReader...
                        // ... or backup the top node and all its attributes
                        topNode = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                        if (reader.HasAttributes) {
                            int attrCount = reader.AttributeCount;
                            for (int i=0;i<attrCount;i++) {
                                reader.MoveToAttribute(i);
                                if (reader.NamespaceURI.Equals(Keywords.XSD_XMLNS_NS))
                                    topNode.SetAttribute(reader.Name, reader.GetAttribute(i));
                                else {
                                    XmlAttribute attr = topNode.SetAttributeNode(reader.LocalName, reader.NamespaceURI);
                                    attr.Prefix = reader.Prefix;
                                    attr.Value = reader.GetAttribute(i);
                                }
                            }
                        }
                        reader.Read();
                    }

                    while(MoveToElement(reader, iCurrentDepth)) {

                        if (reader.LocalName == Keywords.XDR_SCHEMA && reader.NamespaceURI==Keywords.XDRNS) {
                            // load XDR schema
                       	    if (!fSchemaFound && !fDataFound && (mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))  {
                                ReadXDRSchema(reader);
                                fSchemaFound = true;
                                fIsXdr = true;
                            }
                            else {
                                reader.Skip();
                            }
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                        // load XSD schema and exit
                            if ((mode != XmlReadMode.IgnoreSchema) && (mode != XmlReadMode.InferSchema))  {
                                ReadXmlSchema(reader, denyResolving);
                                fSchemaFound = true;
                            }
                            else {
                                reader.Skip();
                            }
                            continue;
                        }

                        if ((reader.LocalName == Keywords.DIFFGRAM) && (reader.NamespaceURI == Keywords.DFFNS)) {
                            if ((mode == XmlReadMode.DiffGram) || (mode == XmlReadMode.IgnoreSchema)) {
                                if (Columns.Count == 0) {
                                    if(reader.IsEmptyElement) {
                                        reader.Read();
                                        return XmlReadMode.DiffGram;
                                    }
                                    throw ExceptionBuilder.DataTableInferenceNotSupported();
                                }
                                this.ReadXmlDiffgram(reader);
                                ret = XmlReadMode.DiffGram;
                            }
                            else {
                                reader.Skip();
                            }
                            continue;
                        }

                        if (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI.StartsWith(Keywords.XSD_NS_START, StringComparison.Ordinal))  {
                            if (this.DataSet != null) { // we should not throw for constraint, we already will throw for unsupported schema, so restore enforce cost, but not via property
                                 this.DataSet.RestoreEnforceConstraints(originalEnforceConstraint);
                            }
                            else {
                                this.enforceConstraints = originalEnforceConstraint;
                            }
                            throw ExceptionBuilder.DataSetUnsupportedSchema(Keywords.XSDNS);
                        }

                        if (mode == XmlReadMode.DiffGram) {
                            reader.Skip();
                            continue; // we do not read data in diffgram mode
                        }

                        // if we are here we found some data
                        fDataFound = true;

                        if (mode == XmlReadMode.InferSchema) { //save the node in DOM until the end;
                            XmlNode node = xdoc.ReadNode(reader);
                            topNode.AppendChild(node);
                        }
                        else {
                            if (Columns.Count == 0) {
                                throw ExceptionBuilder.DataTableInferenceNotSupported();
                            }
                            if (xmlload == null)
                                xmlload = new XmlDataLoader(this, fIsXdr, topNode,  mode == XmlReadMode.IgnoreSchema);
                            xmlload.LoadData(reader);
                        }
                    } //end of the while

                    // read the closing tag of the current element
                    ReadEndElement(reader);

                    // now top node contains the data part
                    xdoc.AppendChild(topNode);

                    if (xmlload == null)
                        xmlload = new XmlDataLoader(this, fIsXdr, mode == XmlReadMode.IgnoreSchema);

                    if (mode == XmlReadMode.DiffGram) {
                        // we already got the diffs through XmlReader interface
                        RestoreConstraint(originalEnforceConstraint);
                        return ret;
                    }
    //todo
                    // Load Data
                    if (mode == XmlReadMode.InferSchema) {
                        if (Columns.Count == 0) {
                            throw ExceptionBuilder.DataTableInferenceNotSupported();
                        }

    // [....]                    xmlload.InferSchema(xdoc, null);
    // [....]                    xmlload.LoadData(xdoc);
                    }
                }
                RestoreConstraint(originalEnforceConstraint);

                return ret;
            }
            finally {
                // Dev11 904428: prepare and cleanup rowDiffId hashtable
                rowDiffIdUsage.Cleanup();
            }
        }


        internal void ReadEndElement(XmlReader reader) {
            while (reader.NodeType == XmlNodeType.Whitespace) {
                reader.Skip();
            }
            if (reader.NodeType == XmlNodeType.None) {
                reader.Skip();
            }
            else if (reader.NodeType == XmlNodeType.EndElement) {
                reader.ReadEndElement();
            }
        }
        internal void ReadXDRSchema(XmlReader reader) {
            XmlDocument xdoc = new XmlDocument(); // we may need this to infer the schema
            XmlNode schNode = xdoc.ReadNode(reader);;
            //consume and ignore it - No support
        }

        internal bool MoveToElement(XmlReader reader, int depth) {
            while (!reader.EOF && reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.Element && reader.Depth > depth) {
                reader.Read();
            }
            return (reader.NodeType == XmlNodeType.Element);
        }
        private void ReadXmlDiffgram(XmlReader reader) { // fill correctly
            int d = reader.Depth;
            bool fEnforce = this.EnforceConstraints;
            this.EnforceConstraints =false;
            DataTable newDt;
            bool isEmpty;

            if (this.Rows.Count == 0) {
                isEmpty = true;
                newDt = this;
            }
            else {
                isEmpty = false;
                newDt = this.Clone();
                newDt.EnforceConstraints = false;
            }

            newDt.Rows.nullInList = 0;

            reader.MoveToContent();

            if ((reader.LocalName != Keywords.DIFFGRAM) && (reader.NamespaceURI != Keywords.DFFNS))
                return;
            reader.Read();
            if (reader.NodeType == XmlNodeType.Whitespace) {
                MoveToElement(reader, reader.Depth - 1 /*iCurrentDepth*/); // skip over whitespaces.
            }

            newDt.fInLoadDiffgram = true;

            if (reader.Depth > d) {
                if ((reader.NamespaceURI != Keywords.DFFNS) && (reader.NamespaceURI != Keywords.MSDNS)) {
                    //we should be inside the dataset part
                    XmlDocument xdoc = new XmlDocument();
                    XmlElement node = xdoc.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI);
                    reader.Read();
                    if (reader.Depth-1 > d) {
                        XmlDataLoader xmlload = new XmlDataLoader(newDt, false, node, false);
                        xmlload.isDiffgram = true; // turn on the special processing
                        xmlload.LoadData(reader);
                    }
                    ReadEndElement(reader);
                }
                if (((reader.LocalName == Keywords.SQL_BEFORE) && (reader.NamespaceURI == Keywords.DFFNS)) ||
                    ((reader.LocalName == Keywords.MSD_ERRORS) && (reader.NamespaceURI == Keywords.DFFNS)))

                {
                    //this will consume the changes and the errors part
                    XMLDiffLoader diffLoader = new XMLDiffLoader();
                    diffLoader.LoadDiffGram(newDt, reader);
                }

                // get to the closing diff tag
                while(reader.Depth > d) {
                    reader.Read();
                }
                // read the closing tag
                ReadEndElement(reader);
            }

            if (newDt.Rows.nullInList > 0)
                throw ExceptionBuilder.RowInsertMissing(newDt.TableName);


            newDt.fInLoadDiffgram = false;
            List<DataTable> tableList = new List<DataTable>();
            tableList.Add(this);
            CreateTableList(this, tableList);

// this is terrible, optimize it
            for (int i = 0; i < tableList.Count ; i++) {
                DataRelation[] relations = tableList[i].NestedParentRelations;
                foreach(DataRelation rel in relations) {
                    if (rel != null && rel.ParentTable == tableList[i]) {
                        foreach (DataRow r in tableList[i].Rows) {
                            foreach (DataRelation rel2 in relations) {
                                r.CheckForLoops(rel2);
                            }
                        }
                    }
                }
            }

            if (!isEmpty) {
                this.Merge(newDt);
            }
            this.EnforceConstraints = fEnforce;
        }

        internal void ReadXSDSchema(XmlReader reader, bool denyResolving) {
            XmlSchemaSet sSet = new XmlSchemaSet();
            while (reader.LocalName == Keywords.XSD_SCHEMA && reader.NamespaceURI==Keywords.XSDNS) {
                XmlSchema s = XmlSchema.Read(reader, null);
                sSet.Add(s);
                //read the end tag
                ReadEndElement(reader);
            }
            sSet.Compile();

            XSDSchema schema = new XSDSchema();
            schema.LoadSchema(sSet, this);
        }


        public void ReadXmlSchema(Stream stream)
        {
            if (stream == null)
                return;

            ReadXmlSchema( new XmlTextReader( stream ), false );
        }

        public void ReadXmlSchema(TextReader reader)
        {
            if (reader == null)
                return;

            ReadXmlSchema( new XmlTextReader( reader ), false );
        }

        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        public void ReadXmlSchema(String fileName)
        {
            XmlTextReader xr = new XmlTextReader(fileName);
            try {
                ReadXmlSchema( xr, false );
            }
            finally {
                xr.Close();
            }
        }

        public void ReadXmlSchema(XmlReader reader)
        {
            ReadXmlSchema(reader, false);
        }

        internal void ReadXmlSchema(XmlReader reader, bool denyResolving)
        {
            IntPtr hscp;
            Bid.ScopeEnter(out hscp, "<ds.DataTable.ReadXmlSchema|INFO> %d#, denyResolving=%d{bool}\n", ObjectID, denyResolving);
            try{
                DataSet ds = new DataSet();
                SerializationFormat cachedRemotingFormat = this.RemotingFormat;
                // fxcop: ReadXmlSchema will provide the CaseSensitive, Locale, Namespace information
                ds.ReadXmlSchema(reader, denyResolving);

                string CurrentTableFullName = ds.MainTableName;

                if (Common.ADP.IsEmpty(this.tableName) && Common.ADP.IsEmpty(CurrentTableFullName))
                    return;

                DataTable currentTable = null;

                if (!Common.ADP.IsEmpty(this.tableName)) {
                    if (!Common.ADP.IsEmpty(this.Namespace)) {
                        currentTable = ds.Tables[this.tableName, this.Namespace];
                    }
                    else {//SQL BU defect tracking 240293
                        int tableIndex = ds.Tables.InternalIndexOf(this.tableName);
                        if (tableIndex  > -1) {
                            currentTable = ds.Tables[tableIndex];
                        }
                    }
                }
                else{  //!Common.ADP.IsEmpty(CurrentTableFullName)
                    string CurrentTableNamespace = "";
                    int nsSeperator = CurrentTableFullName.IndexOf(':');
                    if (nsSeperator > -1) {
                        CurrentTableNamespace = CurrentTableFullName.Substring(0, nsSeperator);
                    }
                    string CurrentTableName = CurrentTableFullName.Substring(nsSeperator + 1, CurrentTableFullName.Length - nsSeperator -1);

                    currentTable = ds.Tables[CurrentTableName, CurrentTableNamespace];
                }

                if (currentTable == null) { // bug fix :99186
                    string qTableName = string.Empty;
                    if (!Common.ADP.IsEmpty(this.tableName)) {
                        qTableName = (this.Namespace.Length > 0)? (this.Namespace + ":" + this.tableName):this.tableName;
                    }
                    else {
                        qTableName = CurrentTableFullName ;
                    }
                    throw ExceptionBuilder.TableNotFound(qTableName);
                }

                currentTable._remotingFormat = cachedRemotingFormat;

                List<DataTable> tableList = new List<DataTable>();
                tableList.Add(currentTable);
                CreateTableList(currentTable, tableList);
                List<DataRelation> relationList = new List<DataRelation>();
                CreateRelationList(tableList, relationList);

                if (relationList.Count == 0) {
                    if (this.Columns.Count == 0) {
                        DataTable tempTable = currentTable;
                        if (tempTable != null)
                            tempTable.CloneTo(this, null, false); // we may have issue Amir
                        if (this.DataSet == null && this.tableNamespace == null) { // webdata 105506
// for standalone table, clone wont get these correctly, since they may come with inheritance
                            this.tableNamespace =  tempTable.Namespace;
                        }
                    }
                    return;
                }
                else {
                    if (Common.ADP.IsEmpty(this.TableName)) {
                        this.TableName = currentTable.TableName;
                        if (!Common.ADP.IsEmpty(currentTable.Namespace)) {
                            this.Namespace = currentTable.Namespace;
                        }
                    }
                    if (this.DataSet == null) {
                        DataSet dataset = new DataSet(ds.DataSetName);
// webdata 105506
                        // if user set values on DataTable, it isn't necessary
                        // to set them on the DataSet because they won't be inherited
                        // but it is simpler to set them in both places

                        // if user did not set values on DataTable, it is required
                        // to set them on the DataSet so the table will inherit
                        // the value already on the Datatable
                        dataset.SetLocaleValue(ds.Locale, ds.ShouldSerializeLocale());
                        dataset.CaseSensitive = ds.CaseSensitive;
                        dataset.Namespace = ds.Namespace;
                        dataset.mainTableName = ds.mainTableName;
                        dataset.RemotingFormat = ds.RemotingFormat;

                        dataset.Tables.Add(this);
                    }

                    DataTable targetTable = CloneHierarchy(currentTable, this.DataSet, null);

                    foreach(DataTable tempTable in tableList) {
                        DataTable destinationTable = this.DataSet.Tables[tempTable.tableName, tempTable.Namespace];
                        DataTable sourceTable = ds.Tables[tempTable.tableName, tempTable.Namespace];
                        foreach(Constraint tempConstrain in sourceTable.Constraints) {
                            ForeignKeyConstraint fkc = tempConstrain as ForeignKeyConstraint;  // we have already cloned the UKC when cloning the datatable
                            if (fkc != null) {
                                if (fkc.Table != fkc.RelatedTable)  {
                                    if (tableList.Contains(fkc.Table) && tableList.Contains(fkc.RelatedTable)) {
                                        ForeignKeyConstraint newFKC = (ForeignKeyConstraint)fkc.Clone(destinationTable.DataSet);
                                        if (!destinationTable.Constraints.Contains(newFKC.ConstraintName))
                                            destinationTable.Constraints.Add(newFKC); // we know that the dest table is already in the table
                                    }
                                }
                           }
                        }
                    }
                    foreach(DataRelation rel in relationList) {
                        if (!this.DataSet.Relations.Contains(rel.RelationName))
                            this.DataSet.Relations.Add(rel.Clone(this.DataSet));
                    }

                    bool hasExternaldependency = false;

                    foreach(DataTable tempTable in tableList) {
                        foreach(DataColumn dc in tempTable.Columns) {
                            hasExternaldependency = false;
                            if (dc.Expression.Length  != 0) {
                                DataColumn[] dependency = dc.DataExpression.GetDependency();
                                for (int j = 0; j < dependency.Length; j++) {
                                    if (!tableList.Contains(dependency[j].Table)) {
                                        hasExternaldependency = true;
                                        break;
                                    }
                                }
                            }
                            if (!hasExternaldependency) {
                                this.DataSet.Tables[tempTable.TableName, tempTable.Namespace].Columns[dc.ColumnName].Expression = dc.Expression;
                            }
                        }
                        hasExternaldependency = false;
                    }

                }
            }
            finally{
                Bid.ScopeLeave(ref hscp);
            }
        }

        private void CreateTableList(DataTable currentTable, List<DataTable> tableList) {
            foreach( DataRelation r in currentTable.ChildRelations ) {
                if (! tableList.Contains(r.ChildTable)) {
                    tableList.Add(r.ChildTable);
                    CreateTableList(r.ChildTable, tableList);
                }
            }
        }
        private void CreateRelationList(List<DataTable> tableList,  List<DataRelation> relationList) {
            foreach(DataTable table in tableList) {
                foreach( DataRelation r in table.ChildRelations) {
                    if (tableList.Contains(r.ChildTable) && tableList.Contains(r.ParentTable)) {
                        relationList.Add(r);
                    }
                }
            }
        }

        /**************************************************************************
        The V2.0 (no V1.0 or V1.1) WSDL for Untyped DataTable being returned as a result (no parameters)
        <s:element name="anyUserSpecifiedMethodName">
            <!--  This is where parameters go -->
            <s:complexType /> 
        </s:element>
        <s:element name="anyUserSpecifiedMethodName"+"Response">
            <s:complexType>
                <s:sequence>
                    <s:element minOccurs="0" maxOccurs="1" name="anyUserSpecifiedMethodName"+"Result">
                        <s:complexType>
                            <s:sequence>
                                <s:any minOccurs="0" maxOccurs="unbounded" namespace="http://www.w3.org/2001/XMLSchema" processContents="lax" /> 
                                <s:any minOccurs="1" namespace="urn:schemas-microsoft-com:xml-diffgram-v1" processContents="lax" /> 
                            </s:sequence>
                        </s:complexType>
                    </s:element>
                </s:sequence>
            </s:complexType>
        </s:element>

        Typed DataTable is not supported in WSDL (SQLBU 444636)

        either fails because xsd generates its typed DataTable with an internal parameterless ctor
        
        or System.NullReferenceException: Object reference not set to an instance of an object.   (if namespace of StronglyTyped DataTable is not set)
           at System.Data.XmlTreeGen.FindTargetNamespace(DataTable table)

        or System.InvalidOperationException: Schema Id is missing. The schema returned from WebServiceDataSetServer.Service+StudentsDataTable.GetSchema() must have an Id.
           at System.Xml.Serialization.SerializableMapping.RetrieveSerializableSchema()
        *****************************************************************************/
        public static XmlSchemaComplexType GetDataTableSchema(XmlSchemaSet schemaSet) {
            XmlSchemaComplexType type = new XmlSchemaComplexType();
            XmlSchemaSequence sequence = new XmlSchemaSequence();
            XmlSchemaAny any = new XmlSchemaAny();
            any.Namespace = XmlSchema.Namespace;
            any.MinOccurs = 0;
            any.MaxOccurs = Decimal.MaxValue;
            any.ProcessContents = XmlSchemaContentProcessing.Lax;
            sequence.Items.Add(any);

            any = new XmlSchemaAny();
            any.Namespace = Keywords.DFFNS;
            any.MinOccurs = 1; // when recognizing WSDL - MinOccurs="0" denotes DataSet, a MinOccurs="1" for DataTable
            any.ProcessContents = XmlSchemaContentProcessing.Lax;
            sequence.Items.Add(any);

            type.Particle = sequence;

            return type;
        }

        XmlSchema IXmlSerializable.GetSchema() {
            return GetSchema();
        }

        protected virtual XmlSchema GetSchema() {
            if (GetType() == typeof(DataTable)) {
                return null;
            }
            MemoryStream stream = new MemoryStream();

            XmlWriter writer = new XmlTextWriter(stream, null);
            if (writer != null) {
                 (new XmlTreeGen(SchemaFormat.WebService)).Save(this, writer);
            }
            stream.Position = 0;
            return XmlSchema.Read(new XmlTextReader(stream), null);
        }

        void IXmlSerializable.ReadXml(XmlReader reader) {
            IXmlTextParser textReader = reader as IXmlTextParser;
            bool fNormalization = true;
            if (textReader != null) {
                fNormalization = textReader.Normalized;
                textReader.Normalized = false;
            }
            ReadXmlSerializable(reader);

            if (textReader != null) {
                textReader.Normalized = fNormalization;
            }
        }

        void IXmlSerializable.WriteXml(XmlWriter writer) {
            WriteXmlSchema(writer, false);
            WriteXml(writer, XmlWriteMode.DiffGram, false);
        }

         protected virtual void ReadXmlSerializable(XmlReader reader) {
            ReadXml(reader, XmlReadMode.DiffGram, true);
        }

/*
        [
        DefaultValue(false),
        ResCategoryAttribute(Res.DataCategory_Data),
        ResDescriptionAttribute(Res.DataTableSerializeHierarchy)
        ]
        public bool SerializeHierarchy {
            get {
                return this.serializeHierarchy;
            }
            set {
                this.serializeHierarchy = value;
            }
        }
*/

        // RowDiffIdUsageSection & DSRowDiffIdUsageSection Usage:
        //
        //        DataTable.[DS]RowDiffIdUsageSection rowDiffIdUsage = new DataTable.[DS]RowDiffIdUsageSection();
        //        try {
        //            rowDiffIdUsage.Prepare(DataTable or DataSet, depending on type);
        //
        //            // code that requires RowDiffId usage
        //
        //        }
        //        finally {
        //            rowDiffIdUsage.Cleanup();
        //        }
        // 
        // Nested calls are allowed on different tables. For example, user can register to row change events and trigger 
        // ReadXml on different table/ds). But, if user code will try to call ReadXml on table that is already in the scope,
        // this code will assert since nested calls on same table are unsupported.
        internal struct RowDiffIdUsageSection
        {
#if DEBUG
            // This list contains tables currently used in diffgram processing, not including new tables that might be added later during.
            // if diffgram processing is not started, this value must be null. when it starts, relevant method should call Prepare.
            // Notes:
            // * in case of ReadXml on empty DataSet, this list can be initialized as empty (so empty list != null).
            // * only one scope is allowed on single thread, either for datatable or dataset
            // * assert is triggered if same table is added to this list twice
            // 
            // do not allocate TLS data in RETAIL bits!
            [ThreadStatic]
            internal static List<DataTable> s_usedTables;
#endif //DEBUG

            DataTable _targetTable;

            internal void Prepare(DataTable table)
            {
                Debug.Assert(_targetTable == null, "do not reuse this section");
                Debug.Assert(table != null);
                Debug.Assert(table.rowDiffId == null, "rowDiffId wasn't previously cleared");
#if DEBUG
                Debug.Assert(s_usedTables == null || !s_usedTables.Contains(table), 
                    "Nested call with same table can cause data corruption!");
#endif //DEBUG

#if DEBUG
                if (s_usedTables == null)
                    s_usedTables = new List<DataTable>();
                s_usedTables.Add(table);
#endif //DEBUG
                _targetTable = table;
                table.rowDiffId = null;
            }

            [Conditional("DEBUG")]
            internal void Cleanup()
            {
                // cannot assume target table was set - ThreadAbortException can be raised before Start is called
                if (_targetTable != null)
                {
#if DEBUG
                    Debug.Assert(s_usedTables != null && s_usedTables.Contains(_targetTable), "missing Prepare before Cleanup");
                    if (s_usedTables != null)
                    {
                        s_usedTables.Remove(_targetTable);
                        if (s_usedTables.Count == 0)
                            s_usedTables = null;
                    }
#endif //DEBUG
                    _targetTable.rowDiffId = null;
                }
            }

            [Conditional("DEBUG")]
            internal static void Assert(string message)
            {
#if DEBUG
                // this code asserts scope was created, but it does not assert that the table was included in it
                // note that in case of DataSet, new tables might be added to the list in which case they won't appear in s_usedTables.
                Debug.Assert(s_usedTables != null, message);
#endif //DEBUG
            }
        }

        internal struct DSRowDiffIdUsageSection
        {
            DataSet _targetDS;

            internal void Prepare(DataSet ds)
            {
                Debug.Assert(_targetDS == null, "do not reuse this section");
                Debug.Assert(ds != null);

                _targetDS = ds;
#if DEBUG
                // initialize list of tables out of current tables
                // note: it might remain empty (still initialization is needed for assert to operate)
                if (RowDiffIdUsageSection.s_usedTables == null)
                    RowDiffIdUsageSection.s_usedTables = new List<DataTable>();
#endif //DEBUG
                for (int tableIndex = 0; tableIndex < ds.Tables.Count; ++tableIndex)
                {
                    DataTable table = ds.Tables[tableIndex];
#if DEBUG
                    Debug.Assert(!RowDiffIdUsageSection.s_usedTables.Contains(table), "Nested call with same table can cause data corruption!");
                    RowDiffIdUsageSection.s_usedTables.Add(table);
#endif //DEBUG
                    Debug.Assert(table.rowDiffId == null, "rowDiffId wasn't previously cleared");
                    table.rowDiffId = null;
                }
            }

            [Conditional("DEBUG")]
            internal void Cleanup()
            {
                // cannot assume target was set - ThreadAbortException can be raised before Start is called
                if (_targetDS != null)
                {
#if DEBUG
                    Debug.Assert(RowDiffIdUsageSection.s_usedTables != null, "missing Prepare before Cleanup");
#endif //DEBUG

                    for (int tableIndex = 0; tableIndex < _targetDS.Tables.Count; ++tableIndex)
                    {
                        DataTable table = _targetDS.Tables[tableIndex];
#if DEBUG
                        // cannot assert that table exists in the usedTables - new tables might be 
                        // created during diffgram processing in DataSet.ReadXml.
                        if (RowDiffIdUsageSection.s_usedTables != null)
                            RowDiffIdUsageSection.s_usedTables.Remove(table);
#endif //DEBUG
                        table.rowDiffId = null;
                    }
#if DEBUG
                    if (RowDiffIdUsageSection.s_usedTables != null && RowDiffIdUsageSection.s_usedTables.Count == 0)
                        RowDiffIdUsageSection.s_usedTables = null; // out-of-scope
#endif //DEBUG
                }
            }
        }

        internal Hashtable RowDiffId {
            get {
                // assert scope has been created either with RowDiffIdUsageSection.Prepare or DSRowDiffIdUsageSection.Prepare
                RowDiffIdUsageSection.Assert("missing call to RowDiffIdUsageSection.Prepare or DSRowDiffIdUsageSection.Prepare");

                if (rowDiffId == null)
                    rowDiffId = new Hashtable();
                return rowDiffId;
            }
        }

        internal int ObjectID {
            get {
                return _objectID;
            }
        }

        internal void AddDependentColumn(DataColumn expressionColumn) {
            if (dependentColumns == null)
                dependentColumns = new List<DataColumn>();

            if (!dependentColumns.Contains(expressionColumn)) {
                // only remember unique columns but expect non-unique columns to be added
                dependentColumns.Add(expressionColumn);
            }
        }

        internal void RemoveDependentColumn(DataColumn expressionColumn) {
            if (dependentColumns != null && dependentColumns.Contains(expressionColumn)) {
                dependentColumns.Remove(expressionColumn);
            }
        }

        internal void EvaluateExpressions() {
            //evaluates all expressions for all rows in table
            // SQLBU 414992: Serious performance issue when calling Merge
            // this improves performance by only computing expressions when they are present
            // and iterating over the rows instead of computing their position multiple times
            if ((null != dependentColumns) && (0 < dependentColumns.Count)) {
                foreach(DataRow row in Rows) {
                    // only evaluate original values if different from current.
                    if (row.oldRecord != -1 && row.oldRecord != row.newRecord) {
                        EvaluateDependentExpressions(dependentColumns, row, DataRowVersion.Original, null);
                    }
                    if (row.newRecord != -1) {
                        EvaluateDependentExpressions(dependentColumns, row, DataRowVersion.Current, null);
                    }
                    if (row.tempRecord != -1) {
                        EvaluateDependentExpressions(dependentColumns, row, DataRowVersion.Proposed, null);
                    }
                }
            }
        }

        internal void EvaluateExpressions(DataRow row, DataRowAction action, List<DataRow> cachedRows) {
            // evaluate all expressions for specified row
            if (action == DataRowAction.Add ||
                action == DataRowAction.Change||
                (action == DataRowAction.Rollback && (row.oldRecord!=-1 || row.newRecord!=-1))) {
                 // only evaluate original values if different from current.
                if (row.oldRecord != -1 && row.oldRecord != row.newRecord) {
                    EvaluateDependentExpressions(dependentColumns, row, DataRowVersion.Original, cachedRows);
                }
                if (row.newRecord != -1) {
                    EvaluateDependentExpressions(dependentColumns, row, DataRowVersion.Current, cachedRows);
                }
                if (row.tempRecord != -1) {
                    EvaluateDependentExpressions(dependentColumns, row, DataRowVersion.Proposed, cachedRows);
                }
                return;
            }
            else if ((action == DataRowAction.Delete || (action==DataRowAction.Rollback && row.oldRecord==-1 && row.newRecord==-1)) && dependentColumns != null) {
                foreach(DataColumn col in dependentColumns) {                    
                    if (col.DataExpression != null && col.DataExpression.HasLocalAggregate() && col.Table == this) {
                        for (int j = 0; j < Rows.Count; j++) {
                            DataRow tableRow = Rows[j];

                            if (tableRow.oldRecord != -1 && tableRow.oldRecord != tableRow.newRecord) {
                                EvaluateDependentExpressions(dependentColumns, tableRow, DataRowVersion.Original, null);
                            }                          
                        }
                        for (int j = 0; j < Rows.Count; j++)
                        {
                            DataRow tableRow = Rows[j];
                         
                            if (tableRow.tempRecord != -1)
                            {
                                EvaluateDependentExpressions(dependentColumns, tableRow, DataRowVersion.Proposed, null);
                            }                           
                        }
                        // VSTFDEVDIV911434: Order is important here - we need to update proposed before current
                        // Oherwise rows that are in edit state will get ListChanged/PropertyChanged event before default value is changed
                        // It is also the reason why we are not doping it in the single loop: EvaluateDependentExpression can update the
                        // whole table, if it happens, current for all but first row is updated before proposed value
                        for (int j = 0; j < Rows.Count; j++)
                        {
                            DataRow tableRow = Rows[j];
                                                     
                            if (tableRow.newRecord != -1)
                            {
                                EvaluateDependentExpressions(dependentColumns, tableRow, DataRowVersion.Current, null);
                            }
                        }
                        break;
                    }
                }

                if (cachedRows != null) {
                    foreach (DataRow relatedRow in cachedRows) {
                        if (relatedRow.oldRecord != -1 && relatedRow.oldRecord != relatedRow.newRecord) {
                            relatedRow.Table.EvaluateDependentExpressions(relatedRow.Table.dependentColumns, relatedRow, DataRowVersion.Original, null);
                        }
                        if (relatedRow.newRecord != -1) {
                            relatedRow.Table.EvaluateDependentExpressions(relatedRow.Table.dependentColumns, relatedRow, DataRowVersion.Current, null);
                        }
                        if (relatedRow.tempRecord != -1) {
                            relatedRow.Table.EvaluateDependentExpressions(relatedRow.Table.dependentColumns, relatedRow, DataRowVersion.Proposed, null);
                        }
                    }
                }
            }
        }

        internal void EvaluateExpressions(DataColumn column) {
            // evaluates all rows for expression from specified column
            Debug.Assert(column.Computed, "Only computed columns should be re-evaluated.");
            int count = column.table.Rows.Count;
            if (column.DataExpression.IsTableAggregate() && count > 0) {
                // this value is a constant across the table.
                object aggCurrent = column.DataExpression.Evaluate();
                for (int j = 0; j < count; j++) {
                    DataRow row = column.table.Rows[j];
                    // only evaluate original values if different from current.
                    if (row.oldRecord != -1 && row.oldRecord != row.newRecord) {
                        column[row.oldRecord] = aggCurrent;
                    }
                    if (row.newRecord != -1) {
                        column[row.newRecord] = aggCurrent;
                    }
                    if (row.tempRecord != -1) {
                        column[row.tempRecord] = aggCurrent;
                    }
                }
            }
            else {
                for (int j = 0; j < count; j++) {
                    DataRow row = column.table.Rows[j];

                    if (row.oldRecord != -1 && row.oldRecord != row.newRecord) {
                        column[row.oldRecord] = column.DataExpression.Evaluate(row, DataRowVersion.Original);
                    }
                    if (row.newRecord != -1) {
                        column[row.newRecord] = column.DataExpression.Evaluate(row, DataRowVersion.Current);
                    }
                    if (row.tempRecord != -1) {
                        column[row.tempRecord] = column.DataExpression.Evaluate(row, DataRowVersion.Proposed);
                    }
                }
            }

            // SQLBU 501916 - DataTable internal index is corrupted:'5'
            column.Table.ResetInternalIndexes(column);
            EvaluateDependentExpressions(column);
        }

        internal void EvaluateDependentExpressions(DataColumn column) {
            // DataTable.Clear(), DataRowCollection.Clear() & DataColumn.set_Expression
            if (column.dependentColumns != null) {
                foreach (DataColumn dc in column.dependentColumns) {
                    if ((dc.table != null) && !Object.ReferenceEquals(column, dc)) { // SQLBU 502736
                        EvaluateExpressions(dc);
                    }
                }
            }
        }

        internal void EvaluateDependentExpressions(List<DataColumn> columns, DataRow row, DataRowVersion version, List<DataRow> cachedRows) {
            if (columns == null)
                return;
            //Expression evaluation is done first over same table expressions.
            int count = columns.Count;
            for(int i = 0; i < count; i++) {
                if (columns[i].Table == this) {// if this column is in my table
                    DataColumn dc = columns[i];
                    if (dc.DataExpression != null && dc.DataExpression.HasLocalAggregate()) {
                    // if column expression references a local Table aggregate we need to recalc it for the each row in the local table
                        DataRowVersion expressionVersion  = (version == DataRowVersion.Proposed) ? DataRowVersion.Default : version;
                        bool isConst = dc.DataExpression.IsTableAggregate(); //is expression constant for entire table?
                        object newValue = null;
                        if (isConst) {  //if new value, just compute once
                            newValue = dc.DataExpression.Evaluate(row, expressionVersion);
                        }
                        for (int j = 0; j < Rows.Count; j++) { //evaluate for all rows in the table
                            DataRow dr = Rows[j];
                            if (dr.RowState == DataRowState.Deleted) {
                                continue;
                            }
                            else if (expressionVersion == DataRowVersion.Original && (dr.oldRecord == -1 || dr.oldRecord == dr.newRecord)) {
                                continue;
                            }

                            if (!isConst) {
                                newValue = dc.DataExpression.Evaluate(dr, expressionVersion);
                            }
                            SilentlySetValue(dr, dc, expressionVersion, newValue);
                        }
                    }
                    else {
                        if (row.RowState == DataRowState.Deleted) {
                            continue;
                        }
                        else if (version == DataRowVersion.Original && (row.oldRecord == -1 || row.oldRecord == row.newRecord)) {
                            continue;
                        }
                        SilentlySetValue(row, dc, version, dc.DataExpression == null ? dc.DefaultValue : dc.DataExpression.Evaluate(row, version));
                    }
                }
            }
            // now do expression evaluation for expression columns other tables.
            count = columns.Count;
            for(int i = 0; i < count; i++) {
                DataColumn dc = columns[i];
                // if this column is NOT in my table or it is in the table and is not a local aggregate (self refs)
                if (dc.Table != this || (dc.DataExpression != null && !dc.DataExpression.HasLocalAggregate())) {
                    DataRowVersion foreignVer = (version == DataRowVersion.Proposed) ? DataRowVersion.Default : version;

                    // first - evaluate expressions for cachedRows (deletes & updates)
                    if (cachedRows != null) {
                        foreach (DataRow cachedRow in cachedRows) {
                            if (cachedRow.Table != dc.Table)
                                continue;
                             // don't update original version if child row doesn't have an oldRecord.
                            if (foreignVer == DataRowVersion.Original && cachedRow.newRecord == cachedRow.oldRecord)
                                 continue;
                            if (cachedRow != null && ((cachedRow.RowState != DataRowState.Deleted) && (version != DataRowVersion.Original || cachedRow.oldRecord != -1))) {// if deleted GetRecordFromVersion will throw
                                object newValue = dc.DataExpression.Evaluate(cachedRow, foreignVer);
                                SilentlySetValue(cachedRow, dc, foreignVer, newValue);
                            }
                        }
                    }

                    // next check parent relations
                    for (int j = 0; j < ParentRelations.Count; j++) {
                        DataRelation relation = ParentRelations[j];
                        if (relation.ParentTable != dc.Table)
                            continue;
                        foreach (DataRow parentRow in row.GetParentRows(relation, version)) {
                            if (cachedRows != null && cachedRows.Contains(parentRow))
                                continue;
                             // don't update original version if child row doesn't have an oldRecord.
                            if (foreignVer == DataRowVersion.Original && parentRow.newRecord == parentRow.oldRecord)
                                 continue;
                            if (parentRow != null && ((parentRow.RowState != DataRowState.Deleted) && (version != DataRowVersion.Original || parentRow.oldRecord != -1))) {// if deleted GetRecordFromVersion will throw
                                object newValue = dc.DataExpression.Evaluate(parentRow, foreignVer);
                                SilentlySetValue(parentRow, dc, foreignVer, newValue);
                            }
                        }
                    }
                    // next check child relations
                    for (int j = 0; j < ChildRelations.Count; j++) {
                        DataRelation relation = ChildRelations[j];
                        if (relation.ChildTable != dc.Table)
                            continue;
                        foreach (DataRow childRow in row.GetChildRows(relation, version)) {
                            // don't update original version if child row doesn't have an oldRecord.
                            if (cachedRows != null && cachedRows.Contains(childRow))
                                continue;
                            if (foreignVer == DataRowVersion.Original && childRow.newRecord == childRow.oldRecord)
                                continue;
                            if (childRow != null && ((childRow.RowState != DataRowState.Deleted) && (version != DataRowVersion.Original || childRow.oldRecord != -1))) { // if deleted GetRecordFromVersion will throw
                                object newValue = dc.DataExpression.Evaluate(childRow, foreignVer);
                                SilentlySetValue(childRow, dc, foreignVer, newValue);
                            }
                        }
                    }
                }
            }
        }
    }
}