DataTableTest2.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194
  1. // Authors:
  2. // Rafael Mizrahi <[email protected]>
  3. // Erez Lotan <[email protected]>
  4. // Oren Gurfinkel <[email protected]>
  5. // Ofer Borstein
  6. //
  7. // Copyright (c) 2004 Mainsoft Co.
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using NUnit.Framework;
  29. using System;
  30. using System.Data;
  31. using MonoTests.System.Data.Utils;
  32. namespace MonoTests_System.Data
  33. {
  34. [TestFixture] public class DataTableTest2
  35. {
  36. private bool _EventTriggered = false;
  37. private bool EventRaised = false;
  38. private bool EventValues = false;
  39. class ProtectedTestClass : DataTable
  40. {
  41. public ProtectedTestClass() : base()
  42. {
  43. this.Columns.Add("Id",typeof(int));
  44. this.Columns.Add("Value",typeof(string));
  45. this.Rows.Add(new object[] {1,"one"});
  46. this.Rows.Add(new object[] {2,"two"});
  47. this.AcceptChanges();
  48. }
  49. public void OnColumnChanged_Test()
  50. {
  51. OnColumnChanged(new DataColumnChangeEventArgs(this.Rows[0],this.Columns["Value"],"NewValue"));
  52. }
  53. public void OnColumnChanging_Test()
  54. {
  55. OnColumnChanging(new DataColumnChangeEventArgs(this.Rows[0],this.Columns["Value"],"NewValue"));
  56. }
  57. public void OnRemoveColumn_Test()
  58. {
  59. OnRemoveColumn(this.Columns[0]);
  60. }
  61. public DataTable CreateInstance_Test()
  62. {
  63. return CreateInstance();
  64. }
  65. public void OnRowChanged_Test(DataRowAction drAction )
  66. {
  67. base.OnRowChanged(new DataRowChangeEventArgs(this.Rows[0],drAction ));
  68. }
  69. public void OnRowChanging_Test(DataRowAction drAction )
  70. {
  71. base.OnRowChanging(new DataRowChangeEventArgs(this.Rows[0],drAction ));
  72. }
  73. public void OnRowDeleted_Test(DataRowAction drAction )
  74. {
  75. base.OnRowDeleted(new DataRowChangeEventArgs(this.Rows[0],drAction ));
  76. }
  77. public void OnRowDeleting_Test(DataRowAction drAction )
  78. {
  79. base.OnRowDeleting(new DataRowChangeEventArgs(this.Rows[0],drAction ));
  80. }
  81. }
  82. [Test] public void AcceptChanges()
  83. {
  84. String sNewValue = "NewValue";
  85. DataRow drModified,drDeleted,drAdded;
  86. DataTable dt = DataProvider.CreateParentDataTable();
  87. drModified = dt.Rows[0];
  88. drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
  89. drDeleted = dt.Rows[1];
  90. drDeleted.Delete(); //DataRowState = Deleted
  91. drAdded = dt.NewRow();
  92. dt.Rows.Add(drAdded); //DataRowState = Added
  93. dt.AcceptChanges();
  94. // AcceptChanges - Unchanged1
  95. Assert.AreEqual(DataRowState.Unchanged , drModified.RowState , "DT1");
  96. // AcceptChanges - Current
  97. Assert.AreEqual(sNewValue , drModified[1,DataRowVersion.Current] , "DT2");
  98. // AcceptChanges - Unchanged2
  99. Assert.AreEqual(DataRowState.Unchanged , drAdded.RowState , "DT3");
  100. // AcceptChanges - Detached
  101. Assert.AreEqual(DataRowState.Detached , drDeleted.RowState , "DT4");
  102. }
  103. [Test] public void ChildRelations()
  104. {
  105. DataTable dtChild,dtParent;
  106. DataSet ds = new DataSet();
  107. //Create tables
  108. dtChild = DataProvider.CreateChildDataTable();
  109. dtParent= DataProvider.CreateParentDataTable();
  110. //Add tables to dataset
  111. ds.Tables.Add(dtChild);
  112. ds.Tables.Add(dtParent);
  113. DataRelationCollection drlCollection;
  114. DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  115. // Checking ChildRelations - default value
  116. //Check default
  117. drlCollection = dtParent.ChildRelations;
  118. Assert.AreEqual(0, drlCollection.Count , "DT5");
  119. ds.Relations.Add(drl);
  120. drlCollection = dtParent.ChildRelations;
  121. // Checking ChildRelations Count
  122. Assert.AreEqual(1, drlCollection.Count , "DT6");
  123. // Checking ChildRelations Value
  124. Assert.AreEqual(drl, drlCollection[0] , "DT7");
  125. }
  126. [Test] public void Clear()
  127. {
  128. DataTable dt = DataProvider.CreateParentDataTable();
  129. dt.Clear();
  130. // Clear
  131. Assert.AreEqual(0, dt.Rows.Count , "DT8");
  132. }
  133. [Test] public void Clone()
  134. {
  135. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  136. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  137. dt2.Columns[0].DefaultValue=7;
  138. dt1 = dt2.Clone();
  139. for (int i=0; i<dt2.Constraints.Count; i++)
  140. {
  141. // Clone - Constraints[{0}],i)
  142. Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT9");
  143. }
  144. for (int i=0; i<dt2.Columns.Count; i++)
  145. {
  146. // Clone - Columns[{0}].ColumnName,i)
  147. Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT10");
  148. // Clone - Columns[{0}].DataType,i)
  149. Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT11");
  150. }
  151. }
  152. [Test] public void ColumnChanged()
  153. {
  154. DataTable dt = DataProvider.CreateParentDataTable();
  155. dt.ColumnChanged += new DataColumnChangeEventHandler( Column_Changed );
  156. _EventTriggered=false;
  157. // ColumnChanged - EventTriggered
  158. dt.Rows[0][1] = "NewValue";
  159. Assert.AreEqual(true , _EventTriggered , "DT12");
  160. _EventTriggered=false;
  161. dt.ColumnChanged -= new DataColumnChangeEventHandler( Column_Changed );
  162. // ColumnChanged - NO EventTriggered
  163. dt.Rows[0][1] = "VeryNewValue";
  164. Assert.AreEqual(false , _EventTriggered , "DT13");
  165. }
  166. private void Column_Changed( object sender, DataColumnChangeEventArgs e )
  167. {
  168. _EventTriggered = true;
  169. }
  170. [Test] public void ColumnChanging()
  171. {
  172. DataTable dt = DataProvider.CreateParentDataTable();
  173. dt.ColumnChanging += new DataColumnChangeEventHandler( Column_Changeding );
  174. _EventTriggered=false;
  175. // ColumnChanged - EventTriggered
  176. dt.Rows[0][1] = "NewValue";
  177. Assert.AreEqual(true , _EventTriggered , "DT14");
  178. _EventTriggered=false;
  179. dt.ColumnChanging -= new DataColumnChangeEventHandler( Column_Changeding );
  180. // ColumnChanged - NO EventTriggered
  181. dt.Rows[0][1] = "VeryNewValue";
  182. Assert.AreEqual(false , _EventTriggered , "DT15");
  183. }
  184. private void Column_Changeding( object sender, DataColumnChangeEventArgs e )
  185. {
  186. _EventTriggered = true;
  187. }
  188. [Test] public void Columns()
  189. {
  190. DataTable dtParent;
  191. DataColumnCollection dcl;
  192. dtParent= DataProvider.CreateParentDataTable();
  193. dcl = dtParent.Columns;
  194. // Checking ColumnsCollection != null
  195. Assert.AreEqual(false, dcl == null , "DT16");
  196. // Checking ColumnCollection Count 1
  197. Assert.AreEqual(6, dcl.Count , "DT17");
  198. // Checking ColumnCollection Count 2
  199. dtParent.Columns.Add(new DataColumn("Test"));
  200. Assert.AreEqual(7, dcl.Count , "DT18");
  201. // Checking ColumnCollection - get columnn by different case
  202. DataColumn tmp = dtParent.Columns["TEST"];
  203. Assert.AreEqual(dtParent.Columns["Test"], tmp , "DT19");
  204. // Checking ColumnCollection colummn name case sensetive
  205. dtParent.Columns.Add(new DataColumn("test"));
  206. Assert.AreEqual(8, dcl.Count , "DT20");
  207. // Checking ColumnCollection - get columnn by different case,ArgumentException
  208. try
  209. {
  210. DataColumn tmp1 = dtParent.Columns["TEST"];
  211. Assert.Fail("DT21: indexer Failed to throw ArgumentException");
  212. }
  213. catch (ArgumentException) {}
  214. catch (AssertionException exc) {throw exc;}
  215. catch (Exception exc)
  216. {
  217. Assert.Fail("DT22: Indexer. Wrong exception type. Got:" + exc);
  218. }
  219. }
  220. [Test] public void Compute()
  221. {
  222. DataTable dt = DataProvider.CreateChildDataTable();
  223. //Get expected
  224. DataRow[] drArr = dt.Select("ParentId=1");
  225. Int64 iExSum = 0;
  226. foreach (DataRow dr in drArr)
  227. {
  228. iExSum += (int)dr["ChildId"];
  229. }
  230. object objCompute=null;
  231. // Compute - sum values
  232. objCompute = dt.Compute("Sum(ChildId)","ParentId=1");
  233. Assert.AreEqual(Int64.Parse(objCompute.ToString()) , Int64.Parse(iExSum.ToString()), "DT23");
  234. // Compute - sum type
  235. Assert.AreEqual(typeof(Int64).FullName , objCompute.GetType().FullName, "DT24");
  236. //get expected
  237. double iExAvg = 0;
  238. drArr = dt.Select("ParentId=5");
  239. foreach (DataRow dr in drArr)
  240. {
  241. iExAvg += (double)dr["ChildDouble"];
  242. }
  243. iExAvg = iExAvg / drArr.Length;
  244. // Compute - Avg value
  245. objCompute = dt.Compute("Avg(ChildDouble)","ParentId=5");
  246. Assert.AreEqual(double.Parse(objCompute.ToString()) , double.Parse(iExAvg.ToString()), "DT25");
  247. // Compute - Avg type
  248. Assert.AreEqual(typeof(double).FullName , objCompute.GetType().FullName, "DT26");
  249. }
  250. [Test] public void Constraints()
  251. {
  252. DataTable dtParent;
  253. ConstraintCollection consColl;
  254. dtParent= DataProvider.CreateParentDataTable();
  255. consColl = dtParent.Constraints;
  256. // Checking Constraints != null
  257. Assert.AreEqual(false, consColl == null , "DT27");
  258. // Checking Constraints Count
  259. Assert.AreEqual(0, consColl.Count , "DT28");
  260. // Checking Constraints Count
  261. //Add primary key
  262. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
  263. Assert.AreEqual(1, consColl.Count , "DT29");
  264. }
  265. [Test] public void Copy()
  266. {
  267. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  268. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  269. dt2.Columns[0].DefaultValue=7;
  270. dt1 = dt2.Copy();
  271. for (int i=0; i<dt2.Constraints.Count; i++)
  272. {
  273. // Copy - Constraints[{0}],i)
  274. Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT30");
  275. }
  276. for (int i=0; i<dt2.Columns.Count; i++)
  277. {
  278. // Copy - Columns[{0}].ColumnName,i)
  279. Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT31");
  280. // Copy - Columns[{0}].DataType,i)
  281. Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT32");
  282. }
  283. DataRow[] drArr1,drArr2;
  284. drArr1 = dt1.Select("");
  285. drArr2 = dt2.Select("");
  286. for (int i=0; i<drArr1.Length ; i++)
  287. {
  288. // Copy - Data [ParentId]{0} ,i)
  289. Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT33");
  290. // Copy - Data [String1]{0} ,i)
  291. Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT34");
  292. // Copy - Data [String2]{0} ,i)
  293. Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2"], "DT35");
  294. }
  295. }
  296. [Test] public void CreateInstance()
  297. {
  298. // CreateInstance
  299. ProtectedTestClass C = new ProtectedTestClass();
  300. DataTable dt = C.CreateInstance_Test();
  301. Assert.AreEqual(true , dt != null , "DT36");
  302. }
  303. [Test] public void DataSet()
  304. {
  305. DataTable dtParent;
  306. DataSet ds;
  307. dtParent= DataProvider.CreateParentDataTable();
  308. ds = dtParent.DataSet;
  309. // Checking DataSet == null
  310. Assert.AreEqual(null, ds, "DT37");
  311. // Checking DataSet != null
  312. ds = new DataSet("MyDataSet");
  313. ds.Tables.Add(dtParent);
  314. Assert.AreEqual(true, dtParent.DataSet != null , "DT38");
  315. // Checking DataSet Name
  316. Assert.AreEqual("MyDataSet", dtParent.DataSet.DataSetName , "DT39");
  317. }
  318. [Test] public void DefaultView()
  319. {
  320. DataTable dtParent;
  321. DataView dv;
  322. dtParent= DataProvider.CreateParentDataTable();
  323. dv = dtParent.DefaultView ;
  324. // Checking DataView != null
  325. Assert.AreEqual(true, dv != null, "DT40");
  326. }
  327. [Test] public void EndLoadData()
  328. {
  329. DataTable dt = DataProvider.CreateParentDataTable();
  330. dt.Columns[0].AllowDBNull = false;
  331. // EndLoadData
  332. dt.BeginLoadData();
  333. dt.LoadDataRow(new object[] {null,"A","B"},false);
  334. try
  335. {
  336. //ConstraintException will be throw
  337. dt.EndLoadData();
  338. Assert.Fail("DT41: EndLoadData Failed to throw ConstraintException");
  339. }
  340. catch (ConstraintException) {}
  341. catch (AssertionException exc) {throw exc;}
  342. catch (Exception exc)
  343. {
  344. Assert.Fail("DT42: EndLoadData Wrong exception type. Got:" + exc);
  345. }
  346. }
  347. [Test] public void GetChanges()
  348. {
  349. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  350. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  351. dt2.Columns[0].DefaultValue=7;
  352. //make some changes
  353. dt2.Rows[0].Delete();
  354. dt2.Rows[1].Delete();
  355. dt2.Rows[2].Delete();
  356. dt2.Rows[3].Delete();
  357. dt1 = dt2.GetChanges();
  358. for (int i=0; i<dt2.Constraints.Count; i++)
  359. {
  360. // GetChanges - Constraints[{0}],i)
  361. Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT43");
  362. }
  363. for (int i=0; i<dt2.Columns.Count; i++)
  364. {
  365. // GetChanges - Columns[{0}].ColumnName,i)
  366. Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT44");
  367. // GetChanges - Columns[{0}].DataType,i)
  368. Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT45");
  369. }
  370. DataRow[] drArr1,drArr2;
  371. drArr1 = dt1.Select("","",DataViewRowState.Deleted );
  372. drArr2 = dt2.Select("","",DataViewRowState.Deleted );
  373. for (int i=0; i<drArr1.Length ; i++)
  374. {
  375. // GetChanges - Data [ParentId]{0} ,i)
  376. Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT46");
  377. // GetChanges - Data [String1]{0} ,i)
  378. Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT47");
  379. // GetChanges - Data [String2]{0} ,i)
  380. Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT48");
  381. }
  382. }
  383. [Test] public void GetChanges_ByDataRowState()
  384. {
  385. DataTable dt1,dt2 = DataProvider.CreateParentDataTable();
  386. dt2.Constraints.Add("Unique",dt2.Columns[0],true);
  387. dt2.Columns[0].DefaultValue=7;
  388. //make some changes
  389. dt2.Rows[0].Delete(); //DataRowState.Deleted
  390. dt2.Rows[1].Delete(); //DataRowState.Deleted
  391. dt2.Rows[2].BeginEdit();
  392. dt2.Rows[2]["String1"] = "Changed"; //DataRowState.Modified
  393. dt2.Rows[2].EndEdit();
  394. dt2.Rows.Add(new object[] {"99","Temp1","Temp2"}); //DataRowState.Added
  395. // *********** Checking GetChanges - DataRowState.Deleted ************
  396. dt1=null;
  397. dt1 = dt2.GetChanges(DataRowState.Deleted);
  398. CheckTableSchema (dt1,dt2,DataRowState.Deleted.ToString());
  399. DataRow[] drArr1,drArr2;
  400. drArr1 = dt1.Select("","",DataViewRowState.Deleted );
  401. drArr2 = dt2.Select("","",DataViewRowState.Deleted );
  402. for (int i=0; i<drArr1.Length ; i++)
  403. {
  404. // GetChanges(Deleted) - Data [ParentId]{0} ,i)
  405. Assert.AreEqual(drArr1[i]["ParentId",DataRowVersion.Original ],drArr2[i]["ParentId",DataRowVersion.Original], "DT49");
  406. // GetChanges(Deleted) - Data [String1]{0} ,i)
  407. Assert.AreEqual(drArr1[i]["String1", DataRowVersion.Original],drArr2[i]["String1",DataRowVersion.Original], "DT50");
  408. // GetChanges(Deleted) - Data [String2]{0} ,i)
  409. Assert.AreEqual(drArr1[i]["String2", DataRowVersion.Original],drArr2[i]["String2",DataRowVersion.Original], "DT51");
  410. }
  411. // *********** Checking GetChanges - DataRowState.Modified ************
  412. dt1=null;
  413. dt1 = dt2.GetChanges(DataRowState.Modified);
  414. CheckTableSchema (dt1,dt2,DataRowState.Modified.ToString());
  415. drArr1 = dt1.Select("","");
  416. drArr2 = dt2.Select("","",DataViewRowState.ModifiedCurrent);
  417. for (int i=0; i<drArr1.Length ; i++)
  418. {
  419. // GetChanges(Modified) - Data [ParentId]{0} ,i)
  420. Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT52");
  421. // GetChanges(Modified) - Data [String1]{0} ,i)
  422. Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT53");
  423. // GetChanges(Modified) - Data [String2]{0} ,i)
  424. Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT54");
  425. }
  426. // *********** Checking GetChanges - DataRowState.Added ************
  427. dt1=null;
  428. dt1 = dt2.GetChanges(DataRowState.Added);
  429. CheckTableSchema (dt1,dt2,DataRowState.Added.ToString());
  430. drArr1 = dt1.Select("","");
  431. drArr2 = dt2.Select("","",DataViewRowState.Added );
  432. for (int i=0; i<drArr1.Length ; i++)
  433. {
  434. // GetChanges(Added) - Data [ParentId]{0} ,i)
  435. Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT55");
  436. // GetChanges(Added) - Data [String1]{0} ,i)
  437. Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT56");
  438. // GetChanges(Added) - Data [String2]{0} ,i)
  439. Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT57");
  440. }
  441. // *********** Checking GetChanges - DataRowState.Unchanged ************
  442. dt1=null;
  443. dt1 = dt2.GetChanges(DataRowState.Unchanged);
  444. CheckTableSchema (dt1,dt2,DataRowState.Unchanged .ToString());
  445. drArr1 = dt1.Select("","");
  446. drArr2 = dt2.Select("","",DataViewRowState.Unchanged );
  447. for (int i=0; i<drArr1.Length ; i++)
  448. {
  449. // GetChanges(Unchanged) - Data [ParentId]{0} ,i)
  450. Assert.AreEqual(drArr2[i]["ParentId"], drArr1[i]["ParentId"], "DT58");
  451. // GetChanges(Unchanged) - Data [String1]{0} ,i)
  452. Assert.AreEqual(drArr2[i]["String1"], drArr1[i]["String1"], "DT59");
  453. // GetChanges(Unchanged) - Data [String2]{0} ,i)
  454. Assert.AreEqual(drArr2[i]["String2"], drArr1[i]["String2" ], "DT60");
  455. }
  456. }
  457. private void CheckTableSchema(DataTable dt1, DataTable dt2,string Description)
  458. {
  459. for (int i=0; i<dt2.Constraints.Count; i++)
  460. {
  461. // GetChanges - Constraints[{0}] - {1},i,Description)
  462. Assert.AreEqual(dt2.Constraints[i].ConstraintName , dt1.Constraints[i].ConstraintName , "DT61");
  463. }
  464. for (int i=0; i<dt2.Columns.Count; i++)
  465. {
  466. // GetChanges - Columns[{0}].ColumnName - {1},i,Description)
  467. Assert.AreEqual(dt2.Columns[i].ColumnName , dt1.Columns[i].ColumnName , "DT62");
  468. // GetChanges - Columns[{0}].DataType {1},i,Description)
  469. Assert.AreEqual(dt2.Columns[i].DataType , dt1.Columns[i].DataType , "DT63");
  470. }
  471. }
  472. [Test] public void GetErrors()
  473. {
  474. DataTable dt = DataProvider.CreateParentDataTable();
  475. DataRow[] drArr = new DataRow[3];
  476. drArr[0] = dt.Rows[0];
  477. drArr[1] = dt.Rows[2];
  478. drArr[2] = dt.Rows[5];
  479. drArr[0].RowError = "Error1";
  480. drArr[1].RowError = "Error2";
  481. drArr[2].RowError = "Error3";
  482. // GetErrors
  483. Assert.AreEqual(dt.GetErrors(), drArr , "DT64");
  484. }
  485. [Test] public new void GetHashCode()
  486. {
  487. DataTable dt = DataProvider.CreateParentDataTable();
  488. int iHashCode;
  489. iHashCode = dt.GetHashCode();
  490. for (int i=0; i<10 ;i++)
  491. {
  492. // HashCode - i= + i.ToString()
  493. Assert.AreEqual(dt.GetHashCode() , iHashCode , "DT65");
  494. }
  495. }
  496. [Test] public new void GetType()
  497. {
  498. DataTable dt = DataProvider.CreateParentDataTable();
  499. Type tmpType = typeof(DataTable);
  500. // GetType
  501. Assert.AreEqual(tmpType, dt.GetType() , "DT66");
  502. }
  503. [Test] public void HasErrors()
  504. {
  505. DataTable dtParent;
  506. dtParent= DataProvider.CreateParentDataTable();
  507. // Checking HasErrors default
  508. Assert.AreEqual(false, dtParent.HasErrors , "DT67");
  509. // Checking HasErrors Get
  510. dtParent.Rows[0].RowError = "Error on row 0";
  511. dtParent.Rows[2].RowError = "Error on row 2";
  512. Assert.AreEqual(true, dtParent.HasErrors , "DT68");
  513. }
  514. [Test] public void ImportRow()
  515. {
  516. DataTable dt1,dt2;
  517. dt1 = DataProvider.CreateParentDataTable();
  518. dt2 = DataProvider.CreateParentDataTable();
  519. DataRow dr = dt2.NewRow();
  520. dr.ItemArray = new object[] {99,"",""};
  521. dt2.Rows.Add(dr);
  522. // ImportRow - Values
  523. dt1.ImportRow(dr);
  524. Assert.AreEqual(dr.ItemArray, dt1.Rows[dt1.Rows.Count-1].ItemArray , "DT69");
  525. // ImportRow - DataRowState
  526. Assert.AreEqual(dr.RowState , dt1.Rows[dt1.Rows.Count-1].RowState , "DT70");
  527. }
  528. [Test] public void LoadDataRow()
  529. {
  530. DataTable dt;
  531. DataRow dr;
  532. dt = DataProvider.CreateParentDataTable();
  533. dt.PrimaryKey= new DataColumn[] {dt.Columns[0]}; //add ParentId as Primary Key
  534. dt.Columns["String1"].DefaultValue = "Default";
  535. dr = dt.Select("ParentId=1")[0];
  536. //Update existing row without accept changes
  537. dt.BeginLoadData();
  538. dt.LoadDataRow(new object[] {1,null,"Changed"},false);
  539. dt.EndLoadData();
  540. // LoadDataRow(update1) - check column String1
  541. Assert.AreEqual(dr["String1"], dt.Columns["String1"].DefaultValue , "DT71");
  542. // LoadDataRow(update1) - check column String2
  543. Assert.AreEqual(dr["String2"], "Changed", "DT72");
  544. // LoadDataRow(update1) - check row state
  545. Assert.AreEqual(DataRowState.Modified , dr.RowState , "DT73");
  546. //Update existing row with accept changes
  547. dr = dt.Select("ParentId=2")[0];
  548. dt.BeginLoadData();
  549. dt.LoadDataRow(new object[] {2,null,"Changed"},true);
  550. dt.EndLoadData();
  551. // LoadDataRow(update2) - check row state
  552. Assert.AreEqual(DataRowState.Unchanged , dr.RowState , "DT74");
  553. //Add New row without accept changes
  554. dt.BeginLoadData();
  555. dt.LoadDataRow(new object[] {99,null,"Changed"},false);
  556. dt.EndLoadData();
  557. // LoadDataRow(insert1) - check column String2
  558. dr = dt.Select("ParentId=99")[0];
  559. Assert.AreEqual("Changed" , dr["String2"] , "DT75");
  560. // LoadDataRow(insert1) - check row state
  561. Assert.AreEqual(DataRowState.Added , dr.RowState , "DT76");
  562. //Add New row with accept changes
  563. dt.BeginLoadData();
  564. dt.LoadDataRow(new object[] {100,null,"Changed"},true);
  565. dt.EndLoadData();
  566. // LoadDataRow(insert2) - check row and values
  567. dr = dt.Select("ParentId=100")[0];
  568. Assert.AreEqual("Changed" , dr["String2"] , "DT77");
  569. // LoadDataRow(insert2) - check row state
  570. Assert.AreEqual(DataRowState.Unchanged , dr.RowState , "DT78");
  571. }
  572. [Test] public void Locale()
  573. {
  574. DataTable dtParent;
  575. DataSet ds = new DataSet("MyDataSet");
  576. dtParent= DataProvider.CreateParentDataTable();
  577. ds.Tables.Add(dtParent);
  578. System.Globalization.CultureInfo culInfo = System.Globalization.CultureInfo.CurrentCulture ;
  579. // Checking Locale default from system
  580. Assert.AreEqual(culInfo, dtParent.Locale , "DT79");
  581. // Checking Locale default from dataset
  582. culInfo = new System.Globalization.CultureInfo("fr"); // = French
  583. ds.Locale = culInfo;
  584. Assert.AreEqual(culInfo , dtParent.Locale , "DT80");
  585. // Checking Locale get/set
  586. culInfo = new System.Globalization.CultureInfo("fr"); // = French
  587. dtParent.Locale = culInfo ;
  588. Assert.AreEqual(culInfo , dtParent.Locale , "DT81");
  589. }
  590. [Test] public void MinimumCapacity()
  591. {
  592. // i get default=50, according to MSDN the value should be 25
  593. // // Checking MinimumCapacity default = 25
  594. // Assert.AreEqual(25, dtParent.MinimumCapacity , "DT82");
  595. // EndCase(null);
  596. DataTable dt = new DataTable();
  597. // Checking MinimumCapacity get/set int.MaxValue
  598. dt.MinimumCapacity = int.MaxValue;
  599. Assert.AreEqual(int.MaxValue, dt.MinimumCapacity , "DT83");
  600. // Checking MinimumCapacity get/set 0
  601. dt.MinimumCapacity = 0;
  602. Assert.AreEqual(0, dt.MinimumCapacity , "DT84");
  603. // // Checking MinimumCapacity get/set int.MinValue
  604. // dtParent.MinimumCapacity = int.MinValue ;
  605. // Assert.AreEqual(int.MinValue, dtParent.MinimumCapacity , "DT85");
  606. // EndCase(null);
  607. }
  608. [Test] public void Namespace()
  609. {
  610. DataTable dtParent = new DataTable();
  611. // Checking Namespace default
  612. Assert.AreEqual(String.Empty, dtParent.Namespace, "DT86");
  613. // Checking Namespace set/get
  614. String s = "MyNamespace";
  615. dtParent.Namespace=s;
  616. Assert.AreEqual(s, dtParent.Namespace, "DT87");
  617. }
  618. [Test] public void NewRow()
  619. {
  620. DataTable dt;
  621. DataRow dr;
  622. dt = DataProvider.CreateParentDataTable();
  623. // NewRow
  624. dr = dt.NewRow();
  625. Assert.AreEqual(true , dr != null , "DT88");
  626. }
  627. [Test] public void OnColumnChanged()
  628. {
  629. ProtectedTestClass dt = new ProtectedTestClass();
  630. EventRaised = false;
  631. dt.OnColumnChanged_Test();
  632. // OnColumnChanged Event 1
  633. Assert.AreEqual(false , EventRaised , "DT89");
  634. EventRaised = false;
  635. EventValues = false;
  636. dt.ColumnChanged += new DataColumnChangeEventHandler(OnColumnChanged_Handler);
  637. dt.OnColumnChanged_Test();
  638. // OnColumnChanged Event 2
  639. Assert.AreEqual(true , EventRaised , "DT90");
  640. // OnColumnChanged Values
  641. Assert.AreEqual(true , EventValues , "DT91");
  642. dt.ColumnChanged -= new DataColumnChangeEventHandler(OnColumnChanged_Handler);
  643. }
  644. private void OnColumnChanged_Handler(Object sender,DataColumnChangeEventArgs e)
  645. {
  646. DataTable dt = (DataTable)sender;
  647. if ( (e.Column.Equals(dt.Columns["Value"]) ) &&
  648. (e.Row.Equals(dt.Rows[0]) ) &&
  649. (e.ProposedValue.Equals("NewValue")) )
  650. {
  651. EventValues = true;
  652. }
  653. else
  654. {
  655. EventValues = false;
  656. }
  657. EventRaised = true;
  658. }
  659. [Test] public void OnColumnChanging()
  660. {
  661. ProtectedTestClass dt = new ProtectedTestClass();
  662. EventRaised = false;
  663. dt.OnColumnChanging_Test();
  664. // OnColumnChanging Event 1
  665. Assert.AreEqual(false , EventRaised , "DT92");
  666. EventRaised = false;
  667. EventValues = false;
  668. dt.ColumnChanging += new DataColumnChangeEventHandler(OnColumnChanging_Handler);
  669. dt.OnColumnChanging_Test();
  670. // OnColumnChanging Event 2
  671. Assert.AreEqual(true , EventRaised , "DT93");
  672. // OnColumnChanging Values
  673. Assert.AreEqual(true , EventValues , "DT94");
  674. dt.ColumnChanging -= new DataColumnChangeEventHandler(OnColumnChanging_Handler);
  675. }
  676. private void OnColumnChanging_Handler(Object sender,DataColumnChangeEventArgs e)
  677. {
  678. DataTable dt = (DataTable)sender;
  679. if ( (e.Column.Equals(dt.Columns["Value"]) ) &&
  680. (e.Row.Equals(dt.Rows[0]) ) &&
  681. (e.ProposedValue.Equals("NewValue")) )
  682. {
  683. EventValues = true;
  684. }
  685. else
  686. {
  687. EventValues = false;
  688. }
  689. EventRaised = true;
  690. }
  691. [Test] public void OnRemoveColumn()
  692. {
  693. ProtectedTestClass dt = new ProtectedTestClass();
  694. // OnRemoveColumn
  695. dt.OnRemoveColumn_Test();
  696. }
  697. [Test] public void ParentRelations()
  698. {
  699. DataTable dtChild,dtParent;
  700. DataSet ds = new DataSet();
  701. //Create tables
  702. dtChild = DataProvider.CreateChildDataTable();
  703. dtParent= DataProvider.CreateParentDataTable();
  704. //Add tables to dataset
  705. ds.Tables.Add(dtChild);
  706. ds.Tables.Add(dtParent);
  707. DataRelationCollection drlCollection;
  708. DataRelation drl = new DataRelation("Parent-Child",dtParent.Columns["ParentId"],dtChild.Columns["ParentId"]);
  709. // Checking ParentRelations - default value
  710. //Check default
  711. drlCollection = dtChild.ParentRelations;
  712. Assert.AreEqual(0, drlCollection.Count , "DT96");
  713. ds.Relations.Add(drl);
  714. drlCollection = dtChild.ParentRelations;
  715. // Checking ParentRelations Count
  716. Assert.AreEqual(1, drlCollection.Count , "DT97");
  717. // Checking ParentRelations Value
  718. Assert.AreEqual(drl, drlCollection[0] , "DT98");
  719. }
  720. [Test] public void Prefix()
  721. {
  722. DataTable dtParent = new DataTable();
  723. // Checking Prefix default
  724. Assert.AreEqual(String.Empty, dtParent.Prefix , "DT99");
  725. // Checking Prefix set/get
  726. String s = "MyPrefix";
  727. dtParent.Prefix=s;
  728. Assert.AreEqual(s, dtParent.Prefix, "DT100");
  729. }
  730. [Test] public void RejectChanges()
  731. {
  732. String sNewValue = "NewValue";
  733. DataRow drModified,drDeleted,drAdded;
  734. DataTable dt = DataProvider.CreateParentDataTable();
  735. drModified = dt.Rows[0];
  736. drModified[1] = sNewValue; //DataRowState = Modified ,DataRowVersion = Proposed
  737. drDeleted = dt.Rows[1];
  738. drDeleted.Delete(); //DataRowState = Deleted
  739. drAdded = dt.NewRow();
  740. dt.Rows.Add(drAdded); //DataRowState = Added
  741. dt.RejectChanges();
  742. // RejectChanges - Unchanged1
  743. Assert.AreEqual(DataRowState.Unchanged , drModified.RowState , "DT101");
  744. // RejectChanges - Unchanged2
  745. Assert.AreEqual(DataRowState.Detached , drAdded.RowState , "DT102");
  746. // RejectChanges - Detached
  747. Assert.AreEqual(DataRowState.Unchanged , drDeleted.RowState , "DT103");
  748. }
  749. [Test] public void Reset()
  750. {
  751. DataTable dt1 = DataProvider.CreateParentDataTable();
  752. DataTable dt2 = DataProvider.CreateChildDataTable();
  753. dt1.PrimaryKey = new DataColumn[] {dt1.Columns[0]};
  754. dt2.PrimaryKey = new DataColumn[] {dt2.Columns[0],dt2.Columns[1]};
  755. DataRelation rel = new DataRelation("Rel",dt1.Columns["ParentId"],dt2.Columns["ParentId"]);
  756. DataSet ds = new DataSet();
  757. ds.Tables.AddRange(new DataTable[] {dt1,dt2});
  758. ds.Relations.Add(rel);
  759. dt2.Reset();
  760. // Reset - ParentRelations
  761. Assert.AreEqual(0 , dt2.ParentRelations.Count , "DT104");
  762. // Reset - Constraints
  763. Assert.AreEqual(0 , dt2.Constraints.Count , "DT105");
  764. // Reset - Rows
  765. Assert.AreEqual(0 , dt2.Rows.Count , "DT106");
  766. // Reset - Columns
  767. Assert.AreEqual(0 , dt2.Columns.Count , "DT107");
  768. }
  769. [Test] public void RowChanged()
  770. {
  771. DataTable dt = DataProvider.CreateParentDataTable();
  772. dt.RowChanged += new DataRowChangeEventHandler ( Row_Changed );
  773. _EventTriggered=false;
  774. // RowChanged - 1
  775. dt.Rows[0][1] = "NewValue";
  776. Assert.AreEqual(true , _EventTriggered , "DT108");
  777. _EventTriggered=false;
  778. // RowChanged - 2
  779. dt.Rows[0].BeginEdit();
  780. dt.Rows[0][1] = "NewValue";
  781. Assert.AreEqual(false , _EventTriggered , "DT109");
  782. _EventTriggered=false;
  783. // RowChanged - 3
  784. dt.Rows[0].EndEdit();
  785. Assert.AreEqual(true , _EventTriggered , "DT110");
  786. _EventTriggered=false;
  787. dt.RowChanged -= new DataRowChangeEventHandler ( Row_Changed );
  788. // RowChanged - 4
  789. dt.Rows[0][1] = "NewValue A";
  790. Assert.AreEqual(false , _EventTriggered , "DT111");
  791. }
  792. private void Row_Changed( object sender, DataRowChangeEventArgs e )
  793. {
  794. _EventTriggered = true;
  795. }
  796. [Test] public void RowChanging()
  797. {
  798. DataTable dt = DataProvider.CreateParentDataTable();
  799. dt.RowChanging += new DataRowChangeEventHandler ( Row_Changing );
  800. _EventTriggered=false;
  801. // RowChanging - 1
  802. dt.Rows[0][1] = "NewValue";
  803. Assert.AreEqual(true , _EventTriggered , "DT112");
  804. _EventTriggered=false;
  805. // RowChanging - 2
  806. dt.Rows[0].BeginEdit();
  807. dt.Rows[0][1] = "NewValue";
  808. Assert.AreEqual(false , _EventTriggered , "DT113");
  809. _EventTriggered=false;
  810. // RowChanging - 3
  811. dt.Rows[0].EndEdit();
  812. Assert.AreEqual(true , _EventTriggered , "DT114");
  813. _EventTriggered=false;
  814. dt.RowChanging -= new DataRowChangeEventHandler ( Row_Changing );
  815. // RowChanging - 4
  816. dt.Rows[0][1] = "NewValue A";
  817. Assert.AreEqual(false , _EventTriggered , "DT115");
  818. }
  819. private void Row_Changing( object sender, DataRowChangeEventArgs e )
  820. {
  821. _EventTriggered = true;
  822. }
  823. [Test] public void RowDeleted()
  824. {
  825. DataTable dt = DataProvider.CreateParentDataTable();
  826. dt.RowDeleted += new DataRowChangeEventHandler ( Row_Deleted );
  827. _EventTriggered=false;
  828. // RowDeleted - 1
  829. dt.Rows[0].Delete();
  830. Assert.AreEqual(true , _EventTriggered , "DT116");
  831. _EventTriggered=false;
  832. dt.RowDeleted -= new DataRowChangeEventHandler ( Row_Deleted );
  833. // RowDeleted - 2
  834. dt.Rows[1].Delete();
  835. Assert.AreEqual(false , _EventTriggered , "DT117");
  836. }
  837. private void Row_Deleted( object sender, DataRowChangeEventArgs e )
  838. {
  839. _EventTriggered = true;
  840. }
  841. [Test] public void RowDeleting()
  842. {
  843. DataTable dt = DataProvider.CreateParentDataTable();
  844. dt.RowDeleting += new DataRowChangeEventHandler ( Row_Deleting );
  845. _EventTriggered=false;
  846. // RowDeleting - 1
  847. dt.Rows[0].Delete();
  848. Assert.AreEqual(true , _EventTriggered , "DT118");
  849. _EventTriggered=false;
  850. dt.RowDeleting -= new DataRowChangeEventHandler ( Row_Deleting );
  851. // RowDeleting - 2
  852. dt.Rows[1].Delete();
  853. Assert.AreEqual(false , _EventTriggered , "DT119");
  854. }
  855. private void Row_Deleting( object sender, DataRowChangeEventArgs e )
  856. {
  857. _EventTriggered = true;
  858. }
  859. [Test] public void Rows()
  860. {
  861. DataTable dtParent;
  862. dtParent = DataProvider.CreateParentDataTable();
  863. // Checking Rows
  864. Assert.AreEqual(true, dtParent.Rows != null, "DT120");
  865. // Checking rows count
  866. Assert.AreEqual(true, dtParent.Rows.Count > 0 , "DT121");
  867. }
  868. [Test] public void Select()
  869. {
  870. DataTable dt = DataProvider.CreateParentDataTable();
  871. DataRow[] drSelect = dt.Select();
  872. DataRow[] drResult = new DataRow[dt.Rows.Count] ;
  873. dt.Rows.CopyTo(drResult,0);
  874. // Select
  875. Assert.AreEqual(drResult, drSelect , "DT122");
  876. }
  877. [Test] public void Select_ByFilter()
  878. {
  879. DataSet ds = new DataSet();
  880. ds.Tables.Add(DataProvider.CreateParentDataTable());
  881. DataTable dt = DataProvider.CreateChildDataTable();
  882. ds.Tables.Add(dt);
  883. DataRow[] drSelect = null;
  884. System.Collections.ArrayList al = new System.Collections.ArrayList();
  885. //add column with special name
  886. DataColumn dc = new DataColumn("Column#",typeof(int));
  887. dc.DefaultValue=-1;
  888. dt.Columns.Add(dc);
  889. //put some values
  890. dt.Rows[0][dc] = 100;
  891. dt.Rows[1][dc] = 200;
  892. dt.Rows[2][dc] = 300;
  893. dt.Rows[4][dc] = -400;
  894. //for trim function
  895. dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " \t\n ";
  896. dt.Rows[0]["String1"] = " \t\n " + dt.Rows[0]["String1"] ;
  897. dt.Rows[0]["String1"] = dt.Rows[0]["String1"] + " ";
  898. ds.Tables[0].Rows[0]["ParentBool"] = DBNull.Value;
  899. ds.Tables[0].Rows[2]["ParentBool"] = DBNull.Value;
  900. ds.Tables[0].Rows[3]["ParentBool"] = DBNull.Value;
  901. //-------------------------------------------------------------
  902. al.Clear();
  903. foreach (DataRow dr in dt.Rows )
  904. {
  905. if ((int)dr["ChildId"] == 1)
  906. {
  907. al.Add(dr);
  908. }
  909. }
  910. // Select_S - ChildId=1
  911. drSelect = dt.Select("ChildId=1");
  912. Assert.AreEqual(al.ToArray(), drSelect , "DT123");
  913. //-------------------------------------------------------------
  914. al.Clear();
  915. foreach (DataRow dr in dt.Rows )
  916. {
  917. if ((int)dr["ChildId"] == 1)
  918. {
  919. al.Add(dr);
  920. }
  921. }
  922. // Select_S - ChildId='1'
  923. drSelect = dt.Select("ChildId='1'");
  924. Assert.AreEqual(al.ToArray(), drSelect , "DT124");
  925. //-------------------------------------------------------------
  926. // Select_S - ChildId= '1' (whitespace in filter string.
  927. drSelect = dt.Select("ChildId= '1'");
  928. Assert.AreEqual(al.ToArray(), drSelect , "DT125");
  929. //-------------------------------------------------------------
  930. al.Clear();
  931. foreach (DataRow dr in dt.Rows ) if (dr["String1"].ToString() == "1-String1") al.Add(dr);
  932. // Select_S - String1='1-String1'
  933. drSelect = dt.Select("String1='1-String1'");
  934. Assert.AreEqual(al.ToArray(), drSelect , "DT126");
  935. //-------------------------------------------------------------
  936. al.Clear();
  937. foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1" ) al.Add(dr);
  938. // Select_S - ChildId=1 and String1='1-String1'
  939. drSelect = dt.Select("ChildId=1 and String1='1-String1'");
  940. Assert.AreEqual(al.ToArray(), drSelect , "DT127");
  941. //-------------------------------------------------------------
  942. al.Clear();
  943. foreach (DataRow dr in dt.Rows ) if ((int)dr["ChildId"] + (int)dr["ParentId"] >= 4 ) al.Add(dr);
  944. // Select_S - ChildId+ParentId >= 4
  945. drSelect = dt.Select("ChildId+ParentId >= 4");
  946. CompareUnSorted(drSelect ,al.ToArray());
  947. //-------------------------------------------------------------
  948. al.Clear();
  949. foreach (DataRow dr in dt.Rows )
  950. {
  951. if ((((int)dr["ChildId"] - (int)dr["ParentId"]) * -1) != 0 )
  952. {
  953. al.Add(dr);
  954. }
  955. }
  956. // Select_S - ChildId-ParentId) * -1 <> 0
  957. drSelect = dt.Select("(ChildId-ParentId) * -1 <> 0");
  958. CompareUnSorted(drSelect ,al.ToArray());
  959. //-------------------------------------------------------------
  960. al.Clear();
  961. foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] < ((int)dr["ParentId"]) % 4 ) al.Add(dr);
  962. // Select_S - ChildDouble < ParentId % 4
  963. drSelect = dt.Select("ChildDouble < ParentId % 4");
  964. CompareUnSorted(drSelect ,al.ToArray());
  965. //-------------------------------------------------------------
  966. al.Clear();
  967. foreach (DataRow dr in dt.Rows ) if ( (double)dr["ChildDouble"] == 10 || (double)dr["ChildDouble"] == 20 || (double)dr["ChildDouble"] == 25 ) al.Add(dr);
  968. // Select_S - ChildDouble in (10,20,25)
  969. drSelect = dt.Select("ChildDouble in (10,20,25)");
  970. CompareUnSorted(drSelect ,al.ToArray());
  971. //-------------------------------------------------------------
  972. al.Clear();
  973. foreach (DataRow dr in dt.Rows ) if ( dr["String2"].ToString().IndexOf("1-S") >= 0 ) al.Add(dr);
  974. // Select_S - String2 like '%1-S%'
  975. drSelect = dt.Select("String2 like '%1-S%'");
  976. Assert.AreEqual(al.ToArray(), drSelect , "DT128");
  977. //-------------------------------------------------------------
  978. //If a column name contains one of the above characters,(ex. #\/=><+-*%&|^'" and so on) the name must be wrapped in brackets. For example to use a column named "Column#" in an expression, you would write "[Column#]":
  979. al.Clear();
  980. foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
  981. // Select_S - [Column#] <= 0
  982. drSelect = dt.Select("[Column#] <= 0 ");
  983. CompareUnSorted(drSelect ,al.ToArray());
  984. //-------------------------------------------------------------
  985. al.Clear();
  986. foreach (DataRow dr in dt.Rows ) if ( (int)dr["Column#"] <= 0) al.Add(dr);
  987. // Select_S - [Column#] <= 0
  988. drSelect = dt.Select("[Column#] <= 0");
  989. CompareUnSorted(drSelect ,al.ToArray());
  990. //-------------------------------------------------------------
  991. al.Clear();
  992. foreach (DataRow dr in dt.Rows ) if (((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2000,12,12)) > 0 ) al.Add(dr);
  993. // Select_S - ChildDateTime > #12/12/2000#
  994. drSelect = dt.Select("ChildDateTime > #12/12/2000# ");
  995. CompareUnSorted(drSelect ,al.ToArray());
  996. //-------------------------------------------------------------
  997. al.Clear();
  998. foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1999,1,12,12,06,30)) > 0 ) al.Add(dr);
  999. // Select_S - ChildDateTime > #1/12/1999 12:06:30 PM#
  1000. drSelect = dt.Select("ChildDateTime > #1/12/1999 12:06:30 PM# ");
  1001. CompareUnSorted(drSelect ,al.ToArray());
  1002. //-------------------------------------------------------------
  1003. al.Clear();
  1004. foreach (DataRow dr in dt.Rows ) if ( ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(2005,12,03,17,06,30)) >= 0 || ((DateTime)dr["ChildDateTime"]).CompareTo(new DateTime(1980,11,03)) <= 0 ) al.Add(dr);
  1005. // Select_S - ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980#
  1006. drSelect = dt.Select("ChildDateTime >= #12/3/2005 5:06:30 PM# or ChildDateTime <= #11/3/1980# ");
  1007. CompareUnSorted(drSelect ,al.ToArray());
  1008. #if LATER
  1009. //-------------------------------------------------------------
  1010. al.Clear();
  1011. foreach (DataRow dr in dt.Rows ) if ( dr["ChildDouble"].ToString().Length > 10) al.Add(dr);
  1012. // Select_S - Len(Convert(ChildDouble,'System.String')) > 10
  1013. drSelect = dt.Select("Len(Convert(ChildDouble,'System.String')) > 10");
  1014. Assert.AreEqual(al.ToArray(), drSelect , "DT129");
  1015. #endif
  1016. //-------------------------------------------------------------
  1017. al.Clear();
  1018. foreach (DataRow dr in dt.Rows ) if ( dr["String1"].ToString().Trim().Substring(0,2) == "1-") al.Add(dr);
  1019. // Select_S - SubString(Trim(String1),1,2) = '1-'
  1020. drSelect = dt.Select("SubString(Trim(String1),1,2) = '1-'");
  1021. Assert.AreEqual(al.ToArray(), drSelect , "DT130");
  1022. //-------------------------------------------------------------
  1023. /*
  1024. al.Clear();
  1025. foreach (DataRow dr in ds.Tables[0].Rows ) if ( dr.IsNull("ParentBool") || (bool)dr["ParentBool"]) al.Add(dr);
  1026. // Select_S - IsNull(ParentBool,true)
  1027. drSelect = ds.Tables[0].Select("IsNull(ParentBool,true) ");
  1028. Assert.AreEqual(al.ToArray(), drSelect , "DT131");
  1029. */
  1030. //-------------------------------------------------------------
  1031. al.Clear();
  1032. // Select_S - Relation not exists, Exception
  1033. try
  1034. {
  1035. drSelect = dt.Select("Parent.ParentId = ChildId");
  1036. Assert.Fail("DT132: Select Failed to throw IndexOutOfRangeException");
  1037. }
  1038. catch (IndexOutOfRangeException) {}
  1039. catch (AssertionException exc) {throw exc;}
  1040. catch (Exception exc)
  1041. {
  1042. Assert.Fail("DT133: Select. Wrong exception type. Got:" + exc);
  1043. }
  1044. //-------------------------------------------------------------
  1045. al.Clear();
  1046. ds.Relations.Add(new DataRelation("ParentChild",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
  1047. foreach (DataRow dr in dt.Rows ) if ( (int)dr["ChildId"] == (int)dr.GetParentRow("ParentChild")["ParentId"]) al.Add(dr);
  1048. // Select_S - Parent.ParentId = ChildId
  1049. drSelect = dt.Select("Parent.ParentId = ChildId");
  1050. Assert.AreEqual(al.ToArray(), drSelect , "DT134");
  1051. }
  1052. private void CompareUnSorted(Array a, Array b)
  1053. {
  1054. string msg = string.Format("Failed while comparing(Array a ={0} ({1}), Array b = {2} ({3}))]", a.ToString(), a.GetType().FullName, b.ToString(), b.GetType().FullName);
  1055. foreach (object item in a)
  1056. {
  1057. if (Array.IndexOf(b, item) < 0) //b does not contain the current item.
  1058. {
  1059. Assert.Fail(msg);
  1060. }
  1061. }
  1062. foreach (object item in b)
  1063. {
  1064. if (Array.IndexOf(a, item) < 0) //a does not contain the current item.
  1065. {
  1066. Assert.Fail(msg);
  1067. return;
  1068. }
  1069. }
  1070. }
  1071. [Test] public void Select_ByFilterDataViewRowState()
  1072. {
  1073. DataTable dt = DataProvider.CreateParentDataTable();
  1074. DataRow[] drSelect, drResult;
  1075. dt.Rows[0].Delete();
  1076. dt.Rows[1]["ParentId"] = 1;
  1077. dt.Rows[2]["ParentId"] = 1;
  1078. dt.Rows[3].Delete();
  1079. dt.Rows.Add(new object[] {1,"A","B"});
  1080. dt.Rows.Add(new object[] {1,"C","D"});
  1081. dt.Rows.Add(new object[] {1,"E","F"});
  1082. drSelect = dt.Select("ParentId=1","",DataViewRowState.Added );
  1083. drResult = GetResultRows(dt,DataRowState.Added);
  1084. // Select_SSD DataViewRowState.Added
  1085. Assert.AreEqual(drResult, drSelect , "DT135");
  1086. drSelect = dt.Select("ParentId=1","",DataViewRowState.CurrentRows );
  1087. drResult = GetResultRows(dt,DataRowState.Unchanged | DataRowState.Added | DataRowState.Modified );
  1088. // Select_SSD DataViewRowState.CurrentRows
  1089. Assert.AreEqual(drResult, drSelect , "DT136");
  1090. drSelect = dt.Select("ParentId=1","",DataViewRowState.Deleted );
  1091. drResult = GetResultRows(dt,DataRowState.Deleted );
  1092. // Select_SSD DataViewRowState.Deleted
  1093. Assert.AreEqual(drResult, drSelect , "DT137");
  1094. drSelect = dt.Select("ParentId=1","",DataViewRowState.ModifiedCurrent | DataViewRowState.ModifiedOriginal );
  1095. drResult = GetResultRows(dt,DataRowState.Modified );
  1096. // Select_SSD ModifiedCurrent or ModifiedOriginal
  1097. Assert.AreEqual(drResult, drSelect , "DT138");
  1098. }
  1099. private DataRow[] GetResultRows(DataTable dt,DataRowState State)
  1100. {
  1101. System.Collections.ArrayList al = new System.Collections.ArrayList();
  1102. DataRowVersion drVer = DataRowVersion.Current;
  1103. //From MSDN - The row the default version for the current DataRowState.
  1104. // For a DataRowState value of Added, Modified or Current,
  1105. // the default version is Current.
  1106. // For a DataRowState of Deleted, the version is Original.
  1107. // For a DataRowState value of Detached, the version is Proposed.
  1108. if ( ((State & DataRowState.Added) > 0)
  1109. | ((State & DataRowState.Modified) > 0)
  1110. | ((State & DataRowState.Unchanged) > 0) )
  1111. drVer = DataRowVersion.Current;
  1112. if ( (State & DataRowState.Deleted) > 0
  1113. | (State & DataRowState.Detached) > 0 )
  1114. drVer = DataRowVersion.Original;
  1115. foreach (DataRow dr in dt.Rows )
  1116. {
  1117. if ( dr.HasVersion(drVer)
  1118. && ((int)dr["ParentId", drVer] == 1)
  1119. && ((dr.RowState & State) > 0 )
  1120. )
  1121. al.Add(dr);
  1122. }
  1123. DataRow[] result = (DataRow[])al.ToArray((typeof(DataRow)));
  1124. return result;
  1125. }
  1126. [Test] public void TableName()
  1127. {
  1128. DataTable dtParent = new DataTable();
  1129. // Checking TableName default
  1130. Assert.AreEqual(String.Empty, dtParent.TableName, "DT139");
  1131. // Checking TableName set/get
  1132. String s = "MyTable";
  1133. dtParent.TableName=s;
  1134. Assert.AreEqual(s, dtParent.TableName, "DT140");
  1135. }
  1136. [Test] public new void ToString()
  1137. {
  1138. DataTable dt = DataProvider.CreateParentDataTable();
  1139. dt.DisplayExpression = dt.Columns[0].ColumnName ;
  1140. string sToString = dt.TableName + " + " + dt.DisplayExpression;
  1141. // ToString
  1142. Assert.AreEqual(sToString , dt.ToString() , "DT141");
  1143. }
  1144. [Test] public void caseSensitive()
  1145. {
  1146. DataTable dtParent = new DataTable();
  1147. // Checking default
  1148. Assert.AreEqual(false, dtParent.CaseSensitive , "DT142");
  1149. // Checking set/get
  1150. dtParent.CaseSensitive = true;
  1151. Assert.AreEqual(true, dtParent.CaseSensitive , "DT143");
  1152. }
  1153. [Test] public void ctor()
  1154. {
  1155. DataTable dt;
  1156. dt = new DataTable();
  1157. // ctor
  1158. Assert.AreEqual(false, dt == null, "DT144");
  1159. }
  1160. [Test] public void ctor_ByName()
  1161. {
  1162. DataTable dt;
  1163. string sName = "MyName";
  1164. dt = new DataTable(sName);
  1165. // Ctor
  1166. Assert.AreEqual(false , dt == null , "DT145");
  1167. // Ctor TableName
  1168. Assert.AreEqual(sName , dt.TableName , "DT146");
  1169. }
  1170. [Test] public void displayExpression()
  1171. {
  1172. DataTable dtParent;
  1173. dtParent= DataProvider.CreateParentDataTable();
  1174. // Checking DisplayExpression default
  1175. Assert.AreEqual(String.Empty , dtParent.DisplayExpression , "DT147");
  1176. // Checking DisplayExpression Set/Get
  1177. dtParent.DisplayExpression = dtParent.Columns[0].ColumnName;
  1178. Assert.AreEqual(dtParent.Columns[0].ColumnName, dtParent.DisplayExpression , "DT148");
  1179. }
  1180. [Test] public void extendedProperties()
  1181. {
  1182. DataTable dtParent;
  1183. PropertyCollection pc;
  1184. dtParent= DataProvider.CreateParentDataTable();
  1185. pc = dtParent.ExtendedProperties ;
  1186. // Checking ExtendedProperties default
  1187. Assert.AreEqual(true, pc != null, "DT149");
  1188. // Checking ExtendedProperties count
  1189. Assert.AreEqual(0, pc.Count , "DT150");
  1190. }
  1191. [Test] public void primaryKey()
  1192. {
  1193. DataTable dtParent;
  1194. dtParent = DataProvider.CreateParentDataTable();
  1195. // Checking PrimaryKey default
  1196. Assert.AreEqual(0, dtParent.PrimaryKey.Length , "DT151");
  1197. // Checking PrimaryKey set/get
  1198. DataColumn[] dcArr = new DataColumn[] {dtParent.Columns[0]};
  1199. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns[0]};
  1200. Assert.AreEqual(dcArr, dtParent.PrimaryKey , "DT152");
  1201. dtParent.PrimaryKey=null;
  1202. DataSet ds = new DataSet();
  1203. DataRow dr = null;
  1204. ds.Tables.Add(dtParent);
  1205. //check primary key - ColumnType String, ds.CaseSensitive = false;
  1206. ds.CaseSensitive = false;
  1207. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["String1"]};
  1208. // check primary key - ColumnType String, ds.CaseSensitive = false;
  1209. dr = dtParent.NewRow();
  1210. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1211. dr["String1"] = dr["String1"].ToString().ToUpper();
  1212. try
  1213. {
  1214. dtParent.Rows.Add(dr);
  1215. Assert.Fail("DT153: Rows.Add Failed to throw ConstraintException");
  1216. }
  1217. catch (ConstraintException) {}
  1218. catch (AssertionException exc) {throw exc;}
  1219. catch (Exception exc)
  1220. {
  1221. Assert.Fail("DT154: Rows.Add. Wrong exception type. Got:" + exc);
  1222. }
  1223. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1224. //check primary key - ColumnType String, ds.CaseSensitive = true;
  1225. ds.CaseSensitive = true;
  1226. // check primary key ConstraintException - ColumnType String, ds.CaseSensitive = true;
  1227. dr = dtParent.NewRow();
  1228. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1229. dr["String1"] = dr["String1"].ToString();
  1230. try
  1231. {
  1232. dtParent.Rows.Add(dr);
  1233. Assert.Fail("DT155: Rows.Add Failed to throw ConstraintException");
  1234. }
  1235. catch (ConstraintException) {}
  1236. catch (AssertionException exc) {throw exc;}
  1237. catch (Exception exc)
  1238. {
  1239. Assert.Fail("DT156: Rows.Add. Wrong exception type. Got:" + exc);
  1240. }
  1241. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1242. //check primary key - ColumnType String, ds.CaseSensitive = true;
  1243. ds.CaseSensitive = true;
  1244. // check primary key - ColumnType String, ds.CaseSensitive = true;
  1245. dr = dtParent.NewRow();
  1246. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1247. dr["String1"] = dr["String1"].ToString().ToUpper() ;
  1248. dtParent.Rows.Add(dr);
  1249. Assert.AreEqual(true, dtParent.Rows.Contains(dr["String1"]), "DT157");
  1250. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1251. dtParent.PrimaryKey=null;
  1252. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDateTime"]};
  1253. // check primary key - ColumnType DateTime
  1254. dr = dtParent.NewRow();
  1255. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1256. dr["ParentDateTime"] = DateTime.Now;
  1257. dtParent.Rows.Add(dr);
  1258. Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDateTime"]), "DT158");
  1259. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1260. // check primary key ConstraintException- ColumnType DateTime
  1261. dr = dtParent.NewRow();
  1262. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1263. dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
  1264. try
  1265. {
  1266. dtParent.Rows.Add(dr);
  1267. Assert.Fail("DT159: Rows.Add Failed to throw ConstraintException");
  1268. }
  1269. catch (ConstraintException) {}
  1270. catch (AssertionException exc) {throw exc;}
  1271. catch (Exception exc)
  1272. {
  1273. Assert.Fail("DT160: Rows.Add. Wrong exception type. Got:" + exc);
  1274. }
  1275. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1276. dtParent.PrimaryKey=null;
  1277. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"]};
  1278. // check primary key - ColumnType ParentDouble, value=Epsilon
  1279. dr = dtParent.NewRow();
  1280. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1281. dr["ParentDouble"] = Double.Epsilon ;
  1282. dtParent.Rows.Add(dr);
  1283. Assert.AreEqual(true, dtParent.Rows.Contains(dr["ParentDouble"]), "DT161");
  1284. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1285. // check primary key ConstraintException - ColumnType ParentDouble
  1286. dr = dtParent.NewRow();
  1287. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1288. dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
  1289. try
  1290. {
  1291. dtParent.Rows.Add(dr);
  1292. Assert.Fail("DT162: Rows.Add Failed to throw ConstraintException");
  1293. }
  1294. catch (ConstraintException) {}
  1295. catch (AssertionException exc) {throw exc;}
  1296. catch (Exception exc)
  1297. {
  1298. Assert.Fail("DT163: Rows.Add. Wrong exception type. Got:" + exc);
  1299. }
  1300. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1301. //
  1302. // SubTest
  1303. //
  1304. dtParent.PrimaryKey=null;
  1305. // check primary key ConstraintException - ColumnType ParentBool
  1306. try
  1307. {
  1308. //ParentBool is not unique, will raise exception
  1309. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentBool"]};
  1310. Assert.Fail("DT164: PrimaryKey Failed to throw ArgumentException");
  1311. }
  1312. catch (ArgumentException) {}
  1313. catch (AssertionException exc) {throw exc;}
  1314. catch (Exception exc)
  1315. {
  1316. Assert.Fail("DT165: PrimaryKey. Wrong exception type. Got:" + exc);
  1317. }
  1318. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1319. //
  1320. // SubTest
  1321. //
  1322. dtParent.PrimaryKey=null;
  1323. dtParent.PrimaryKey = new DataColumn[] {dtParent.Columns["ParentDouble"],dtParent.Columns["ParentDateTime"]};
  1324. // check primary key - ColumnType Double,DateTime test1
  1325. dr = dtParent.NewRow();
  1326. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1327. dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
  1328. dr["ParentDateTime"] = DateTime.Now;
  1329. dtParent.Rows.Add(dr);
  1330. Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT166");
  1331. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1332. // check primary key - ColumnType Double,DateTime test2
  1333. dr = dtParent.NewRow();
  1334. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1335. dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
  1336. dr["ParentDouble"] = 99.399;
  1337. dtParent.Rows.Add(dr);
  1338. Assert.AreEqual(true, dtParent.Rows.Contains(new object[] {dr["ParentDouble"],dr["ParentDateTime"]}), "DT167");
  1339. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1340. // check primary key ConstraintException - ColumnType Double,DateTime
  1341. dr = dtParent.NewRow();
  1342. dr.ItemArray = dtParent.Rows[0].ItemArray;
  1343. dr["ParentDouble"] = dtParent.Rows[0]["ParentDouble"];
  1344. dr["ParentDateTime"] = dtParent.Rows[0]["ParentDateTime"];
  1345. try
  1346. {
  1347. dtParent.Rows.Add(dr);
  1348. Assert.Fail("DT168: Rows.Add Failed to throw ConstraintException");
  1349. }
  1350. catch (ConstraintException) {}
  1351. catch (AssertionException exc) {throw exc;}
  1352. catch (Exception exc)
  1353. {
  1354. Assert.Fail("DT169: Rows.Add. Wrong exception type. Got:" + exc);
  1355. }
  1356. if (dr.RowState != DataRowState.Detached) dtParent.Rows.Remove(dr);
  1357. DataTable dtChild = DataProvider.CreateChildDataTable();
  1358. ds.Tables.Add(dtChild);
  1359. dtParent.PrimaryKey = null;
  1360. //this test was addedd to check java exception:
  1361. //System.ArgumentException: Cannot remove UniqueConstraint because the ForeignKeyConstraint myRelation exists.
  1362. // check add primary key with relation
  1363. ds.Relations.Add(new DataRelation("myRelation",ds.Tables[0].Columns[0],ds.Tables[1].Columns[0]));
  1364. //the following line will cause java to fail
  1365. ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns[0],ds.Tables[0].Columns[1]};
  1366. Assert.AreEqual(2, ds.Tables[0].PrimaryKey.Length , "DT170");
  1367. }
  1368. // Test for bug #76213
  1369. [Test]
  1370. public void Compute_WithoutSchemaData_Test()
  1371. {
  1372. DataSet ds = new DataSet ("TestData");
  1373. DataTable table = ds.Tables.Add ("TestTable");
  1374. table.Columns.Add ("Id");
  1375. table.Columns.Add ("Value");
  1376. table.Rows.Add (new object[] {"1","4.5"});
  1377. table.Rows.Add (new object[] {"2","7.5"});
  1378. table.Rows.Add (new object[] {"3","2.5"});
  1379. table.Rows.Add (new object[] {"4","3.5"});
  1380. Assert.AreEqual ("1",
  1381. table.Compute ("Min(Id)",String.Empty),"#1");
  1382. Assert.AreEqual ("4",
  1383. table.Compute ("Max(Id)",String.Empty),"#2");
  1384. Assert.AreEqual ("2.5",
  1385. table.Compute ("Min(Value)",String.Empty),"#3");
  1386. Assert.AreEqual ("7.5",
  1387. table.Compute ("Max(Value)",String.Empty),"#4");
  1388. }
  1389. [Test]
  1390. public void BeginLoadData()
  1391. {
  1392. DataTable dt = DataProvider.CreateParentDataTable();
  1393. dt.Columns[0].AllowDBNull = false;
  1394. //dt.BeginLoadData();
  1395. try
  1396. {
  1397. //if BeginLoadData has not been called, an exception will be throw
  1398. dt.LoadDataRow(new object[] {null,"A","B"},false);
  1399. Assert.Fail("DT170: Failed to throw NoNullAllowedException");
  1400. }
  1401. catch (NoNullAllowedException) {}
  1402. catch (AssertionException exc) {throw exc;}
  1403. catch (Exception exc)
  1404. {
  1405. Assert.Fail("DT171: Wrong exception type. Got:" + exc);
  1406. }
  1407. DataTable dt1 = DataProvider.CreateUniqueConstraint();
  1408. bool excptionOccurd = false;
  1409. try
  1410. {
  1411. dt1.BeginLoadData();
  1412. DataRow dr = dt1.NewRow();
  1413. dr[0] = 3;
  1414. dt1.Rows.Add(dr);
  1415. dt1.EndLoadData(); // constraint violation
  1416. Assert.Fail("DT172: Failed to throw ConstraintException");
  1417. }
  1418. catch (ConstraintException)
  1419. {
  1420. Assert.AreEqual(2,dt1.GetErrors().Length,"DT173");
  1421. Assert.AreEqual(true,dt1.GetErrors()[0].RowError.Length > 10,"DT174");
  1422. Assert.AreEqual(true,dt1.GetErrors()[1].RowError.Length > 10,"DT175");
  1423. }
  1424. catch (AssertionException exc) {throw exc;}
  1425. catch (Exception exc)
  1426. {
  1427. Assert.Fail("DT176: Wrong exception type. Got:" + exc);
  1428. }
  1429. DataSet ds=null;
  1430. excptionOccurd = false;
  1431. try
  1432. {
  1433. ds= DataProvider.CreateForigenConstraint();
  1434. ds.Tables[0].BeginLoadData();
  1435. ds.Tables[0].Rows[0][0] = 10; //Forigen constraint violation
  1436. //ds.Tables[0].AcceptChanges();
  1437. ds.Tables[0].EndLoadData();
  1438. Assert.Fail("DT177: Failed to throw ConstraintException");
  1439. }
  1440. catch (ConstraintException)
  1441. {
  1442. Assert.AreEqual(3,ds.Tables[1].GetErrors().Length,"DT178");
  1443. for(int index=0;index<3;index++)
  1444. {
  1445. Assert.AreEqual(true,ds.Tables[1].GetErrors()[index].RowError.Length > 10,"DT179");
  1446. }
  1447. }
  1448. catch (AssertionException exc) {throw exc;}
  1449. catch (Exception exc)
  1450. {
  1451. Assert.Fail("DT180: Wrong exception type. Got:" + exc);
  1452. }
  1453. }
  1454. private DataRowAction drExpectedAction;
  1455. public void OnRowChanged()
  1456. {
  1457. ProtectedTestClass dt = new ProtectedTestClass();
  1458. EventRaised = false;
  1459. dt.OnRowChanged_Test(DataRowAction.Nothing );
  1460. Assert.IsFalse(EventRaised ,"DT181" );
  1461. dt.RowChanged += new DataRowChangeEventHandler(OnRowChanged_Handler);
  1462. foreach (int i in Enum.GetValues(typeof(DataRowAction)))
  1463. {
  1464. EventRaised = false;
  1465. EventValues = false;
  1466. drExpectedAction = (DataRowAction)i;
  1467. dt.OnRowChanged_Test(drExpectedAction);
  1468. Assert.IsTrue(EventRaised ,"DT182" );
  1469. Assert.IsTrue(EventValues ,"DT183" );
  1470. }
  1471. dt.RowChanged -= new DataRowChangeEventHandler(OnRowChanged_Handler);
  1472. }
  1473. private void OnRowChanged_Handler(Object sender,DataRowChangeEventArgs e)
  1474. {
  1475. DataTable dt = (DataTable)sender;
  1476. if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
  1477. EventValues = true;
  1478. EventRaised = true;
  1479. }
  1480. [Test]
  1481. public void OnRowChanging()
  1482. {
  1483. ProtectedTestClass dt = new ProtectedTestClass();
  1484. EventRaised = false;
  1485. dt.OnRowChanging_Test(DataRowAction.Nothing );
  1486. Assert.IsFalse(EventRaised ,"DT184" );
  1487. dt.RowChanging += new DataRowChangeEventHandler(OnRowChanging_Handler);
  1488. foreach (int i in Enum.GetValues(typeof(DataRowAction)))
  1489. {
  1490. EventRaised = false;
  1491. EventValues = false;
  1492. drExpectedAction = (DataRowAction)i;
  1493. dt.OnRowChanging_Test(drExpectedAction);
  1494. Assert.IsTrue(EventRaised ,"DT185" );
  1495. Assert.IsTrue(EventValues ,"DT186" );
  1496. }
  1497. dt.RowChanging -= new DataRowChangeEventHandler(OnRowChanging_Handler);
  1498. }
  1499. private void OnRowChanging_Handler(Object sender,DataRowChangeEventArgs e)
  1500. {
  1501. DataTable dt = (DataTable)sender;
  1502. if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
  1503. EventValues = true;
  1504. EventRaised = true;
  1505. }
  1506. [Test]
  1507. public void OnRowDeleted()
  1508. {
  1509. ProtectedTestClass dt = new ProtectedTestClass();
  1510. EventRaised = false;
  1511. dt.OnRowDeleted_Test(DataRowAction.Nothing );
  1512. Assert.IsFalse(EventRaised ,"DT187" );
  1513. dt.RowDeleted += new DataRowChangeEventHandler(OnRowDeleted_Handler);
  1514. foreach (int i in Enum.GetValues(typeof(DataRowAction)))
  1515. {
  1516. EventRaised = false;
  1517. EventValues = false;
  1518. drExpectedAction = (DataRowAction)i;
  1519. dt.OnRowDeleted_Test(drExpectedAction);
  1520. Assert.IsTrue(EventRaised ,"DT188" );
  1521. Assert.IsTrue(EventValues ,"DT189" );
  1522. }
  1523. dt.RowDeleted -= new DataRowChangeEventHandler(OnRowDeleted_Handler);
  1524. }
  1525. private void OnRowDeleted_Handler(Object sender,DataRowChangeEventArgs e)
  1526. {
  1527. DataTable dt = (DataTable)sender;
  1528. if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
  1529. EventValues = true;
  1530. EventRaised = true;
  1531. }
  1532. public void OnRowDeleting()
  1533. {
  1534. ProtectedTestClass dt = new ProtectedTestClass();
  1535. EventRaised = false;
  1536. dt.OnRowDeleting_Test(DataRowAction.Nothing );
  1537. Assert.IsFalse(EventRaised ,"DT190" );
  1538. dt.RowDeleting += new DataRowChangeEventHandler(OnRowDeleting_Handler);
  1539. foreach (int i in Enum.GetValues(typeof(DataRowAction)))
  1540. {
  1541. EventRaised = false;
  1542. EventValues = false;
  1543. drExpectedAction = (DataRowAction)i;
  1544. dt.OnRowDeleting_Test(drExpectedAction);
  1545. Assert.IsTrue(EventRaised ,"DT191" );
  1546. Assert.IsTrue(EventValues ,"DT192" );
  1547. }
  1548. dt.RowDeleting -= new DataRowChangeEventHandler(OnRowDeleting_Handler);
  1549. }
  1550. [Test]
  1551. public void BeginInit_PrimaryKey_1 ()
  1552. {
  1553. DataTable table = new DataTable ();
  1554. DataColumn col = table.Columns.Add ("col", typeof (int));
  1555. table.PrimaryKey = new DataColumn[] {col};
  1556. table.AcceptChanges ();
  1557. Assert.AreEqual (1, table.PrimaryKey.Length, "#1");
  1558. table.BeginInit ();
  1559. DataColumn col2 = new DataColumn ("col2", typeof (int));
  1560. table.Columns.AddRange (new DataColumn[] {col2});
  1561. table.PrimaryKey = new DataColumn[] {col2};
  1562. table.EndInit ();
  1563. Assert.AreEqual (1, table.PrimaryKey.Length, "#2");
  1564. Assert.AreEqual ("col2", table.PrimaryKey[0].ColumnName, "#3");
  1565. }
  1566. [Test]
  1567. [ExpectedException (typeof (ArgumentException))]
  1568. public void BeginInit_PrimaryKey_2()
  1569. {
  1570. DataTable table = new DataTable ();
  1571. DataColumn col = table.Columns.Add ("col", typeof (int));
  1572. table.PrimaryKey = new DataColumn[] {col};
  1573. table.AcceptChanges ();
  1574. // ms.net behavior.
  1575. table.BeginInit ();
  1576. DataColumn col1 = new DataColumn ("col1", typeof (int));
  1577. table.Columns.AddRange (new DataColumn[] {col1});
  1578. UniqueConstraint uc = new UniqueConstraint ("", new String[] {"col1"}, true);
  1579. table.Constraints.AddRange (new Constraint[] {uc});
  1580. table.EndInit ();
  1581. }
  1582. [Test]
  1583. public void BeginInit_PrimaryKey_3 ()
  1584. {
  1585. DataTable table = new DataTable ();
  1586. DataColumn col1 = table.Columns.Add ("col1", typeof (int));
  1587. DataColumn col2 = table.Columns.Add ("col2", typeof (int));
  1588. // ms.net behavior
  1589. table.BeginInit ();
  1590. UniqueConstraint uc = new UniqueConstraint ("", new String[] {"col1"}, true);
  1591. table.Constraints.AddRange (new Constraint[] {uc});
  1592. table.PrimaryKey = new DataColumn [] {col2};
  1593. table.EndInit ();
  1594. Assert.AreEqual ("col1", table.PrimaryKey[0].ColumnName, "#1");
  1595. }
  1596. [Test]
  1597. public void PrimaryKey_OnFailing ()
  1598. {
  1599. DataTable table = new DataTable ();
  1600. DataColumn col1 = table.Columns.Add ("col1", typeof (int));
  1601. table.PrimaryKey = new DataColumn[] {col1};
  1602. try {
  1603. table.PrimaryKey = new DataColumn[] { new DataColumn ("col2", typeof (int))};
  1604. } catch (Exception e) {}
  1605. Assert.AreEqual ("col1", table.PrimaryKey[0].ColumnName, "#1");
  1606. }
  1607. [Test]
  1608. public void BeginInit_Cols_Constraints ()
  1609. {
  1610. DataTable table = new DataTable ();
  1611. // if both cols and constraints are added after BeginInit, the cols
  1612. // should be added, before the constraints are added/validated
  1613. table.BeginInit ();
  1614. DataColumn col1 = new DataColumn ("col1", typeof (int));
  1615. table.Columns.AddRange (new DataColumn[] {col1});
  1616. UniqueConstraint uc = new UniqueConstraint ("", new String[] {"col1"}, false);
  1617. table.Constraints.AddRange (new Constraint[] {uc});
  1618. // no exception shud be thrown
  1619. table.EndInit ();
  1620. Assert.AreEqual (1, table.Constraints.Count, "#1");
  1621. }
  1622. [Test]
  1623. public void LoadDataRow_ExistingData ()
  1624. {
  1625. DataSet ds = new DataSet ();
  1626. DataTable table = ds.Tables.Add ();
  1627. table.Columns.Add ("col1", typeof (int));
  1628. table.Columns.Add ("col2", typeof (int));
  1629. table.PrimaryKey = new DataColumn[] {table.Columns [0]};
  1630. table.BeginLoadData ();
  1631. table.LoadDataRow (new object[] {1,10}, true);
  1632. table.LoadDataRow (new object[] {2,10}, true);
  1633. table.LoadDataRow (new object[] {3,10}, true);
  1634. table.LoadDataRow (new object[] {4,10}, true);
  1635. table.EndLoadData ();
  1636. Assert.AreEqual (4, table.Rows.Count, "#1");
  1637. Assert.AreEqual (10, table.Rows [0][1], "#2");
  1638. Assert.AreEqual (10, table.Rows [1][1], "#3");
  1639. Assert.AreEqual (10, table.Rows [2][1], "#4");
  1640. Assert.AreEqual (10, table.Rows [3][1], "#5");
  1641. table.BeginLoadData ();
  1642. table.LoadDataRow (new object[] {1,100}, true);
  1643. table.LoadDataRow (new object[] {2,100}, true);
  1644. table.LoadDataRow (new object[] {3,100}, true);
  1645. table.LoadDataRow (new object[] {4,100}, true);
  1646. table.EndLoadData ();
  1647. Assert.AreEqual (4, table.Rows.Count, "#6");
  1648. Assert.AreEqual (100, table.Rows [0][1], "#7");
  1649. Assert.AreEqual (100, table.Rows [1][1], "#8");
  1650. Assert.AreEqual (100, table.Rows [2][1], "#7");
  1651. Assert.AreEqual (100, table.Rows [3][1], "#10");
  1652. }
  1653. private void OnRowDeleting_Handler(Object sender,DataRowChangeEventArgs e)
  1654. {
  1655. DataTable dt = (DataTable)sender;
  1656. if (dt.Rows[0].Equals(e.Row) && e.Action == drExpectedAction)
  1657. EventValues = true;
  1658. EventRaised = true;
  1659. }
  1660. [Test]
  1661. public void Select_StringString()
  1662. {
  1663. DataTable dt = DataProvider.CreateChildDataTable();
  1664. DataRow[] drSelect;
  1665. System.Collections.ArrayList al;
  1666. //add some rows
  1667. dt.Rows.Add(new object[] {99,88,"bla","wowww"});
  1668. dt.Rows.Add(new object[] {999,888,"","woowww"});
  1669. //get excepted resault
  1670. al = new System.Collections.ArrayList();
  1671. foreach (DataRow dr in dt.Rows )
  1672. {
  1673. if ((int)dr["ChildId"] == 1)
  1674. al.Add(dr);
  1675. }
  1676. //al.Reverse();
  1677. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1678. drSelect = dt.Select("ChildId=1","ParentId Desc");
  1679. Assert.AreEqual(al.ToArray(),drSelect ,"DT193");
  1680. //get excepted resault
  1681. al = new System.Collections.ArrayList();
  1682. foreach (DataRow dr in dt.Rows )
  1683. {
  1684. if (dr["String1"].ToString() == "1-String1")
  1685. al.Add(dr);
  1686. }
  1687. //al.Reverse();
  1688. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1689. drSelect = dt.Select("String1='1-String1'","ParentId Desc");
  1690. Assert.AreEqual(al.ToArray(),drSelect ,"DT194");
  1691. //get excepted resault
  1692. al = new System.Collections.ArrayList();
  1693. foreach (DataRow dr in dt.Rows )
  1694. {
  1695. if ((int)dr["ChildId"] == 1 && dr["String1"].ToString() == "1-String1")
  1696. al.Add(dr);
  1697. }
  1698. //al.Reverse();
  1699. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1700. drSelect = dt.Select("ChildId=1 and String1='1-String1'","ParentId Desc");
  1701. Assert.AreEqual(al.ToArray(),drSelect ,"DT195");
  1702. //get excepted resault
  1703. al = new System.Collections.ArrayList();
  1704. foreach (DataRow dr in dt.Rows )
  1705. {
  1706. if (dr["String1"].ToString().Length < 4 )
  1707. al.Add(dr);
  1708. }
  1709. //al.Reverse();
  1710. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1711. drSelect = dt.Select("Len(String1) < 4 ","ParentId Desc");
  1712. Assert.AreEqual(al.ToArray(),drSelect ,"DT196");
  1713. //get excepted resault
  1714. al = new System.Collections.ArrayList();
  1715. foreach (DataRow dr in dt.Rows )
  1716. {
  1717. if ( dr["String1"].ToString().IndexOf("String") > 0 )
  1718. al.Add(dr);
  1719. }
  1720. //al.Reverse();
  1721. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1722. drSelect = dt.Select("String1 like '%%String*' ","ParentId Desc");
  1723. Assert.AreEqual(al.ToArray(),drSelect ,"DT197");
  1724. //get excepted resault
  1725. al = new System.Collections.ArrayList();
  1726. foreach (DataRow dr in dt.Rows )
  1727. {
  1728. if (((int)dr["ChildId"] == 2) || ((int)dr["ChildId"] == 3))
  1729. al.Add(dr);
  1730. }
  1731. //al.Reverse();
  1732. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1733. drSelect = dt.Select("ChildId in (2,3) ","ParentId Desc");
  1734. Assert.AreEqual(al.ToArray(),drSelect ,"DT198");
  1735. //get excepted resault
  1736. al = new System.Collections.ArrayList();
  1737. foreach (DataRow dr in dt.Rows )
  1738. {
  1739. if ((((int)dr["ChildId"] * (int)dr["ParentId"]) > 5 ))
  1740. al.Add(dr);
  1741. }
  1742. al.Sort(new DataRowsComparer("ChildId", "Asc"));
  1743. drSelect = dt.Select("ChildId * ParentId > 5 ","ChildId Asc");
  1744. // Cannot check this way as ArrayList.Sort uses unstable sort and
  1745. // so the Order of the elements is not preserved when elements are equal
  1746. //Assert.AreEqual(al.ToArray(),drSelect ,"DT199");
  1747. Assert.AreEqual (al.Count, drSelect.Length, "#DT199");
  1748. for (int i=0; i<al.Count; ++i){
  1749. // check the datarow is present and that the sort order is correct
  1750. Assert.IsTrue (Array.IndexOf (drSelect, al[i]) != -1, "#DT_199_2_"+i);
  1751. Assert.AreEqual ( ((DataRow)al[i])["ChildId"], drSelect[i]["ChildId"], "#DT_199_1" +i);
  1752. }
  1753. //get excepted resault
  1754. al = new System.Collections.ArrayList();
  1755. foreach (DataRow dr in dt.Rows )
  1756. {
  1757. if (dr["String2"].ToString().Substring(2,3) == "Str" )
  1758. al.Add(dr);
  1759. }
  1760. al.Sort(new DataRowsComparer("ParentId", "Desc"));
  1761. drSelect = dt.Select("SubString(String2,3,3) like 'Str' ","ParentId Desc");
  1762. Assert.AreEqual(al.ToArray(),drSelect ,"DT200");
  1763. }
  1764. [Test]
  1765. [ExpectedException(typeof(ArgumentException))]
  1766. #if !TARGET_JVM
  1767. [Category ("NotWorking")]
  1768. #endif
  1769. public void Select_StringString_2()
  1770. {
  1771. DataTable dt = DataProvider.CreateParentDataTable();
  1772. //Checking the parsing of the sort string
  1773. dt.Select(dt.Columns[0].ColumnName + "=1","[x");
  1774. }
  1775. [Test]
  1776. [ExpectedException(typeof(IndexOutOfRangeException))]
  1777. public void Select_StringString_3()
  1778. {
  1779. DataTable dt = DataProvider.CreateParentDataTable();
  1780. //Select - parse sort string checking 1");
  1781. dt.Select(dt.Columns[0].ColumnName,dt.Columns[0].ColumnName + "1");
  1782. }
  1783. internal class DataRowsComparer : System.Collections.IComparer
  1784. {
  1785. #region Memebers
  1786. private string _columnName;
  1787. private string _direction;
  1788. #endregion
  1789. #region Constructors
  1790. public DataRowsComparer(string columnName, string direction)
  1791. {
  1792. _columnName = columnName;
  1793. if (direction.ToLower() != "asc" && direction.ToLower() != "desc")
  1794. {
  1795. throw new ArgumentException("Direction can only be one of: 'asc' or 'desc'");
  1796. }
  1797. _direction = direction;
  1798. }
  1799. #endregion
  1800. #region IComparer Members
  1801. public int Compare(object x, object y)
  1802. {
  1803. DataRow drX = (DataRow)x;
  1804. DataRow drY = (DataRow)y;
  1805. object objX = drX[_columnName];
  1806. object objY = drY[_columnName];
  1807. int compareResult = System.Collections.Comparer.Default.Compare(objX, objY);
  1808. //If we are comparing desc we need to reverse the result.
  1809. if (_direction.ToLower() == "desc")
  1810. {
  1811. compareResult = -compareResult;
  1812. }
  1813. return compareResult;
  1814. }
  1815. #endregion
  1816. }
  1817. }
  1818. }