PageRenderTime 53ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/DataGridViewTest.cs

https://bitbucket.org/danipen/mono
C# | 2604 lines | 2530 code | 43 blank | 31 comment | 40 complexity | 6a8b8b8c2db4e5494b9b89f469244a70 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2005, 2006, 2007 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Author:
  23. // Pedro Martínez Juliá <pedromj@gmail.com>
  24. // Daniel Nauck (dna(at)mono-project(dot)de)
  25. // Ivan N. Zlatev <contact@i-nz.net>
  26. #if NET_2_0
  27. using System;
  28. using System.Data;
  29. using System.Drawing;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.Diagnostics;
  34. using System.IO;
  35. using System.Runtime.InteropServices;
  36. using System.Text;
  37. using System.Windows.Forms;
  38. using NUnit.Framework;
  39. namespace MonoTests.System.Windows.Forms
  40. {
  41. [TestFixture]
  42. public class DataGridViewTest : TestHelper
  43. {
  44. // Send a mouse event in Win32.
  45. [DllImport ("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  46. private static extern void mouse_event (long dwFlags, long dx, long dy, long dwData, long dwExtraInfo);
  47. private const int MOUSEEVENTF_LEFTDOWN = 0x02;
  48. private const int MOUSEEVENTF_LEFTUP = 0x04;
  49. private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
  50. private const int MOUSEEVENTF_RIGHTUP = 0x10;
  51. private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
  52. // Set the mouse-pointer position in Win32.
  53. [DllImport ("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  54. private static extern long SetCursorPos (int x, int y);
  55. // Convert from window coordinates to screen coordinates in Win32.
  56. [DllImport ("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  57. private static extern bool ClientToScreen (IntPtr hWnd, ref Win32Point point);
  58. [StructLayout (LayoutKind.Sequential)]
  59. private struct Win32Point
  60. {
  61. public int x;
  62. public int y;
  63. };
  64. private DataGridView grid = null;
  65. [SetUp]
  66. protected override void SetUp()
  67. {
  68. grid = new DataGridView();
  69. base.SetUp ();
  70. }
  71. [TearDown]
  72. protected override void TearDown ()
  73. {
  74. grid.Dispose ();
  75. base.TearDown ();
  76. }
  77. [Test]
  78. [ExpectedException (typeof (InvalidOperationException), ExpectedMessage = "Generating Clipboard content is not supported when the ClipboardCopyMode property is Disable.")]
  79. public void GetClipboardContentsDisabled ()
  80. {
  81. using (DataGridView dgv = new DataGridView ()) {
  82. dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
  83. object o = dgv.GetClipboardContent ();
  84. }
  85. }
  86. private class ExposeProtectedProperties : DataGridView
  87. {
  88. public new Padding DefaultPadding { get { return base.DefaultPadding; } }
  89. public new Size DefaultSize { get { return base.DefaultSize; } }
  90. public new bool IsDoubleBuffered { get { return base.DoubleBuffered; } }
  91. public ControlStyles GetControlStyles ()
  92. {
  93. ControlStyles retval = (ControlStyles)0;
  94. foreach (ControlStyles cs in Enum.GetValues (typeof (ControlStyles)))
  95. if (this.GetStyle (cs) == true)
  96. retval |= cs;
  97. return retval;
  98. }
  99. public bool PublicIsInputKey (Keys keyData)
  100. {
  101. return base.IsInputKey (keyData);
  102. }
  103. public bool PublicIsInputChar (char charCode)
  104. {
  105. return base.IsInputChar (charCode);
  106. }
  107. }
  108. #region GenerateClipboardTest
  109. public static void GenerateClipboardTest ()
  110. {
  111. GenerateClipboardTest (false);
  112. GenerateClipboardTest (true);
  113. }
  114. public static string GenerateClipboardTest (bool headers)
  115. {
  116. StringBuilder result = new StringBuilder ();
  117. int tab = 0;
  118. string classname = headers ? "DataGridViewClipboardHeaderTest" : "DataGridViewClipboardTest";
  119. append (result, tab, "//");
  120. append (result, tab, "// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)");
  121. append (result, tab, "//");
  122. append (result, tab, "// Author:");
  123. append (result, tab, "// DataGridViewTest.GenerateClipboardTest ({0});", headers.ToString ().ToLower ());
  124. append (result, tab, "//");
  125. append (result, tab, "#if NET_2_0");
  126. append (result, tab, "using NUnit.Framework;");
  127. append (result, tab, "using System;");
  128. append (result, tab, "using System.Drawing;");
  129. append (result, tab, "using System.Windows.Forms;");
  130. append (result, tab, "using System.ComponentModel;");
  131. append (result, tab, "using System.Collections;");
  132. append (result, tab, "using System.Text;");
  133. append (result, tab, "using System.Collections.Generic;");
  134. append (result, tab, "using System.Diagnostics;");
  135. append (result, tab, "using System.IO;");
  136. append (result, tab, "namespace MonoTests.System.Windows.Forms {"); tab++;
  137. append (result, tab, "[TestFixture]");
  138. append (result, tab, "public class {0} {{", classname); tab++;
  139. append (result, tab, "[Test]");
  140. append (result, tab, "public void Test () {"); tab++;
  141. append (result, tab, "DataObject data;");
  142. append (result, tab, "DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell row_header_cell;");
  143. append (result, tab, "DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell col_header_cell;");
  144. //append (result, tab, "string csv = null, html = null, utext = null, text = null;");
  145. append (result, tab, "string code = null;");
  146. int counter;
  147. List<List<int>> selected_bands = new List<List<int>> ();
  148. List<List<CellSelection>> selected_cells = new List<List<CellSelection>> ();
  149. selected_bands.Add (new List<int> ());
  150. selected_bands.Add (new List<int> (new int [] { 0 }));
  151. selected_bands.Add (new List<int> (new int [] { 2 }));
  152. selected_bands.Add (new List<int> (new int [] { 1, 2 }));
  153. selected_bands.Add (new List<int> (new int [] { 1, 3 }));
  154. selected_cells.Add (new List<CellSelection> ());
  155. selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (0, 0, true) }));
  156. selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (2, 2, false) }));
  157. selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (0, 0, false), new CellSelection (2, 2, true) }));
  158. foreach (DataGridViewClipboardCopyMode copymode in Enum.GetValues (typeof (DataGridViewClipboardCopyMode))) {
  159. if (copymode == DataGridViewClipboardCopyMode.Disable)
  160. continue;
  161. counter = 0;
  162. foreach (DataGridViewSelectionMode selectionmode in Enum.GetValues (typeof (DataGridViewSelectionMode))) {
  163. bool is_row_selectable, is_col_selectable, is_cell_selectable;
  164. is_row_selectable = selectionmode == DataGridViewSelectionMode.RowHeaderSelect || selectionmode == DataGridViewSelectionMode.FullRowSelect;
  165. is_col_selectable = selectionmode == DataGridViewSelectionMode.ColumnHeaderSelect || selectionmode == DataGridViewSelectionMode.FullColumnSelect;
  166. is_cell_selectable = selectionmode == DataGridViewSelectionMode.CellSelect || selectionmode == DataGridViewSelectionMode.ColumnHeaderSelect || selectionmode == DataGridViewSelectionMode.RowHeaderSelect;
  167. foreach (List<int> cols in selected_bands) {
  168. if (!is_col_selectable && cols.Count > 0)
  169. continue;
  170. foreach (List<int> rows in selected_bands) {
  171. if (!is_row_selectable && rows.Count > 0)
  172. continue;
  173. foreach (List<CellSelection> cells in selected_cells) {
  174. if (!is_cell_selectable && cells.Count > 0)
  175. continue;
  176. using (DataGridView dgv = DataGridViewCommon.CreateAndFillForClipboard ()) {
  177. dgv.SelectionMode = selectionmode;
  178. dgv.ClipboardCopyMode = copymode;
  179. bool any_selected = false;
  180. if (is_col_selectable && cols.Count > 0) {
  181. foreach (int c in cols) {
  182. dgv.Columns [c].Selected = true;
  183. any_selected = true;
  184. }
  185. }
  186. if (is_row_selectable && rows.Count > 0) {
  187. foreach (int r in rows) {
  188. dgv.Rows [r].Selected = true;
  189. any_selected = true;
  190. }
  191. }
  192. if (is_cell_selectable && cells.Count > 0) {
  193. foreach (CellSelection selection in cells) {
  194. DataGridViewCell cell = dgv.Rows [selection.Row].Cells [selection.Col];
  195. if (cell.Selected != selection.Selected) {
  196. cell.Selected = selection.Selected;
  197. any_selected = true;
  198. }
  199. }
  200. }
  201. if (any_selected == false && !(cols.Count == 0 && rows.Count == 0 && cells.Count == 0)) {
  202. continue;
  203. }
  204. generate_case (result, dgv, copymode.ToString () + "#" + (counter++).ToString (), headers);
  205. }
  206. }
  207. }
  208. }
  209. }
  210. }
  211. append (result, --tab, "}");
  212. append (result, --tab, "}");
  213. append (result, --tab, "}");
  214. append (result, tab, "#endif"); ;
  215. throw new NotImplementedException ("Where am I?");
  216. // Uncomment the following line, change the path, and comment out the exception.
  217. //File.WriteAllText (@"Z:\mono\head\mcs\class\SWF\Test\System.Windows.Forms\" + classname + ".cs", result.ToString ());
  218. return string.Empty;
  219. }
  220. private static string tabs (int t) { return new string ('\t', t); }
  221. private static void append (StringBuilder result, int tab, string text) { result.Append (tabs (tab) + text + "\n"); }
  222. private static void append (StringBuilder result, int tab, string text, params object [] args) { result.Append (tabs (tab) + string.Format (text, args) + "\n"); }
  223. private static string cs_encode (string literal, string newline) {
  224. bool has_newlines = literal.Contains ("\r\n");
  225. bool format_string = has_newlines;
  226. literal = literal.Replace ("\\", "\\\\");
  227. literal = literal.Replace ("\"", "\\\"");
  228. literal = literal.Replace ("\t", "\\t");
  229. if (has_newlines) {
  230. if (newline == @"""\r\n""") {
  231. literal = literal.Replace ("\r\n", @"\r\n");
  232. format_string = false;
  233. } else {
  234. literal = literal.Replace ("\r\n", "{0}");
  235. }
  236. }
  237. literal = "\"" + literal + "\"";
  238. if (format_string) {
  239. return "string.Format (" + literal/*.Replace ("{", "{{").Replace ("}", "}}")*/ + ", " + newline + ")";
  240. } else {
  241. return literal;
  242. }
  243. }
  244. private static string cs_encode (string literal) {
  245. return cs_encode (literal, "Environment.NewLine");
  246. }
  247. private class CellSelection {
  248. public bool Selected;
  249. public int Row;
  250. public int Col;
  251. public CellSelection (int Row, int Col, bool Selected) {
  252. this.Selected = Selected;
  253. this.Row = Row;
  254. this.Col = Col;
  255. }
  256. }
  257. static private void generate_case (StringBuilder result, DataGridView dgv, string message, bool headers)
  258. {
  259. Console.WriteLine (message + ", current length: " + result.Length.ToString ());
  260. Debug.WriteLine (message + ", current length: " + result.Length.ToString ());
  261. if (headers) {
  262. if (dgv.SelectionMode != DataGridViewSelectionMode.CellSelect)
  263. return;
  264. if (dgv.ClipboardCopyMode != DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText)
  265. return;
  266. }
  267. int tab = 3;
  268. DataObject data;
  269. string csv = null, html = null, utext = null, text = null;
  270. string code = null;
  271. DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell row_header_cell;
  272. DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell col_header_cell;
  273. int counter = 0;
  274. append (result, tab, "using (DataGridView dgv = DataGridViewCommon.CreateAndFillForClipboard ()) {");
  275. tab++;
  276. append (result, tab, "dgv.SelectionMode = DataGridViewSelectionMode.{0};", dgv.SelectionMode.ToString ());
  277. append (result, tab, "dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.{0};", dgv.ClipboardCopyMode.ToString ());
  278. switch (dgv.SelectionMode) {
  279. case DataGridViewSelectionMode.FullRowSelect:
  280. foreach (DataGridViewRow row in dgv.Rows) {
  281. if (row.Selected) {
  282. append (result, tab, "dgv.Rows [{0}].Selected = true;", row.Index);
  283. }
  284. }
  285. break;
  286. case DataGridViewSelectionMode.FullColumnSelect:
  287. foreach (DataGridViewColumn col in dgv.Columns) {
  288. if (col.Selected) {
  289. append (result, tab, "dgv.Columns [{0}].Selected = true;", col.Index);
  290. }
  291. }
  292. break;
  293. case DataGridViewSelectionMode.ColumnHeaderSelect:
  294. case DataGridViewSelectionMode.RowHeaderSelect:
  295. case DataGridViewSelectionMode.CellSelect:
  296. if (dgv.SelectionMode == DataGridViewSelectionMode.RowHeaderSelect) {
  297. foreach (DataGridViewRow row in dgv.Rows) {
  298. if (row.Selected) {
  299. append (result, tab, "dgv.Rows [{0}].Selected = true;", row.Index);
  300. }
  301. }
  302. }
  303. if (dgv.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect) {
  304. foreach (DataGridViewColumn col in dgv.Columns) {
  305. if (col.Selected) {
  306. append (result, tab, "dgv.Columns [{0}].Selected = true;", col.Index);
  307. }
  308. }
  309. }
  310. for (int r = 0; r < dgv.RowCount; r++) {
  311. for (int c = 0; c < dgv.ColumnCount; c++) {
  312. bool rowS = dgv.Rows [r].Selected;
  313. bool colS = dgv.Columns [c].Selected;
  314. bool cellS = dgv.Rows [r].Cells [c].Selected;
  315. if ((rowS || colS) && !cellS) {
  316. append (result, tab, "dgv.Rows [{0}].Cells [{1}].Selected = false;", r, c);
  317. } else if ((!rowS && !colS) && cellS) {
  318. append (result, tab, "dgv.Rows [{0}].Cells [{1}].Selected = true;", r, c);
  319. }
  320. }
  321. }
  322. break;
  323. }
  324. if (!headers) {
  325. data = dgv.GetClipboardContent ();
  326. append (result, tab, "data = dgv.GetClipboardContent ();");
  327. if (data == null) {
  328. append (result, tab, "Assert.IsNull (data, {0});", cs_encode ("#" + message + "-" + (counter++).ToString ()));
  329. } else {
  330. append (result, tab, "Assert.IsNotNull (data, {0});", cs_encode ("#" + message + "-" + (counter++).ToString ()));
  331. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  332. html = data.GetData (DataFormats.Html) as string;
  333. utext = data.GetData (DataFormats.UnicodeText) as string;
  334. text = data.GetData (DataFormats.Text) as string;
  335. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.CommaSeparatedValue), {1});", cs_encode (csv), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  336. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.Html), {1});", cs_encode (html, @"""\r\n"""), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  337. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.UnicodeText), {1});", cs_encode (utext), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  338. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.Text), {1});", cs_encode (text), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  339. }
  340. } else {
  341. bool [] bools = new bool [] { true, false };
  342. string [] formats = new string [] { DataFormats.Text, DataFormats.UnicodeText, DataFormats.Html, DataFormats.CommaSeparatedValue };
  343. foreach (bool a in bools) {
  344. foreach (bool b in bools) {
  345. foreach (bool c in bools) {
  346. foreach (bool d in bools) {
  347. foreach (string format in formats) {
  348. bool did_selected = false;
  349. bool did_unselected = false;
  350. foreach (DataGridViewRow row in dgv.Rows) {
  351. int i = row.Index;
  352. if (row.Selected) {
  353. if (did_selected)
  354. continue;
  355. did_selected = true;
  356. } else {
  357. if (did_unselected)
  358. continue;
  359. did_unselected = true;
  360. }
  361. row_header_cell = row.HeaderCell as DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell;
  362. if (row_header_cell == null) {
  363. append (result, tab, "Assert.IsNull (dgv.Rows [{0}].Headercell, {1});", row.Index, cs_encode ("#" + message + "-" + (counter++).ToString ()));
  364. } else {
  365. append (result, tab, "row_header_cell = dgv.Rows [{0}].HeaderCell as DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell;", row.Index);
  366. code = cs_encode (row_header_cell.GetClipboardContentPublic (i, a, b, c, d, format) as string);
  367. append (result, tab, "code = row_header_cell.GetClipboardContentPublic ({0}, {1}, {2}, {3}, {4}, \"{5}\") as string;", i, a.ToString ().ToLower (), b.ToString ().ToLower (), c.ToString ().ToLower (), d.ToString ().ToLower (), format);
  368. append (result, tab, "Assert.AreEqual ({0}, code, {1});", code, cs_encode ("#" + message + "-" + (counter++).ToString ()));
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. foreach (bool a in bools) {
  377. foreach (bool b in bools) {
  378. foreach (bool c in bools) {
  379. foreach (bool d in bools) {
  380. foreach (string format in formats) {
  381. bool did_selected = false;
  382. bool did_unselected = false;
  383. foreach (DataGridViewColumn col in dgv.Columns) {
  384. int i = -1;
  385. if (col.Index > 1)
  386. continue;
  387. if (col.Selected) {
  388. if (did_selected)
  389. continue;
  390. did_selected = true;
  391. } else {
  392. if (did_unselected)
  393. continue;
  394. did_unselected = true;
  395. }
  396. col_header_cell = col.HeaderCell as DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell;
  397. append (result, tab, "col_header_cell = dgv.Columns [{0}].HeaderCell as DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell;", col.Index);
  398. code = cs_encode (col_header_cell.GetClipboardContentPublic (i, a, b, c, d, format) as string);
  399. append (result, tab, "code = col_header_cell.GetClipboardContentPublic ({0}, {1}, {2}, {3}, {4}, \"{5}\") as string;", i, a.ToString ().ToLower (), b.ToString ().ToLower (), c.ToString ().ToLower (), d.ToString ().ToLower (), format);
  400. append (result, tab, "Assert.AreEqual ({0}, code, {1});", code, cs_encode ("#" + message + "-" + (counter++).ToString ()));
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. }
  408. tab--;
  409. append (result, tab, "}");
  410. }
  411. #endregion GenerateClipboardTest
  412. [Test]
  413. public void GetClipboardContents ()
  414. {
  415. DataObject data;
  416. string csv, html, utext, text;
  417. using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
  418. data = dgv.GetClipboardContent ();
  419. Assert.IsNull (data, "#01");
  420. dgv.Rows [0].Cells [0].Selected = true;
  421. data = dgv.GetClipboardContent ();
  422. Assert.IsNotNull (data, "#B1");
  423. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
  424. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
  425. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  426. html = data.GetData (DataFormats.Html) as string;
  427. utext = data.GetData (DataFormats.UnicodeText) as string;
  428. text = data.GetData (DataFormats.Text) as string;
  429. Assert.AreEqual ("Cell A1", csv, "CSV B");
  430. Assert.AreEqual ("Cell A1", utext, "UTEXT B");
  431. Assert.AreEqual ("Cell A1", text, "TEXT B");
  432. Assert.AreEqual (string.Format(@"Version:1.0{0}" +
  433. "StartHTML:00000097{0}" +
  434. "EndHTML:00000211{0}" +
  435. "StartFragment:00000133{0}" +
  436. "EndFragment:00000175{0}" +
  437. "<HTML>{0}" +
  438. "<BODY>{0}" +
  439. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD></TR></TABLE>{0}" +
  440. "<!--EndFragment-->{0}" +
  441. "</BODY>{0}" +
  442. "</HTML>", "\r\n"), html, "HTML B");
  443. dgv.Rows [1].Cells [1].Selected = true;
  444. data = dgv.GetClipboardContent ();
  445. Assert.IsNotNull (data, "#C1");
  446. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
  447. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
  448. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  449. html = data.GetData (DataFormats.Html) as string;
  450. utext = data.GetData (DataFormats.UnicodeText) as string;
  451. text = data.GetData (DataFormats.Text) as string;
  452. Assert.AreEqual (string.Format("Cell A1,{0},Cell B2", Environment.NewLine), csv, "CSV C");
  453. Assert.AreEqual (string.Format("Cell A1\t{0}\tCell B2", Environment.NewLine), utext, "UTEXT C");
  454. Assert.AreEqual (string.Format("Cell A1\t{0}\tCell B2", Environment.NewLine), text, "TEXT C");
  455. string tmp;
  456. tmp = string.Format(@"Version:1.0{0}" +
  457. "StartHTML:00000097{0}" +
  458. "EndHTML:00000266{0}" +
  459. "StartFragment:00000133{0}" +
  460. "EndFragment:00000230{0}" +
  461. "<HTML>{0}" +
  462. "<BODY>{0}" +
  463. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  464. "<!--EndFragment-->{0}" +
  465. "</BODY>{0}" +
  466. "</HTML>", "\r\n");
  467. Assert.AreEqual (string.Format(@"Version:1.0{0}" +
  468. "StartHTML:00000097{0}" +
  469. "EndHTML:00000266{0}" +
  470. "StartFragment:00000133{0}" +
  471. "EndFragment:00000230{0}" +
  472. "<HTML>{0}" +
  473. "<BODY>{0}" +
  474. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  475. "<!--EndFragment-->{0}" +
  476. "</BODY>{0}" +
  477. "</HTML>", "\r\n"), html, "HTML C");
  478. }
  479. }
  480. [Test]
  481. public void GetClipboardContents_HeadersAlways ()
  482. {
  483. DataObject data;
  484. string csv, html, utext, text;
  485. using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
  486. dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  487. data = dgv.GetClipboardContent ();
  488. Assert.IsNull (data, "#01");
  489. dgv.Rows [0].Cells [0].Selected = true;
  490. data = dgv.GetClipboardContent ();
  491. Assert.IsNotNull (data, "#B1");
  492. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
  493. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
  494. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  495. html = data.GetData (DataFormats.Html) as string;
  496. utext = data.GetData (DataFormats.UnicodeText) as string;
  497. text = data.GetData (DataFormats.Text) as string;
  498. Assert.AreEqual (string.Format (",A{0},Cell A1", Environment.NewLine), csv, "CSV B");
  499. Assert.AreEqual (string.Format ("\tA{0}\tCell A1", Environment.NewLine), utext, "UTEXT B");
  500. Assert.AreEqual (string.Format ("\tA{0}\tCell A1", Environment.NewLine), text, "TEXT B");
  501. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  502. "StartHTML:00000097{0}" +
  503. "EndHTML:00000281{0}" +
  504. "StartFragment:00000133{0}" +
  505. "EndFragment:00000245{0}" +
  506. "<HTML>{0}" +
  507. "<BODY>{0}" +
  508. "<!--StartFragment--><TABLE><THEAD><TH>&nbsp;</TH><TH>A</TH></THEAD><TR><TD ALIGN=\"center\">&nbsp;</TD><TD>Cell A1</TD></TR></TABLE>{0}" +
  509. "<!--EndFragment-->{0}" +
  510. "</BODY>{0}" +
  511. "</HTML>", "\r\n"), html, "HTML B");
  512. dgv.Rows [1].Cells [1].Selected = true;
  513. data = dgv.GetClipboardContent ();
  514. Assert.IsNotNull (data, "#C1");
  515. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
  516. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
  517. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  518. html = data.GetData (DataFormats.Html) as string;
  519. utext = data.GetData (DataFormats.UnicodeText) as string;
  520. text = data.GetData (DataFormats.Text) as string;
  521. Assert.AreEqual (string.Format (",A,B{0},Cell A1,{0},,Cell B2", Environment.NewLine), csv, "CSV C");
  522. Assert.AreEqual (string.Format ("\tA\tB{0}\tCell A1\t{0}\t\tCell B2", Environment.NewLine), utext, "UTEXT C");
  523. Assert.AreEqual (string.Format ("\tA\tB{0}\tCell A1\t{0}\t\tCell B2", Environment.NewLine), text, "TEXT C");
  524. string tmp;
  525. tmp = string.Format (@"Version:1.0{0}" +
  526. "StartHTML:00000097{0}" +
  527. "EndHTML:00000266{0}" +
  528. "StartFragment:00000133{0}" +
  529. "EndFragment:00000230{0}" +
  530. "<HTML>{0}" +
  531. "<BODY>{0}" +
  532. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  533. "<!--EndFragment-->{0}" +
  534. "</BODY>{0}" +
  535. "</HTML>", "\r\n");
  536. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  537. "StartHTML:00000097{0}" +
  538. "EndHTML:00000376{0}" +
  539. "StartFragment:00000133{0}" +
  540. "EndFragment:00000340{0}" +
  541. "<HTML>{0}" +
  542. "<BODY>{0}" +
  543. "<!--StartFragment--><TABLE><THEAD><TH>&nbsp;</TH><TH>A</TH><TH>B</TH></THEAD><TR><TD ALIGN=\"center\">&nbsp;</TD><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD ALIGN=\"center\">&nbsp;</TD><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  544. "<!--EndFragment-->{0}" +
  545. "</BODY>{0}" +
  546. "</HTML>", "\r\n"), html, "HTML C");
  547. }
  548. }
  549. [Test]
  550. public void GetClipboardContents_HeadersNever ()
  551. {
  552. DataObject data;
  553. string csv, html, utext, text;
  554. using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
  555. dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
  556. data = dgv.GetClipboardContent ();
  557. Assert.IsNull (data, "#01");
  558. dgv.Rows [0].Cells [0].Selected = true;
  559. data = dgv.GetClipboardContent ();
  560. Assert.IsNotNull (data, "#B1");
  561. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
  562. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
  563. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  564. html = data.GetData (DataFormats.Html) as string;
  565. utext = data.GetData (DataFormats.UnicodeText) as string;
  566. text = data.GetData (DataFormats.Text) as string;
  567. Assert.AreEqual ("Cell A1", csv, "CSV B");
  568. Assert.AreEqual ("Cell A1", utext, "UTEXT B");
  569. Assert.AreEqual ("Cell A1", text, "TEXT B");
  570. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  571. "StartHTML:00000097{0}" +
  572. "EndHTML:00000211{0}" +
  573. "StartFragment:00000133{0}" +
  574. "EndFragment:00000175{0}" +
  575. "<HTML>{0}" +
  576. "<BODY>{0}" +
  577. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD></TR></TABLE>{0}" +
  578. "<!--EndFragment-->{0}" +
  579. "</BODY>{0}" +
  580. "</HTML>", "\r\n"), html, "HTML B");
  581. dgv.Rows [1].Cells [1].Selected = true;
  582. data = dgv.GetClipboardContent ();
  583. Assert.IsNotNull (data, "#C1");
  584. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
  585. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
  586. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  587. html = data.GetData (DataFormats.Html) as string;
  588. utext = data.GetData (DataFormats.UnicodeText) as string;
  589. text = data.GetData (DataFormats.Text) as string;
  590. Assert.AreEqual (string.Format ("Cell A1,{0},Cell B2", Environment.NewLine), csv, "CSV C");
  591. Assert.AreEqual (string.Format ("Cell A1\t{0}\tCell B2", Environment.NewLine), utext, "UTEXT C");
  592. Assert.AreEqual (string.Format ("Cell A1\t{0}\tCell B2", Environment.NewLine), text, "TEXT C");
  593. string tmp;
  594. tmp = string.Format (@"Version:1.0{0}" +
  595. "StartHTML:00000097{0}" +
  596. "EndHTML:00000266{0}" +
  597. "StartFragment:00000133{0}" +
  598. "EndFragment:00000230{0}" +
  599. "<HTML>{0}" +
  600. "<BODY>{0}" +
  601. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  602. "<!--EndFragment-->{0}" +
  603. "</BODY>{0}" +
  604. "</HTML>", "\r\n");
  605. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  606. "StartHTML:00000097{0}" +
  607. "EndHTML:00000266{0}" +
  608. "StartFragment:00000133{0}" +
  609. "EndFragment:00000230{0}" +
  610. "<HTML>{0}" +
  611. "<BODY>{0}" +
  612. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  613. "<!--EndFragment-->{0}" +
  614. "</BODY>{0}" +
  615. "</HTML>", "\r\n"), html, "HTML C");
  616. }
  617. }
  618. [Test]
  619. public void EditingRow ()
  620. {
  621. using (DataGridView dgv = new DataGridView ()) {
  622. Assert.AreEqual (true, dgv.AllowUserToAddRows, "1");
  623. Assert.AreEqual (0, dgv.RowCount, "2");
  624. Assert.AreEqual (-1, dgv.NewRowIndex, "3");
  625. dgv.Columns.Add ("A", "B");
  626. Assert.AreEqual (1, dgv.RowCount, "4");
  627. int added;
  628. added = dgv.Rows.Add ("a");
  629. Assert.AreEqual (0, added, "5");
  630. }
  631. }
  632. [Test] // bug 82226
  633. public void EditingRowAfterAddingColumns ()
  634. {
  635. using (DataGridView _dataGridView = new DataGridView ()) {
  636. DataGridViewTextBoxColumn _nameTextBoxColumn;
  637. DataGridViewTextBoxColumn _firstNameTextBoxColumn;
  638. //
  639. // _nameTextBoxColumn
  640. //
  641. _nameTextBoxColumn = new DataGridViewTextBoxColumn ();
  642. _nameTextBoxColumn.HeaderText = "Name";
  643. _dataGridView.Columns.Add (_nameTextBoxColumn);
  644. //
  645. // _firstNameTextBoxColumn
  646. //
  647. _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
  648. _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  649. _firstNameTextBoxColumn.HeaderText = "First Name";
  650. _dataGridView.Columns.Add (_firstNameTextBoxColumn);
  651. _dataGridView.Rows.Add ("de Icaza", "Miguel");
  652. _dataGridView.Rows.Add ("Toshok", "Chris");
  653. _dataGridView.Rows.Add ("Harper", "Jackson");
  654. Assert.AreEqual (4, _dataGridView.RowCount, "#01");
  655. Assert.AreEqual (2, _dataGridView.Rows [3].Cells.Count, "#02");
  656. }
  657. }
  658. // For testing the editing-control-showing event.
  659. int editingControlShowingTest_FoundColumns;
  660. private void DataGridView_EditingControlShowingTest (object sender,
  661. DataGridViewEditingControlShowingEventArgs e)
  662. {
  663. DataGridView dgv = sender as DataGridView;
  664. if (dgv.CurrentCellAddress.X == 0)
  665. {
  666. // This is the name combo-box column.
  667. // Remember that the event-handler was called for
  668. // this column.
  669. editingControlShowingTest_FoundColumns |= 1;
  670. // Get the combo-box and the column.
  671. ComboBox cb = e.Control as ComboBox;
  672. DataGridViewComboBoxColumn col
  673. = dgv.Columns[0] as DataGridViewComboBoxColumn;
  674. // Since ObjectCollection doesn't support ToArray(), make
  675. // a list of the items in the combo-box and in the column.
  676. List<string> itemList = new List<string> ();
  677. foreach (string item in cb.Items)
  678. itemList.Add (item);
  679. List<string> expectedItemList = new List<string> ();
  680. foreach (string item in col.Items)
  681. expectedItemList.Add (item);
  682. // Make sure the combo-box has the list of allowed
  683. // items from the column.
  684. string items = string.Join (",", itemList.ToArray ());
  685. string expectedItems = string.Join (",", expectedItemList.ToArray ());
  686. Assert.AreEqual (expectedItems, items, "1-1");
  687. // Make sure the combo-box has the right selected item.
  688. Assert.AreEqual ("Boswell", cb.Text, "1-2");
  689. }
  690. else if (dgv.CurrentCellAddress.X == 1)
  691. {
  692. // This is the first-name text-box column.
  693. // Remember that the event-handler was called for
  694. // this column.
  695. editingControlShowingTest_FoundColumns |= 2;
  696. // Get the text-box.
  697. TextBox tb = e.Control as TextBox;
  698. // Make sure the text-box has the right contents.
  699. Assert.AreEqual ("Miguel", tb.Text, "1-3");
  700. }
  701. else if (dgv.CurrentCellAddress.X == 2)
  702. {
  703. // This is the chosen check-box column.
  704. // Remember that the event-handler was called for
  705. // this column.
  706. editingControlShowingTest_FoundColumns |= 4;
  707. // Get the check-box.
  708. CheckBox tb = e.Control as CheckBox;
  709. // Make sure the check-box has the right contents.
  710. Assert.AreEqual (CheckState.Checked, tb.CheckState, "1-4");
  711. }
  712. else
  713. Assert.AreEqual (0, 1, "1-5");
  714. }
  715. [Test] // Xamarin bug 5419
  716. public void EditingControlShowingTest_Unbound ()
  717. {
  718. using (DataGridView _dataGridView = new DataGridView ()) {
  719. DataGridViewComboBoxColumn _nameComboBoxColumn;
  720. DataGridViewTextBoxColumn _firstNameTextBoxColumn;
  721. DataGridViewCheckBoxColumn _chosenCheckBoxColumn;
  722. // Add the event-handler.
  723. _dataGridView.EditingControlShowing
  724. += new DataGridViewEditingControlShowingEventHandler
  725. (DataGridView_EditingControlShowingTest);
  726. // No columns have been found in the event-handler yet.
  727. editingControlShowingTest_FoundColumns = 0;
  728. // _nameComboBoxColumn
  729. _nameComboBoxColumn = new DataGridViewComboBoxColumn ();
  730. _nameComboBoxColumn.HeaderText = "Name";
  731. _dataGridView.Columns.Add (_nameComboBoxColumn);
  732. // _firstNameTextBoxColumn
  733. _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
  734. _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  735. _firstNameTextBoxColumn.HeaderText = "First Name";
  736. _dataGridView.Columns.Add (_firstNameTextBoxColumn);
  737. // _chosenCheckBoxColumn
  738. _chosenCheckBoxColumn = new DataGridViewCheckBoxColumn ();
  739. _chosenCheckBoxColumn.HeaderText = "Chosen";
  740. _dataGridView.Columns.Add (_chosenCheckBoxColumn);
  741. // .NET requires that all possible values for combo-boxes in a column
  742. // are added to the column.
  743. _nameComboBoxColumn.Items.Add ("de Icaza");
  744. _nameComboBoxColumn.Items.Add ("Toshok");
  745. _nameComboBoxColumn.Items.Add ("Harper");
  746. _nameComboBoxColumn.Items.Add ("Boswell");
  747. // Set up the contents of the data-grid.
  748. _dataGridView.Rows.Add ("de Icaza", "Miguel", true);
  749. _dataGridView.Rows.Add ("Toshok", "Chris", false);
  750. _dataGridView.Rows.Add ("Harper", "Jackson", false);
  751. _dataGridView.Rows.Add ("Boswell", "Steven", true);
  752. // Edit a combo-box cell.
  753. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[0];
  754. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[0].Selected, "1-6");
  755. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-7");
  756. _dataGridView.CancelEdit();
  757. // Edit a text-box cell.
  758. _dataGridView.CurrentCell = _dataGridView.Rows[0].Cells[1];
  759. Assert.AreEqual (false, _dataGridView.Rows[3].Cells[0].Selected, "1-8");
  760. Assert.AreEqual (true, _dataGridView.Rows[0].Cells[1].Selected, "1-9");
  761. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-10");
  762. _dataGridView.CancelEdit();
  763. // Edit a check-box cell.
  764. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[2];
  765. Assert.AreEqual (false, _dataGridView.Rows[0].Cells[1].Selected, "1-11");
  766. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[2].Selected, "1-12");
  767. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-13");
  768. _dataGridView.CancelEdit();
  769. // Make sure the event-handler was called each time.
  770. // (DataGridViewCheckBoxCell isn't derived from Control, so the
  771. // EditingControlShowing event doesn't get called for it.)
  772. Assert.AreEqual (3, editingControlShowingTest_FoundColumns, "1-14");
  773. _dataGridView.Dispose();
  774. }
  775. }
  776. // A simple class, for testing the data-binding variant of the
  777. // editing-control-showing event.
  778. private class EcstRecord
  779. {
  780. string name;
  781. string firstName;
  782. bool chosen;
  783. public EcstRecord (string newName, string newFirstName, bool newChosen)
  784. {
  785. name = newName;
  786. firstName = newFirstName;
  787. chosen = newChosen;
  788. }
  789. public string Name
  790. {
  791. get { return name; }
  792. set { name = value; }
  793. }
  794. public string FirstName
  795. {
  796. get { return firstName; }
  797. set { firstName = value; }
  798. }
  799. public bool Chosen
  800. {
  801. get { return chosen; }
  802. set { chosen = value; }
  803. }
  804. };
  805. [Test] // Xamarin bug 5419
  806. public void EditingControlShowingTest_Bound ()
  807. {
  808. using (DataGridView _dataGridView = new DataGridView ()) {
  809. DataGridViewComboBoxColumn _nameComboBoxColumn;
  810. DataGridViewTextBoxColumn _firstNameTextBoxColumn;
  811. DataGridViewCheckBoxColumn _chosenCheckBoxColumn;
  812. _dataGridView.AutoGenerateColumns = false;
  813. // Add the event-handler.
  814. _dataGridView.EditingControlShowing
  815. += new DataGridViewEditingControlShowingEventHandler
  816. (DataGridView_EditingControlShowingTest);
  817. // No columns have been found in the event-handler yet.
  818. editingControlShowingTest_FoundColumns = 0;
  819. // _nameComboBoxColumn
  820. _nameComboBoxColumn = new DataGridViewComboBoxColumn ();
  821. _nameComboBoxColumn.HeaderText = "Name";
  822. _nameComboBoxColumn.DataPropertyName = "Name";
  823. _dataGridView.Columns.Add (_nameComboBoxColumn);
  824. // _firstNameTextBoxColumn
  825. _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
  826. _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  827. _firstNameTextBoxColumn.HeaderText = "First Name";
  828. _firstNameTextBoxColumn.DataPropertyName = "FirstName";
  829. _dataGridView.Columns.Add (_firstNameTextBoxColumn);
  830. // _chosenCheckBoxColumn
  831. _chosenCheckBoxColumn = new DataGridViewCheckBoxColumn ();
  832. _chosenCheckBoxColumn.HeaderText = "Chosen";
  833. _chosenCheckBoxColumn.DataPropertyName = "Chosen";
  834. _chosenCheckBoxColumn.FalseValue = "false";
  835. _chosenCheckBoxColumn.TrueValue = "true";
  836. _dataGridView.Columns.Add (_chosenCheckBoxColumn);
  837. // .NET requires that all possible values for combo-boxes in a column
  838. // are added to the column.
  839. _nameComboBoxColumn.Items.Add ("de Icaza");
  840. _nameComboBoxColumn.Items.Add ("Toshok");
  841. _nameComboBoxColumn.Items.Add ("Harper");
  842. _nameComboBoxColumn.Items.Add ("Boswell");
  843. // Set up the contents of the data-grid.
  844. BindingList<EcstRecord> boundData = new BindingList<EcstRecord> ();
  845. boundData.Add (new EcstRecord ("de Icaza", "Miguel", true));
  846. boundData.Add (new EcstRecord ("Toshok", "Chris", false));
  847. boundData.Add (new EcstRecord ("Harper", "Jackson", false));
  848. boundData.Add (new EcstRecord ("Boswell", "Steven", true));
  849. _dataGridView.DataSource = boundData;
  850. // For data binding to work, there needs to be a Form, apparently.
  851. Form form = new Form ();
  852. form.ShowInTaskbar = false;
  853. form.Controls.Add (_dataGridView);
  854. form.Show ();
  855. // Make sure the data-source took.
  856. // (Without the Form, instead of having four rows, the data grid
  857. // only has one row, and all its cell values are null.)
  858. Assert.AreEqual (boundData.Count, _dataGridView.Rows.Count, "1-6");
  859. // Edit a combo-box cell.
  860. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[0];
  861. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[0].Selected, "1-7");
  862. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-8");
  863. _dataGridView.CancelEdit();
  864. // Edit a text-box cell.
  865. _dataGridView.CurrentCell = _dataGridView.Rows[0].Cells[1];
  866. Assert.AreEqual (false, _dataGridView.Rows[3].Cells[0].Selected, "1-9");
  867. Assert.AreEqual (true, _dataGridView.Rows[0].Cells[1].Selected, "1-10");
  868. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-11");
  869. _dataGridView.CancelEdit();
  870. // Edit a check-box cell.
  871. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[2];
  872. Assert.AreEqual (false, _dataGridView.Rows[0].Cells[1].Selected, "1-12");
  873. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[2].Selected, "1-13");
  874. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-14");
  875. _dataGridView.CancelEdit();
  876. // Make sure the event-handler was called each time.
  877. // (DataGridViewCheckBoxCell isn't derived from Control, so the
  878. // EditingControlShowing event doesn't get called for it.)
  879. Assert.AreEqual (3, editingControlShowingTest_FoundColumns, "1-14");
  880. // Get rid of the form.
  881. form.Close();
  882. }
  883. }
  884. [Test]
  885. public void bug_81918 ()
  886. {
  887. using (DataGridView dgv = new DataGridView ()) {
  888. DataGridViewColumn col = new DataGridViewComboBoxColumn ();
  889. dgv.Columns.Add (col);
  890. dgv.Rows.Add ("a");
  891. DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell) dgv [0, 0];
  892. }
  893. }
  894. // A custom data-grid-view, created solely so that
  895. // mouse clicks can be faked on it.
  896. private class ClickableDataGridView : DataGridView
  897. {
  898. public ClickableDataGridView ()
  899. : base ()
  900. {
  901. }
  902. internal void OnMouseDownInternal (MouseEventArgs e)
  903. {
  904. OnMouseDown (e);
  905. }
  906. internal void OnMouseUpInternal (MouseEventArgs e)
  907. {
  908. OnMouseUp (e);
  909. }
  910. };
  911. [Test]
  912. public void OneClickComboBoxCell ()
  913. {
  914. Form form = null;
  915. try
  916. {
  917. // Create a form, a text label, and a data-grid-view.
  918. form = new Form ();
  919. Label label = new Label ();
  920. label.Text = "Label";
  921. label.Parent = form;
  922. ClickableDataGridView dgv = new ClickableDataGridView ();
  923. dgv.Parent = form;
  924. // Create a combo-box column.
  925. DataGridViewComboBoxColumn cbCol = new DataGridViewComboBoxColumn ();
  926. cbCol.HeaderText = "Name";
  927. dgv.Columns.Add (cbCol);
  928. // .NET requires that all possible values for combo-boxes
  929. // in a column are added to the column.
  930. cbCol.Items.Add ("Item1");
  931. cbCol.Items.Add ("Item2");
  932. cbCol.Items.Add ("Item3");
  933. cbCol.Items.Add ("Item4");
  934. // Set up the contents of the data-grid.
  935. dgv.Rows.Add ("Item1");
  936. dgv.Rows.Add ("Item2");
  937. // Select the cell.
  938. dgv.CurrentCell = dgv.Rows[0].Cells[0];
  939. // Focus the data-grid-view. (Without this, its Leave
  940. // event won't get called when something outside of the
  941. // data-grid-view gets focused.)
  942. dgv.Focus ();
  943. // Show the form, let it draw.
  944. form.Show ();
  945. Application.DoEvents ();
  946. // Locate the drop-down button. (This code is taken from mono-winforms,
  947. // from the private method DataGridViewComboBoxCell.CalculateButtonArea(),
  948. // and was then hacked mercilessly.)
  949. Rectangle button_area = Rectangle.Empty;
  950. {
  951. int border = 3 /* ThemeEngine.Current.Border3DSize.Width */;
  952. const int button_width = 16;
  953. Rectangle text_area = dgv.GetCellDisplayRectangle (0, 0, false);
  954. button_area.X = text_area.Right - button_width - border;
  955. button_area.Y = text_area.Y + border;
  956. button_area.Width = button_width;
  957. button_area.Height = text_area.Height - 2 * border;
  958. }
  959. // Click on the drop-down button.
  960. int x = button_area.X + (button_area.Width / 2);
  961. int y = button_area.Y + (button_area.Height / 2);
  962. if (Environment.OSVersion.Platform == PlatformID.Win32NT
  963. && Type.GetType ("Mono.Runtime") == null)
  964. {
  965. // Calling OnMouseDownInternal () in Win32 doesn't work.
  966. // My best guess as to why is that the WinForms ComboBox
  967. // is a wrapper around the ComCtl control, e.g. similar
  968. // to the reason that Paint event-handlers don't work on
  969. // TreeView. So we go through all this rigamarole to
  970. // simulate a mouse click.
  971. // First, get the location of the desired mouse-click, in
  972. // data-grid-view coordinates.
  973. Win32Point ptGlobal = new Win32Point ();
  974. ptGlobal.x = x + dgv.Location.X;
  975. ptGlobal.y = y + dgv.Location.Y;
  976. // Convert that to screen coordinates.
  977. ClientToScreen (form.Handle, ref ptGlobal);
  978. // Move the mouse-pointer there. (Yes, this really appears
  979. // to be necessary.)
  980. SetCursorPos (ptGlobal.x, ptGlobal.y);
  981. // Convert screen coordinates to mouse coordinates.
  982. ptGlobal.x *= (65535 / SystemInformation.VirtualScreen.Width);
  983. ptGlobal.y *= (65535 / SystemInformation.VirtualScreen.Height);
  984. // Finally, fire a mouse-down and mouse-up event.
  985. mouse_event (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE,
  986. ptGlobal.x, ptGlobal.y, 0, 0);
  987. mouse_event (MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE,
  988. ptGlobal.x, ptGlobal.y, 0, 0);
  989. // Let the system process these events.
  990. Application.DoEvents ();
  991. }
  992. else
  993. {
  994. // And this is how the same code is done under Linux.
  995. // (No one should wonder why I prefer Mono to MS Windows .NET ;-)
  996. MouseEventArgs me = new MouseEventArgs (MouseButtons.Left, 1, x, y, 0);
  997. DataGridViewCellMouseEventArgs cme = new DataGridViewCellMouseEventArgs (0, 0, x, y, me);
  998. dgv.OnMouseDownInternal (cme);
  999. dgv.OnMouseUpInternal (cme);
  1000. }
  1001. // Make sure that created an editing control.
  1002. ComboBox cb = dgv.EditingControl as ComboBox;
  1003. Assert.AreNotEqual (null, cb, "1-1");
  1004. // Make sure that dropped down the menu.
  1005. Assert.AreEqual (true, cb.DroppedDown, "1-2");
  1006. // Close the menu.
  1007. cb.DroppedDown = false;
  1008. // Change the selection on the menu.
  1009. cb.SelectedIndex = 2 /* "Item3" */;
  1010. // Leave the data-grid-view.
  1011. label.Focus ();
  1012. // That should have ended editing and saved the value.
  1013. string cellValue = (string)(dgv.Rows[0].Cells[0].FormattedValue);
  1014. Assert.AreEqual ("Item3", cellValue, "1-3");
  1015. }
  1016. finally
  1017. {
  1018. if (form != null)
  1019. form.Close ();
  1020. }
  1021. }
  1022. // For testing row/column selection.
  1023. List<List<int>> selections;
  1024. void DataGridView_RowSelectionChanged (object sender, EventArgs e)
  1025. {
  1026. // Make a list of selected rows.
  1027. DataGridView dgv = sender as DataGridView;
  1028. List<int> selection = new List<int> ();
  1029. foreach (DataGridViewRow row in dgv.SelectedRows)
  1030. selection.Add (row.Index);
  1031. selections.Add (selection);
  1032. }
  1033. void DataGridView_ColumnSelectionChanged (object sender, EventArgs e)
  1034. {
  1035. // Make a list of selected columns.
  1036. DataGridView dgv = sender as DataGridView;
  1037. List<int> selection = new List<int> ();
  1038. foreach (DataGridViewColumn column in dgv.SelectedColumns)
  1039. selection.Add (column.Index);
  1040. selections.Add (selection);
  1041. }
  1042. // Used to generate printable representation of selections.
  1043. string ListListIntToString (List<List<int>> selections)
  1044. {
  1045. List<string> selectionsList = new List<string> ();
  1046. foreach (List<int> selection in selections)
  1047. {
  1048. List<string> selectionList = new List<string> ();
  1049. foreach (int selectionNo in selection)
  1050. selectionList.Add (selectionNo.ToString ("D"));
  1051. selectionsList.Add ("<" + string.Join (",", selectionList.ToArray()) + ">");
  1052. }
  1053. return string.Join (",", selectionsList.ToArray());
  1054. // (Here is the disallowed Linq version.)
  1055. /* return string.Join (",", (selections.Select ((List<int> x)
  1056. => "<" + string.Join (",", (x.Select ((int y)
  1057. => (y.ToString("D")))).ToArray()) + ">").ToArray())); */
  1058. }
  1059. [Test]
  1060. public void SelectedRowsTest ()
  1061. {
  1062. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1063. dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  1064. // Prepare to test the SelectionChanged event.
  1065. selections = new List<List<int>> ();
  1066. List<List<int>> expectedSelections = new List<List<int>> ();
  1067. dgv.SelectionChanged += new EventHandler (DataGridView_RowSelectionChanged);
  1068. // Make sure there's no selection to begin with.
  1069. Assert.AreEqual (0, dgv.SelectedRows.Count, "1-10");
  1070. // Select a row.
  1071. dgv.Rows [1].Selected = true;
  1072. Assert.AreEqual (1, dgv.SelectedRows.Count, "1-1");
  1073. Assert.AreEqual (1, dgv.SelectedRows [0].Index, "1-2");
  1074. expectedSelections.Add (new List<int> { 1 });
  1075. // Select another row.
  1076. dgv.Rows [3].Selected = true;
  1077. Assert.AreEqual (2, dgv.SelectedRows.Count, "1-3");
  1078. Assert.AreEqual (3, dgv.SelectedRows [0].Index, "1-4");
  1079. Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-5");
  1080. expectedSelections.Add (new List<int> { 3, 1 });
  1081. // Select another row.
  1082. dgv.Rows [2].Selected = true;
  1083. Assert.AreEqual (3, dgv.SelectedRows.Count, "1-6");
  1084. Assert.AreEqual (2, dgv.SelectedRows [0].Index, "1-7");
  1085. Assert.AreEqual (3, dgv.SelectedRows [1].Index, "1-8");
  1086. Assert.AreEqual (1, dgv.SelectedRows [2].Index, "1-9");
  1087. expectedSelections.Add (new List<int> { 2, 3, 1 });
  1088. // Unselect a row.
  1089. dgv.Rows [2].Selected = false;
  1090. Assert.AreEqual (2, dgv.SelectedRows.Count, "1-11");
  1091. Assert.AreEqual (3, dgv.SelectedRows [0].Index, "1-12");
  1092. Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-13");
  1093. expectedSelections.Add (new List<int> { 3, 1 });
  1094. // Delete a row.
  1095. // Since the row wasn't selected, it doesn't fire a
  1096. // SelectionChanged event.
  1097. dgv.Rows.RemoveAt (2);
  1098. Assert.AreEqual (2, dgv.SelectedRows.Count, "1-14");
  1099. Assert.AreEqual (2, dgv.SelectedRows [0].Index, "1-16");
  1100. Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-17");
  1101. // Delete a selected row.
  1102. dgv.Rows.RemoveAt (2);
  1103. Assert.AreEqual (1, dgv.SelectedRows.Count, "1-18");
  1104. Assert.AreEqual (1, dgv.SelectedRows [0].Index, "1-19");
  1105. expectedSelections.Add (new List<int> { 1 });
  1106. // Make sure the SelectionChanged event was called when expected.
  1107. string selectionsText = ListListIntToString (selections);
  1108. string expectedSelectionsText = ListListIntToString (expectedSelections);
  1109. Assert.AreEqual (expectedSelectionsText, selectionsText, "1-15");
  1110. }
  1111. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1112. dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
  1113. dgv.Rows [1].Selected = true;
  1114. Assert.AreEqual (0, dgv.SelectedRows.Count, "3-1");
  1115. dgv.Rows [3].Selected = true;
  1116. Assert.AreEqual (0, dgv.SelectedRows.Count, "3-3");
  1117. dgv.Rows [2].Selected = true;
  1118. Assert.AreEqual (0, dgv.SelectedRows.Count, "3-6");
  1119. }
  1120. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1121. foreach (DataGridViewColumn col in dgv.Columns)
  1122. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1123. dgv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
  1124. dgv.Rows [1].Selected = true;
  1125. Assert.AreEqual (0, dgv.SelectedRows.Count, "4-1");
  1126. dgv.Rows [3].Selected = true;
  1127. Assert.AreEqual (0, dgv.SelectedRows.Count, "4-3");
  1128. dgv.Rows [2].Selected = true;
  1129. Assert.AreEqual (0, dgv.SelectedRows.Count, "4-6");
  1130. }
  1131. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1132. foreach (DataGridViewColumn col in dgv.Columns)
  1133. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1134. dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
  1135. dgv.Rows [1].Selected = true;
  1136. Assert.AreEqual (0, dgv.SelectedRows.Count, "5-1");
  1137. dgv.Rows [3].Selected = true;
  1138. Assert.AreEqual (0, dgv.SelectedRows.Count, "5-3");
  1139. dgv.Rows [2].Selected = true;
  1140. Assert.AreEqual (0, dgv.SelectedRows.Count, "5-6");
  1141. }
  1142. }
  1143. [Test] // bug #325979
  1144. public void SelectedRows_FindColumnByName ()
  1145. {
  1146. DataTable dt = new DataTable ();
  1147. dt.Columns.Add ("Date", typeof (DateTime));
  1148. dt.Columns.Add ("Registered", typeof (bool));
  1149. dt.Columns.Add ("Event", typeof (string));
  1150. DataRow row = dt.NewRow ();
  1151. row ["Date"] = new DateTime (2007, 2, 3);
  1152. row ["Event"] = "one";
  1153. row ["Registered"] = false;
  1154. dt.Rows.Add (row);
  1155. row = dt.NewRow ();
  1156. row ["Date"] = new DateTime (2008, 3, 4);
  1157. row ["Event"] = "two";
  1158. row ["Registered"] = true;
  1159. dt.Rows.Add (row);
  1160. DataGridView dgv = new DataGridView ();
  1161. dgv.DataSource = dt;
  1162. Form form = new Form ();
  1163. form.ShowInTaskbar = false;
  1164. form.Controls.Add (dgv);
  1165. form.Show ();
  1166. dgv.Rows [1].Selected = true;
  1167. DataGridViewCell cell = dgv.SelectedRows [0].Cells ["DaTE"];
  1168. Assert.IsNotNull (cell, "#A1");
  1169. Assert.IsNotNull (cell.OwningColumn, "#A2");
  1170. Assert.AreEqual ("Date", cell.OwningColumn.Name, "#A3");
  1171. Assert.IsNotNull (cell.Value, "#A4");
  1172. Assert.AreEqual (new DateTime (2008, 3, 4), cell.Value, "#A5");
  1173. cell = dgv.SelectedRows [0].Cells ["Event"];
  1174. Assert.IsNotNull (cell, "#B1");
  1175. Assert.IsNotNull (cell.OwningColumn, "#B2");
  1176. Assert.AreEqual ("Event", cell.OwningColumn.Name, "#B3");
  1177. Assert.IsNotNull (cell.Value, "#B3");
  1178. Assert.AreEqual ("two", cell.Value, "#B4");
  1179. form.Dispose ();
  1180. }
  1181. [Test]
  1182. public void SelectedColumnsTest ()
  1183. {
  1184. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1185. foreach (DataGridViewColumn col in dgv.Columns)
  1186. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1187. dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
  1188. // Prepare to test the SelectionChanged event.
  1189. selections = new List<List<int>> ();
  1190. List<List<int>> expectedSelections = new List<List<int>> ();
  1191. dgv.SelectionChanged += new EventHandler (DataGridView_ColumnSelectionChanged);
  1192. // Make sure there's no selection to begin with.
  1193. Assert.AreEqual (0, dgv.SelectedColumns.Count, "1-13");
  1194. // Select a column.
  1195. dgv.Columns [1].Selected = true;
  1196. Assert.AreEqual (1, dgv.SelectedColumns.Count, "1-1");
  1197. Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "1-2");
  1198. expectedSelections.Add (new List<int> { 1 });
  1199. // Select another column.
  1200. dgv.Columns [3].Selected = true;
  1201. Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-3");
  1202. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "1-4");
  1203. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-5");
  1204. expectedSelections.Add (new List<int> { 3, 1 });
  1205. // Select another column.
  1206. dgv.Columns [2].Selected = true;
  1207. Assert.AreEqual (3, dgv.SelectedColumns.Count, "1-6");
  1208. Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "1-7");
  1209. Assert.AreEqual (3, dgv.SelectedColumns [1].Index, "1-8");
  1210. Assert.AreEqual (1, dgv.SelectedColumns [2].Index, "1-9");
  1211. expectedSelections.Add (new List<int> { 2, 3, 1 });
  1212. // Unselect a column.
  1213. dgv.Columns [2].Selected = false;
  1214. Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-10");
  1215. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "1-11");
  1216. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-12");
  1217. expectedSelections.Add (new List<int> { 3, 1 });
  1218. // Delete a column.
  1219. // Since the column wasn't selected, it doesn't fire a
  1220. // SelectionChanged event.
  1221. dgv.Columns.RemoveAt (2);
  1222. Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-14");
  1223. Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "1-16");
  1224. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-17");
  1225. // Delete a selected column.
  1226. dgv.Columns.RemoveAt (2);
  1227. Assert.AreEqual (1, dgv.SelectedColumns.Count, "1-18");
  1228. Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "1-19");
  1229. expectedSelections.Add (new List<int> { 1 });
  1230. // Make sure the SelectionChanged event was called when expected.
  1231. string selectionsText = ListListIntToString (selections);
  1232. string expectedSelectionsText = ListListIntToString (expectedSelections);
  1233. Assert.AreEqual (expectedSelectionsText, selectionsText, "1-15");
  1234. }
  1235. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1236. foreach (DataGridViewColumn col in dgv.Columns)
  1237. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1238. dgv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
  1239. // Prepare to test the SelectionChanged event.
  1240. selections = new List<List<int>> ();
  1241. List<List<int>> expectedSelections = new List<List<int>> ();
  1242. dgv.SelectionChanged += new EventHandler (DataGridView_ColumnSelectionChanged);
  1243. // Make sure there's no selection to begin with.
  1244. Assert.AreEqual (0, dgv.SelectedColumns.Count, "2-10");
  1245. // Select a column.
  1246. dgv.Columns [1].Selected = true;
  1247. Assert.AreEqual (1, dgv.SelectedColumns.Count, "2-1");
  1248. Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "2-2");
  1249. expectedSelections.Add (new List<int> { 1 });
  1250. // Select another column.
  1251. dgv.Columns [3].Selected = true;
  1252. Assert.AreEqual (2, dgv.SelectedColumns.Count, "2-3");
  1253. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "2-4");
  1254. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "2-5");
  1255. expectedSelections.Add (new List<int> { 3, 1 });
  1256. // Select another column.
  1257. dgv.Columns [2].Selected = true;
  1258. Assert.AreEqual (3, dgv.SelectedColumns.Count, "2-6");
  1259. Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "2-7");
  1260. Assert.AreEqual (3, dgv.SelectedColumns [1].Index, "2-8");
  1261. Assert.AreEqual (1, dgv.SelectedColumns [2].Index, "2-9");
  1262. expectedSelections.Add (new List<int> { 2, 3, 1 });
  1263. // Unselect another column.
  1264. dgv.Columns [2].Selected = false;
  1265. Assert.AreEqual (2, dgv.SelectedColumns.Count, "2-11");
  1266. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "2-12");
  1267. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "2-13");
  1268. expectedSelections.Add (new List<int> { 3, 1 });
  1269. // Make sure the SelectionChanged event was called when expected.
  1270. string selectionsText = ListListIntToString (selections);
  1271. string expectedSelectionsText = ListListIntToString (expectedSelections);
  1272. Assert.AreEqual (expectedSelectionsText, selectionsText, "2-14");
  1273. }
  1274. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1275. dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
  1276. dgv.Columns [1].Selected = true;
  1277. Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-1");
  1278. dgv.Columns [3].Selected = true;
  1279. Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-3");
  1280. dgv.Columns [2].Selected = true;
  1281. Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-6");
  1282. }
  1283. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1284. foreach (DataGridViewColumn col in dgv.Columns)
  1285. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1286. dgv.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
  1287. dgv.Columns [1].Selected = true;
  1288. Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-1");
  1289. dgv.Columns [3].Selected = true;
  1290. Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-3");
  1291. dgv.Columns [2].Selected = true;
  1292. Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-6");
  1293. }
  1294. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1295. foreach (DataGridViewColumn col in dgv.Columns)
  1296. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1297. dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  1298. dgv.Columns [1].Selected = true;
  1299. Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-1");
  1300. dgv.Columns [3].Selected = true;
  1301. Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-3");
  1302. dgv.Columns [2].Selected = true;
  1303. Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-6");
  1304. }
  1305. }
  1306. [Test]
  1307. public void TopLeftHeaderCellTest ()
  1308. {
  1309. Assert.Ignore("Missing quite a few bits still");
  1310. using (DataGridView dgv = new DataGridView ()) {
  1311. DataGridViewHeaderCell cell = dgv.TopLeftHeaderCell;
  1312. cell = dgv.TopLeftHeaderCell;
  1313. Assert.IsNotNull (cell, "#01");
  1314. Assert.AreEqual (cell.DataGridView, dgv, "#02");
  1315. Assert.AreEqual ("DataGridViewTopLeftHeaderCell", cell.GetType ().Name, "#03");
  1316. Assert.IsNotNull (cell.AccessibilityObject, "#cell.AccessibilityObject");
  1317. Assert.AreEqual (-1, cell.ColumnIndex, "#cell.ColumnIndex");
  1318. // /* NIE for the moment... */ Assert.IsNotNull (cell.ContentBounds, "#cell.ContentBounds");
  1319. Assert.IsNull (cell.ContextMenuStrip, "#cell.ContextMenuStrip");
  1320. Assert.IsNotNull (cell.DataGridView, "#cell.DataGridView");
  1321. Assert.IsNull (cell.DefaultNewRowValue, "#cell.DefaultNewRowValue");
  1322. Assert.AreEqual (false, cell.Displayed, "#cell.Displayed");
  1323. // /* NIE for the moment... */ Assert.AreEqual (@"", cell.EditedFormattedValue, "#cell.EditedFormattedValue");
  1324. Assert.IsNotNull (cell.EditType, "#cell.EditType");
  1325. Assert.IsNotNull (cell.ErrorIconBounds, "#cell.ErrorIconBounds");
  1326. Assert.AreEqual (@"", cell.ErrorText, "#cell.ErrorText");
  1327. // /* NIE for the moment... */ Assert.AreEqual (@"", cell.FormattedValue, "#cell.FormattedValue");
  1328. Assert.IsNotNull (cell.FormattedValueType, "#cell.FormattedValueType");
  1329. // /* NIE for the moment... */ Assert.AreEqual (true, cell.Frozen, "#cell.Frozen");
  1330. Assert.AreEqual (false, cell.HasStyle, "#cell.HasStyle");
  1331. Assert.AreEqual (DataGridViewElementStates.Frozen | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Resizable | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Visible, cell.InheritedState, "#cell.InheritedState");
  1332. Assert.IsNotNull (cell.InheritedStyle, "#cell.InheritedStyle");
  1333. try {
  1334. object zxf = cell.IsInEditMode;
  1335. TestHelper.RemoveWarning (zxf);
  1336. Assert.Fail ("Expected 'System.InvalidOperationException', but no exception was thrown.", "#cell.IsInEditMode");
  1337. } catch (InvalidOperationException ex) {
  1338. Assert.AreEqual (@"Operation cannot be performed on a cell of a shared row.", ex.Message);
  1339. } catch (Exception ex) {
  1340. Assert.Fail ("Expected 'System.InvalidOperationException', got '" + ex.GetType ().FullName + "'.", "#cell.IsInEditMode");
  1341. }
  1342. Assert.IsNull (cell.OwningColumn, "#cell.OwningColumn");
  1343. Assert.IsNull (cell.OwningRow, "#cell.OwningRow");
  1344. Assert.IsNotNull (cell.PreferredSize, "#cell.PreferredSize");
  1345. Assert.AreEqual (true, cell.ReadOnly, "#cell.ReadOnly");
  1346. Assert.AreEqual (true, cell.Resizable, "#cell.Resizable");
  1347. Assert.AreEqual (-1, cell.RowIndex, "#cell.RowIndex");
  1348. Assert.AreEqual (false, cell.Selected, "#cell.Selected");
  1349. Assert.IsNotNull (cell.Size, "#cell.Size");
  1350. Assert.AreEqual (DataGridViewElementStates.None, cell.State, "#cell.State");
  1351. if (cell.HasStyle)
  1352. Assert.IsNotNull (cell.Style, "#cell.Style");
  1353. Assert.IsNull (cell.Tag, "#cell.Tag");
  1354. Assert.AreEqual (@"", cell.ToolTipText, "#cell.ToolTipText");
  1355. Assert.IsNull (cell.Value, "#cell.Value");
  1356. Assert.IsNotNull (cell.ValueType, "#cell.ValueType");
  1357. Assert.AreEqual (true, cell.Visible, "#cell.Visible");
  1358. }
  1359. }
  1360. [Test]
  1361. public void TestDefaultValues ()
  1362. {
  1363. DataGridView grid = new DataGridView ();
  1364. Assert.AreEqual (true, grid.AllowUserToAddRows, "#A1");
  1365. Assert.AreEqual (true, grid.AllowUserToDeleteRows, "#A2");
  1366. Assert.AreEqual (false, grid.AllowUserToOrderColumns, "#A3");
  1367. Assert.AreEqual (true, grid.AllowUserToResizeColumns, "#A4");
  1368. Assert.AreEqual (true, grid.AllowUserToResizeRows, "#A5");
  1369. Assert.AreEqual (new DataGridViewCellStyle(), grid.AlternatingRowsDefaultCellStyle, "#A6");
  1370. Assert.AreEqual (true, grid.AutoGenerateColumns, "#A7");
  1371. Assert.AreEqual (DataGridViewAutoSizeRowsMode.None, grid.AutoSizeRowsMode, "#A8");
  1372. Assert.AreEqual (Control.DefaultBackColor, grid.BackColor, "#A9");
  1373. Assert.AreEqual (SystemColors.AppWorkspace, grid.BackgroundColor, "#A10");
  1374. Assert.AreEqual (BorderStyle.FixedSingle, grid.BorderStyle, "#A11");
  1375. Assert.AreEqual (DataGridViewClipboardCopyMode.EnableWithAutoHeaderText, grid.ClipboardCopyMode, "#A12");
  1376. Assert.AreEqual (DataGridViewColumnHeadersHeightSizeMode.EnableResizing, grid.ColumnHeadersHeightSizeMode, "#A21");
  1377. Assert.AreEqual (true, grid.ColumnHeadersVisible, "#A22");
  1378. Assert.AreEqual (String.Empty, grid.DataMember, "#A23");
  1379. Assert.AreEqual (DataGridViewEditMode.EditOnKeystrokeOrF2, grid.EditMode, "#A31");
  1380. Assert.AreEqual (Control.DefaultFont, grid.Font, "#A32");
  1381. Assert.AreEqual (Control.DefaultForeColor, grid.ForeColor, "#A33");
  1382. Assert.AreEqual (Color.FromKnownColor(KnownColor.ControlDark), grid.GridColor, "#A34");
  1383. Assert.AreEqual (true, grid.MultiSelect, "#A35");
  1384. Assert.AreEqual (grid.Rows.Count - 1, grid.NewRowIndex, "#A36");
  1385. Assert.AreEqual (Padding.Empty, grid.Padding, "#A37");
  1386. Assert.AreEqual (false, grid.ReadOnly, "#A38");
  1387. Assert.AreEqual (true, grid.RowHeadersVisible, "#A39");
  1388. Assert.AreEqual (41, grid.RowHeadersWidth, "#A40");
  1389. Assert.AreEqual (DataGridViewSelectionMode.RowHeaderSelect, grid.SelectionMode, "#A41");
  1390. Assert.AreEqual (true, grid.ShowCellErrors, "#A42");
  1391. Assert.AreEqual (true, grid.ShowEditingIcon, "#A43");
  1392. Assert.AreEqual (Cursors.Default, grid.UserSetCursor, "#A44");
  1393. Assert.AreEqual (false, grid.VirtualMode, "#A45");
  1394. }
  1395. #region AutoSizeColumnsModeExceptions
  1396. [Test]
  1397. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1398. public void TestAutoSizeColumnsModeInvalidEnumArgumentException ()
  1399. {
  1400. DataGridView grid = new DataGridView();
  1401. grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill | DataGridViewAutoSizeColumnsMode.None;
  1402. }
  1403. [Test]
  1404. [ExpectedException (typeof (InvalidOperationException))]
  1405. public void TestAutoSizeColumnsModeInvalidOperationException1 ()
  1406. {
  1407. DataGridView grid = new DataGridView ();
  1408. grid.ColumnHeadersVisible = false;
  1409. DataGridViewColumn col = new DataGridViewColumn ();
  1410. col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
  1411. grid.Columns.Add (col);
  1412. grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
  1413. }
  1414. [Test]
  1415. [ExpectedException (typeof (InvalidOperationException))]
  1416. public void TestAutoSizeColumnsModeInvalidOperationException2 ()
  1417. {
  1418. DataGridView grid = new DataGridView ();
  1419. DataGridViewColumn col = new DataGridViewColumn ();
  1420. col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
  1421. col.Frozen = true;
  1422. grid.Columns.Add (col);
  1423. grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  1424. }
  1425. #endregion
  1426. #region AutoSizeRowsModeExceptions
  1427. [Test]
  1428. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1429. public void TestAutoSizeRowsModeInvalidEnumArgumentException ()
  1430. {
  1431. DataGridView grid = new DataGridView ();
  1432. grid.AutoSizeRowsMode = (DataGridViewAutoSizeRowsMode) 4;
  1433. }
  1434. [Test]
  1435. [ExpectedException (typeof (InvalidOperationException))]
  1436. public void TestAutoSizeRowsModeInvalidOperationException1 ()
  1437. {
  1438. DataGridView grid = new DataGridView ();
  1439. grid.RowHeadersVisible = false;
  1440. grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllHeaders;
  1441. }
  1442. [Test]
  1443. [ExpectedException (typeof (InvalidOperationException))]
  1444. public void TestAutoSizeRowsModeInvalidOperationException2 ()
  1445. {
  1446. DataGridView grid = new DataGridView ();
  1447. grid.RowHeadersVisible = false;
  1448. grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedHeaders;
  1449. }
  1450. #endregion
  1451. [Test]
  1452. public void AutoResizeColumTest ()
  1453. {
  1454. using (Form f = new Form ()) {
  1455. f.Show ();
  1456. using (DataGridView dgv = new DataGridView ()) {
  1457. f.Controls.Add (dgv);
  1458. DataGridViewColumn col, col2, col3;
  1459. Assert.AreEqual ("{Width=240, Height=150}", dgv.ClientSize.ToString (), "#01");
  1460. col = new DataGridViewColumn ();
  1461. col.MinimumWidth = 20;
  1462. col.FillWeight = 20;
  1463. col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  1464. col.CellTemplate = new DataGridViewTextBoxCell ();
  1465. Assert.AreEqual (100, col.Width, "#02");
  1466. dgv.Columns.Add (col);
  1467. Assert.AreEqual (197, col.Width, "#03");
  1468. col2 = new DataGridViewColumn ();
  1469. col2.MinimumWidth = 20;
  1470. col2.FillWeight = 40;
  1471. col2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  1472. col2.CellTemplate = new DataGridViewTextBoxCell (); ;
  1473. dgv.Columns.Add (col2);
  1474. Assert.AreEqual (66, col.Width, "#04");
  1475. Assert.AreEqual (131, col2.Width, "#05");
  1476. col3 = new DataGridViewColumn ();
  1477. col3.MinimumWidth = 20;
  1478. col3.FillWeight = 5;
  1479. col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  1480. col3.CellTemplate = new DataGridViewTextBoxCell (); ;
  1481. dgv.Columns.Add (col3);
  1482. Assert.AreEqual (59, col.Width, "#04");
  1483. Assert.AreEqual (118, col2.Width, "#05");
  1484. Assert.AreEqual (20, col3.Width, "#05");
  1485. }
  1486. }
  1487. }
  1488. [Test]
  1489. public void ControlsTest ()
  1490. {
  1491. using (DataGridView grid = new DataGridView ()) {
  1492. Assert.AreEqual ("DataGridViewControlCollection", grid.Controls.GetType ().Name, "#01");
  1493. Assert.AreEqual (2, grid.Controls.Count, "#02");
  1494. }
  1495. }
  1496. [Test]
  1497. [ExpectedException (typeof (ArgumentException))]
  1498. public void TestBackgroundColorArgumentException ()
  1499. {
  1500. DataGridView grid = new DataGridView ();
  1501. grid.BackgroundColor = Color.Empty;
  1502. }
  1503. [Test]
  1504. [ExpectedException(typeof(InvalidEnumArgumentException))]
  1505. public void TestBorderStyleInvalidEnumArgumentException ()
  1506. {
  1507. DataGridView grid = new DataGridView ();
  1508. grid.BorderStyle = BorderStyle.FixedSingle | BorderStyle.Fixed3D;
  1509. }
  1510. [Test]
  1511. public void ColumnCount ()
  1512. {
  1513. DataGridView dgv = new DataGridView ();
  1514. dgv.RowCount = 10;
  1515. dgv.ColumnCount = 2;
  1516. Assert.AreEqual (10, dgv.RowCount, "A1");
  1517. Assert.AreEqual (2, dgv.ColumnCount, "A2");
  1518. dgv.ColumnCount = 1;
  1519. Assert.AreEqual (10, dgv.RowCount, "B1");
  1520. Assert.AreEqual (1, dgv.ColumnCount, "B2");
  1521. dgv.ColumnCount = 3;
  1522. Assert.AreEqual (10, dgv.RowCount, "C1");
  1523. Assert.AreEqual (3, dgv.ColumnCount, "C2");
  1524. dgv.ColumnCount = 0;
  1525. Assert.AreEqual (0, dgv.RowCount, "D1");
  1526. Assert.AreEqual (0, dgv.ColumnCount, "D2");
  1527. Assert.AreEqual (0, dgv.ColumnCount, "E1");
  1528. try {
  1529. dgv.ColumnCount = -1;
  1530. Assert.Fail ("F1");
  1531. } catch (ArgumentOutOfRangeException ex) {
  1532. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "F2");
  1533. Assert.IsNotNull (ex.Message, "F3");
  1534. Assert.IsNotNull (ex.ParamName, "F4");
  1535. Assert.AreEqual ("ColumnCount", ex.ParamName, "F5");
  1536. Assert.IsNull (ex.InnerException, "F6");
  1537. }
  1538. }
  1539. [Test]
  1540. public void ColumnCountIncrease ()
  1541. {
  1542. DataGridView dgv = new DataGridView ();
  1543. dgv.ColumnCount = 1;
  1544. // Increasing the ColumnCount adds TextBoxColumns, not generic columns
  1545. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[0].GetType ().ToString (), "A1");
  1546. }
  1547. [Test]
  1548. public void ColumnCountDecrease ()
  1549. {
  1550. DataGridView dgv = new DataGridView ();
  1551. dgv.ColumnCount = 6;
  1552. Assert.AreEqual (6, dgv.ColumnCount, "A1");
  1553. dgv.ColumnCount = 3;
  1554. Assert.AreEqual (3, dgv.ColumnCount, "A2");
  1555. // Increasing the ColumnCount adds TextBoxColumns, not generic columns
  1556. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[0].GetType ().ToString (), "A3");
  1557. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[1].GetType ().ToString (), "A4");
  1558. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[2].GetType ().ToString (), "A5");
  1559. }
  1560. private class DataItem
  1561. {
  1562. public string Text {
  1563. get { return String.Empty; }
  1564. }
  1565. [Browsable (false)]
  1566. public string NotVisible {
  1567. get { return String.Empty; }
  1568. }
  1569. }
  1570. [Test]
  1571. public void NoDuplicateAutoGeneratedColumn ()
  1572. {
  1573. List<DataItem> dataList = new List<DataItem> ();
  1574. dataList.Add (new DataItem ());
  1575. dataList.Add (new DataItem ());
  1576. DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn ();
  1577. column.DataPropertyName = "Text";
  1578. column.HeaderText = "Custom Column";
  1579. grid.Columns.Add (column);
  1580. grid.DataSource = dataList;
  1581. // Test that the column autogeneration hasn't generated duplicate column
  1582. // for the property Text
  1583. Assert.AreEqual (1, grid.Columns.Count, "#1");
  1584. Assert.AreEqual ("Custom Column", grid.Columns[0].HeaderText, "#2");
  1585. }
  1586. [Test]
  1587. [ExpectedException (typeof (InvalidOperationException))]
  1588. public void TestColumnCountInvalidOperationException ()
  1589. {
  1590. DataGridView grid = new DataGridView ();
  1591. grid.DataSource = new ArrayList ();
  1592. grid.ColumnCount = 0;
  1593. }
  1594. [Test]
  1595. public void ColumnHeadersHeight ()
  1596. {
  1597. DataGridView grid = new DataGridView ();
  1598. Assert.AreEqual (23, grid.ColumnHeadersHeight, "#A1");
  1599. grid.ColumnHeadersHeight = 4;
  1600. Assert.AreEqual (4, grid.ColumnHeadersHeight, "#A2");
  1601. grid.ColumnHeadersHeight = 32768;
  1602. Assert.AreEqual (32768, grid.ColumnHeadersHeight, "#A3");
  1603. try {
  1604. grid.ColumnHeadersHeight = 3;
  1605. Assert.Fail ("#B1");
  1606. } catch (ArgumentOutOfRangeException ex) {
  1607. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  1608. Assert.IsNotNull (ex.Message, "#B3");
  1609. Assert.IsNotNull (ex.ParamName, "#B4");
  1610. Assert.AreEqual ("ColumnHeadersHeight", ex.ParamName, "#B5");
  1611. Assert.IsNull (ex.InnerException, "#B6");
  1612. }
  1613. try {
  1614. grid.ColumnHeadersHeight = 32769;
  1615. Assert.Fail ("#C1");
  1616. } catch (ArgumentOutOfRangeException ex) {
  1617. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  1618. Assert.IsNotNull (ex.Message, "#C3");
  1619. Assert.IsNotNull (ex.ParamName, "#C4");
  1620. Assert.AreEqual ("ColumnHeadersHeight", ex.ParamName, "#C5");
  1621. Assert.IsNull (ex.InnerException, "#C6");
  1622. }
  1623. }
  1624. [Test]
  1625. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1626. public void TestColumnHeadersHeightSizeModeInvalidEnumArgumentException ()
  1627. {
  1628. DataGridView grid = new DataGridView ();
  1629. grid.ColumnHeadersHeightSizeMode = (DataGridViewColumnHeadersHeightSizeMode) 3;
  1630. }
  1631. [Test]
  1632. public void RowHeadersWidth ()
  1633. {
  1634. DataGridView grid = new DataGridView();
  1635. Assert.AreEqual (41, grid.RowHeadersWidth, "#A1");
  1636. grid.RowHeadersWidth = 4;
  1637. Assert.AreEqual (4, grid.RowHeadersWidth, "#A2");
  1638. grid.RowHeadersWidth = 32768;
  1639. Assert.AreEqual (32768, grid.RowHeadersWidth, "#A3");
  1640. try {
  1641. grid.RowHeadersWidth = 3;
  1642. Assert.Fail ("#B1");
  1643. } catch (ArgumentOutOfRangeException ex) {
  1644. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  1645. Assert.IsNotNull (ex.Message, "#B3");
  1646. Assert.IsNotNull (ex.ParamName, "#B4");
  1647. Assert.AreEqual ("RowHeadersWidth", ex.ParamName, "#B5");
  1648. Assert.IsNull (ex.InnerException, "#B6");
  1649. }
  1650. try {
  1651. grid.RowHeadersWidth = 32769;
  1652. Assert.Fail ("#C1");
  1653. } catch (ArgumentOutOfRangeException ex) {
  1654. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  1655. Assert.IsNotNull (ex.Message, "#C3");
  1656. Assert.IsNotNull (ex.ParamName, "#C4");
  1657. Assert.AreEqual ("RowHeadersWidth", ex.ParamName, "#C5");
  1658. Assert.IsNull (ex.InnerException, "#C6");
  1659. }
  1660. }
  1661. [Test]
  1662. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1663. public void TestDataGridViewRowHeadersWidthSizeModeInvalidEnumArgumentException () {
  1664. DataGridView grid = new DataGridView ();
  1665. grid.RowHeadersWidthSizeMode = (DataGridViewRowHeadersWidthSizeMode) 5;
  1666. }
  1667. [Test]
  1668. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1669. public void TestScrollBarsInvalidEnumArgumentException ()
  1670. {
  1671. DataGridView grid = new DataGridView ();
  1672. grid.ScrollBars = (ScrollBars) 4;
  1673. }
  1674. [Test]
  1675. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1676. public void TestSelectionModeInvalidEnumArgumentException ()
  1677. {
  1678. DataGridView grid = new DataGridView ();
  1679. grid.SelectionMode = (DataGridViewSelectionMode) 5;
  1680. }
  1681. [Test]
  1682. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1683. public void TestAutoResizeRowsInvalidEnumArgumentException ()
  1684. {
  1685. DataGridView grid = new DataGridView ();
  1686. grid.AutoResizeRows ((DataGridViewAutoSizeRowsMode) 4);
  1687. }
  1688. [Test]
  1689. [ExpectedException (typeof (InvalidOperationException))]
  1690. public void TestAutoResizeRowsInvalidOperationException1 ()
  1691. {
  1692. DataGridView grid = new DataGridView ();
  1693. grid.RowHeadersVisible = false;
  1694. grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.AllHeaders);
  1695. }
  1696. [Test]
  1697. [ExpectedException (typeof (InvalidOperationException))]
  1698. public void TestAutoResizeRowsInvalidOperationException2 ()
  1699. {
  1700. DataGridView grid = new DataGridView ();
  1701. grid.RowHeadersVisible = false;
  1702. grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.DisplayedHeaders);
  1703. }
  1704. [Test]
  1705. [ExpectedException (typeof (ArgumentException))]
  1706. public void TestAutoResizeRowsArgumentException ()
  1707. {
  1708. DataGridView grid = new DataGridView ();
  1709. grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.None);
  1710. }
  1711. [Test]
  1712. public void DefaultSize ()
  1713. {
  1714. MockDataGridView grid = new MockDataGridView ();
  1715. Assert.AreEqual (new Size (240, 150), grid.default_size, "#1");
  1716. Assert.AreEqual (new Size (240, 150), grid.Size, "#2");
  1717. }
  1718. [Test]
  1719. public void ColumnHeadersDefaultCellStyle ()
  1720. {
  1721. DataGridView grid = new DataGridView();
  1722. Assert.AreEqual (SystemColors.Control, grid.ColumnHeadersDefaultCellStyle.BackColor, "#A1");
  1723. Assert.AreEqual (SystemColors.WindowText, grid.ColumnHeadersDefaultCellStyle.ForeColor, "#A2");
  1724. Assert.AreEqual (SystemColors.Highlight, grid.ColumnHeadersDefaultCellStyle.SelectionBackColor, "#A3");
  1725. Assert.AreEqual (SystemColors.HighlightText, grid.ColumnHeadersDefaultCellStyle.SelectionForeColor, "#A4");
  1726. Assert.AreSame (grid.Font, grid.ColumnHeadersDefaultCellStyle.Font, "#A5");
  1727. Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.ColumnHeadersDefaultCellStyle.Alignment, "#A6");
  1728. Assert.AreEqual (DataGridViewTriState.True, grid.ColumnHeadersDefaultCellStyle.WrapMode, "#A7");
  1729. }
  1730. [Test]
  1731. public void DefaultCellStyle ()
  1732. {
  1733. DataGridView grid = new DataGridView();
  1734. Assert.AreEqual (SystemColors.Window, grid.DefaultCellStyle.BackColor, "#A1");
  1735. Assert.AreEqual (SystemColors.ControlText, grid.DefaultCellStyle.ForeColor, "#A2");
  1736. Assert.AreEqual (SystemColors.Highlight, grid.DefaultCellStyle.SelectionBackColor, "#A3");
  1737. Assert.AreEqual (SystemColors.HighlightText, grid.DefaultCellStyle.SelectionForeColor, "#A4");
  1738. Assert.AreSame (grid.Font, grid.DefaultCellStyle.Font, "#A5");
  1739. Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.DefaultCellStyle.Alignment, "#A6");
  1740. Assert.AreEqual (DataGridViewTriState.False, grid.DefaultCellStyle.WrapMode, "#A7");
  1741. }
  1742. [Test]
  1743. public void MethodIsInputKey ()
  1744. {
  1745. string result = string.Empty;
  1746. string expected = "13;13;33;33;34;34;35;36;37;38;39;40;46;48;96;113;";
  1747. ExposeProtectedProperties dgv = new ExposeProtectedProperties ();
  1748. foreach (Keys k in Enum.GetValues (typeof (Keys)))
  1749. if (dgv.PublicIsInputKey (k))
  1750. result += ((int)k).ToString () + ";";
  1751. Assert.AreEqual (expected, result, "A1");
  1752. }
  1753. [Test]
  1754. public void MethodIsInputChar ()
  1755. {
  1756. bool result = false;
  1757. ExposeProtectedProperties dgv = new ExposeProtectedProperties ();
  1758. for (int i = 0; i < 255; i++)
  1759. if (!dgv.PublicIsInputChar ((char)i))
  1760. result = true;
  1761. // Basically, it always returns true
  1762. Assert.AreEqual (false, result, "A1");
  1763. }
  1764. [Test]
  1765. public void RowsDefaultCellStyle ()
  1766. {
  1767. DataGridView grid = new DataGridView();
  1768. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.BackColor, "#A1");
  1769. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.ForeColor, "#A2");
  1770. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.SelectionBackColor, "#A3");
  1771. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.SelectionForeColor, "#A4");
  1772. Assert.IsNull(grid.RowsDefaultCellStyle.Font, "#A5");
  1773. Assert.AreEqual (DataGridViewContentAlignment.NotSet, grid.RowsDefaultCellStyle.Alignment, "#A6");
  1774. Assert.AreEqual (DataGridViewTriState.NotSet, grid.RowsDefaultCellStyle.WrapMode, "#A7");
  1775. }
  1776. [Test]
  1777. public void RowHeadersDefaultCellStyle ()
  1778. {
  1779. DataGridView grid = new DataGridView();
  1780. Assert.AreEqual (SystemColors.Control, grid.RowHeadersDefaultCellStyle.BackColor, "#A1");
  1781. Assert.AreEqual (SystemColors.WindowText, grid.RowHeadersDefaultCellStyle.ForeColor, "#A2");
  1782. Assert.AreEqual (SystemColors.Highlight, grid.RowHeadersDefaultCellStyle.SelectionBackColor, "#A3");
  1783. Assert.AreEqual (SystemColors.HighlightText, grid.RowHeadersDefaultCellStyle.SelectionForeColor, "#A4");
  1784. Assert.AreSame (grid.Font, grid.RowHeadersDefaultCellStyle.Font, "#A5");
  1785. Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.RowHeadersDefaultCellStyle.Alignment, "#A6");
  1786. Assert.AreEqual (DataGridViewTriState.True, grid.RowHeadersDefaultCellStyle.WrapMode, "#A7");
  1787. }
  1788. private class MockDataGridView : DataGridView
  1789. {
  1790. public Size default_size {
  1791. get { return base.DefaultSize; }
  1792. }
  1793. }
  1794. [Test]
  1795. public void bug_82326 ()
  1796. {
  1797. using (Form f = new Form ()) {
  1798. DataGridView _dataGrid;
  1799. DataGridViewTextBoxColumn _column;
  1800. _dataGrid = new DataGridView ();
  1801. _column = new DataGridViewTextBoxColumn ();
  1802. f.SuspendLayout ();
  1803. ((ISupportInitialize)(_dataGrid)).BeginInit ();
  1804. //
  1805. // _dataGrid
  1806. //
  1807. _dataGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  1808. _dataGrid.Columns.Add (_column);
  1809. _dataGrid.RowTemplate.Height = 21;
  1810. _dataGrid.Location = new Point (12, 115);
  1811. _dataGrid.Size = new Size (268, 146);
  1812. _dataGrid.TabIndex = 0;
  1813. //
  1814. // _column
  1815. //
  1816. _column.HeaderText = "Column";
  1817. //
  1818. // MainForm
  1819. //
  1820. f.ClientSize = new Size (292, 273);
  1821. f.Controls.Add (_dataGrid);
  1822. ((ISupportInitialize)(_dataGrid)).EndInit ();
  1823. f.ResumeLayout (false);
  1824. f.Load += delegate (object sender, EventArgs e) { ((Control)sender).FindForm ().Close (); };
  1825. Application.Run (f);
  1826. }
  1827. }
  1828. [Test]
  1829. public void RowCountIncrease ()
  1830. {
  1831. DataGridView dgv = new DataGridView ();
  1832. dgv.RowCount = 3;
  1833. Assert.AreEqual (3, dgv.RowCount, "A1");
  1834. Assert.AreEqual (1, dgv.ColumnCount, "A2");
  1835. Assert.AreEqual (0, dgv.Rows[0].Index, "A3");
  1836. Assert.AreEqual (1, dgv.Rows[1].Index, "A4");
  1837. Assert.AreEqual (2, dgv.Rows[2].Index, "A5");
  1838. dgv.RowCount = 2;
  1839. Assert.AreEqual (2, dgv.RowCount, "B1");
  1840. Assert.AreEqual (1, dgv.ColumnCount, "B2");
  1841. Assert.AreEqual (0, dgv.Rows[0].Index, "B3");
  1842. Assert.AreEqual (1, dgv.Rows[1].Index, "B4");
  1843. dgv.RowCount = 6;
  1844. Assert.AreEqual (6, dgv.RowCount, "C1");
  1845. Assert.AreEqual (1, dgv.ColumnCount, "C2");
  1846. Assert.AreEqual (0, dgv.Rows[0].Index, "C3");
  1847. Assert.AreEqual (1, dgv.Rows[1].Index, "C4");
  1848. Assert.AreEqual (2, dgv.Rows[2].Index, "C5");
  1849. dgv.AllowUserToAddRows = false;
  1850. Assert.AreEqual (5, dgv.RowCount, "D1");
  1851. Assert.AreEqual (1, dgv.ColumnCount, "D2");
  1852. dgv.RowCount = 1;
  1853. Assert.AreEqual (1, dgv.RowCount, "E1");
  1854. Assert.AreEqual (1, dgv.ColumnCount, "E2");
  1855. Assert.AreEqual (0, dgv.Rows[0].Index, "E3");
  1856. dgv.RowCount = 8;
  1857. Assert.AreEqual (8, dgv.RowCount, "F1");
  1858. Assert.AreEqual (1, dgv.ColumnCount, "F2");
  1859. Assert.AreEqual (0, dgv.Rows[0].Index, "F3");
  1860. Assert.AreEqual (1, dgv.Rows[1].Index, "F4");
  1861. }
  1862. [Test]
  1863. public void RowCountDecrease ()
  1864. {
  1865. DataGridView dgv = new DataGridView ();
  1866. dgv.RowCount = 6;
  1867. Assert.AreEqual (6, dgv.RowCount, "A1");
  1868. Assert.AreEqual (1, dgv.ColumnCount, "A2");
  1869. dgv.RowCount = 3;
  1870. Assert.AreEqual (3, dgv.RowCount, "A3");
  1871. Assert.AreEqual (0, dgv.Rows[0].Index, "A4");
  1872. Assert.AreEqual (1, dgv.Rows[1].Index, "A5");
  1873. Assert.AreEqual (2, dgv.Rows[2].Index, "A6");
  1874. try {
  1875. dgv.RowCount = 0;
  1876. Assert.Fail ("C1");
  1877. } catch {}
  1878. dgv.RowCount = 6;
  1879. Assert.AreEqual (6, dgv.RowCount, "B1");
  1880. Assert.AreEqual (1, dgv.ColumnCount, "B2");
  1881. Assert.AreEqual (0, dgv.Rows[0].Index, "B3");
  1882. Assert.AreEqual (1, dgv.Rows[1].Index, "B4");
  1883. Assert.AreEqual (2, dgv.Rows[2].Index, "B5");
  1884. dgv.RowCount = 2;
  1885. Assert.AreEqual (2, dgv.RowCount, "C1");
  1886. Assert.AreEqual (1, dgv.ColumnCount, "C2");
  1887. Assert.AreEqual (0, dgv.Rows[0].Index, "C3");
  1888. Assert.AreEqual (1, dgv.Rows[1].Index, "C4");
  1889. dgv.AllowUserToAddRows = false;
  1890. Assert.AreEqual (1, dgv.RowCount, "D1");
  1891. Assert.AreEqual (1, dgv.ColumnCount, "D2");
  1892. Assert.AreEqual (0, dgv.Rows[0].Index, "D3");
  1893. dgv.RowCount = 6;
  1894. Assert.AreEqual (6, dgv.RowCount, "E1");
  1895. Assert.AreEqual (1, dgv.ColumnCount, "E2");
  1896. Assert.AreEqual (0, dgv.Rows[0].Index, "E3");
  1897. Assert.AreEqual (1, dgv.Rows[1].Index, "E4");
  1898. Assert.AreEqual (2, dgv.Rows[2].Index, "E5");
  1899. dgv.RowCount = 2;
  1900. Assert.AreEqual (2, dgv.RowCount, "F1");
  1901. Assert.AreEqual (1, dgv.ColumnCount, "F2");
  1902. Assert.AreEqual (0, dgv.Rows[0].Index, "F3");
  1903. Assert.AreEqual (1, dgv.Rows[1].Index, "F4");
  1904. }
  1905. [Test]
  1906. public void BindToReadonlyProperty ()
  1907. {
  1908. Form f = new Form ();
  1909. f.ShowInTaskbar = false;
  1910. DataGridView dgv = new DataGridView ();
  1911. List<cust> l = new List<cust> ();
  1912. l.Add (new cust ());
  1913. dgv.DataSource = l;
  1914. f.Controls.Add (dgv);
  1915. f.Show ();
  1916. Assert.AreEqual ("Name", dgv.Columns[0].Name, "A1");
  1917. Assert.AreEqual (true, dgv.Columns[0].ReadOnly, "A2");
  1918. f.Close ();
  1919. f.Dispose ();
  1920. }
  1921. class cust { public string Name { get { return "test"; } } }
  1922. [Test]
  1923. public void EnableHeadersVisualStylesDefaultValue ()
  1924. {
  1925. Assert.AreEqual (true, new DataGridView ().EnableHeadersVisualStyles);
  1926. }
  1927. [Test]
  1928. public void RowTemplate ()
  1929. {
  1930. DataGridView dgv = new DataGridView ();
  1931. Form f = new Form ();
  1932. f.Controls.Add (dgv);
  1933. f.Show ();
  1934. dgv.Columns.Add ("A1", "A1");
  1935. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A1");
  1936. Assert.IsNull (dgv.RowTemplate.DataGridView, "A2");
  1937. dgv.Columns.Add ("A2", "A2");
  1938. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A3");
  1939. dgv.Rows.Add (3, 6);
  1940. dgv.Columns.Remove ("A1");
  1941. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A4");
  1942. Assert.AreEqual (1, dgv.Rows[0].Cells.Count, "A5");
  1943. dgv.Columns.Clear ();
  1944. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A6");
  1945. Assert.AreEqual (0, dgv.Rows.Count, "A7");
  1946. f.Close ();
  1947. f.Dispose ();
  1948. }
  1949. [Test]
  1950. public void ScrollToSelectionSynchronous()
  1951. {
  1952. DataGridView dgv = new DataGridView ();
  1953. dgv.RowCount = 1000;
  1954. dgv.CurrentCell = dgv[0, dgv.RowCount -1];
  1955. Rectangle rowRect = dgv.GetRowDisplayRectangle (dgv.RowCount - 1, false);
  1956. Assert.AreEqual (true, dgv.DisplayRectangle.Contains (rowRect), "#01");
  1957. }
  1958. [Test]
  1959. public void CurrentCell()
  1960. {
  1961. DataGridView dgv = new DataGridView ();
  1962. dgv.AllowUserToAddRows = false;
  1963. Assert.IsNull (dgv.CurrentCell, "A1");
  1964. dgv.RowCount = 10;
  1965. dgv.ColumnCount = 2;
  1966. Assert.AreEqual (10, dgv.RowCount, "B1");
  1967. Assert.AreEqual (2, dgv.ColumnCount, "B2");
  1968. Assert.IsNull (dgv.CurrentCell, "B3");
  1969. dgv.CurrentCell = dgv[1, 9];
  1970. Assert.IsNotNull (dgv.CurrentCell, "H1");
  1971. Assert.AreEqual (9, dgv.CurrentCell.RowIndex, "H2");
  1972. Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "H3");
  1973. dgv.CurrentCell = null;
  1974. Assert.IsNull (dgv.CurrentCell, "C1");
  1975. dgv.CurrentCell = dgv[1, 9];
  1976. Assert.IsNotNull (dgv.CurrentCell, "D1");
  1977. Assert.AreEqual (9, dgv.CurrentCell.RowIndex, "D2");
  1978. Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "D3");
  1979. dgv.RowCount = 9;
  1980. Assert.IsNotNull (dgv.CurrentCell, "E1");
  1981. Assert.AreEqual (8, dgv.CurrentCell.RowIndex, "E2");
  1982. Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "E3");
  1983. dgv.CurrentCell = dgv[0, 4];
  1984. dgv.RowCount = 2;
  1985. Assert.IsNotNull (dgv.CurrentCell, "F1");
  1986. Assert.AreEqual (1, dgv.CurrentCell.RowIndex, "F2");
  1987. Assert.AreEqual (0, dgv.CurrentCell.ColumnIndex, "F3");
  1988. dgv.RowCount = 0;
  1989. Assert.IsNull (dgv.CurrentCell, "P1");
  1990. dgv.RowCount = 10;
  1991. Assert.AreEqual (10, dgv.RowCount, "I1");
  1992. dgv.CurrentCell = dgv[0, 4];
  1993. dgv.ColumnCount = 0;
  1994. Assert.AreEqual (0, dgv.RowCount, "I2");
  1995. Assert.IsNull (dgv.CurrentCell, "I3");
  1996. dgv.RowCount = 0;
  1997. dgv.ColumnCount = 0;
  1998. dgv.CreateControl ();
  1999. dgv.ColumnCount = 2;
  2000. dgv.RowCount = 3;
  2001. Assert.IsNotNull (dgv.CurrentCell, "G1");
  2002. Assert.AreEqual (0, dgv.CurrentCell.RowIndex, "G1");
  2003. Assert.AreEqual (0, dgv.CurrentCell.ColumnIndex, "G1");
  2004. }
  2005. [Test]
  2006. public void DataSourceBindingContextDependency ()
  2007. {
  2008. List<DataItem> dataList = new List<DataItem> ();
  2009. dataList.Add (new DataItem ());
  2010. dataList.Add (new DataItem ());
  2011. DataGridView dgv = new DataGridView ();
  2012. dgv.DataSource = dataList;
  2013. Assert.IsNull (dgv.BindingContext, "#1");
  2014. Assert.IsFalse (dgv.IsHandleCreated, "#2");
  2015. Assert.AreEqual (0, dgv.RowCount, "#3");
  2016. dgv.DataSource = null;
  2017. Form form = new Form ();
  2018. form.Controls.Add (dgv);
  2019. dgv.DataSource = dataList;
  2020. Assert.IsNotNull (dgv.BindingContext, "#4");
  2021. Assert.IsFalse (dgv.IsHandleCreated, "#5");
  2022. Assert.AreEqual (2, dgv.RowCount, "#6");
  2023. dgv.Dispose ();
  2024. dgv = new DataGridView ();
  2025. dgv.DataSource = dataList;
  2026. Assert.IsNull (dgv.BindingContext, "#7");
  2027. Assert.IsFalse (dgv.IsHandleCreated, "#8");
  2028. Assert.AreEqual (0, dgv.RowCount, "#9");
  2029. dgv.CreateControl ();
  2030. Assert.IsNull (dgv.BindingContext, "#10");
  2031. Assert.IsTrue (dgv.IsHandleCreated, "#11");
  2032. Assert.AreEqual (0, dgv.RowCount, "#12");
  2033. }
  2034. [Test]
  2035. public void RowTemplateDataGridView ()
  2036. {
  2037. DataGridView gdv = new DataGridView ();
  2038. Assert.IsNull (gdv.RowTemplate.DataGridView, "#1");
  2039. }
  2040. [Test] // Xamarin bug 2392
  2041. public void RowHeightInVirtualMode ()
  2042. {
  2043. using (var dgv = new DataGridView ()) {
  2044. dgv.RowHeightInfoNeeded += (sender, e) => {
  2045. e.Height = 50;
  2046. e.MinimumHeight = 30;
  2047. };
  2048. dgv.VirtualMode = true;
  2049. dgv.RowCount = 2;
  2050. Assert.AreEqual (50, dgv.Rows [0].Height);
  2051. Assert.AreEqual (30, dgv.Rows [0].MinimumHeight);
  2052. Assert.AreEqual (50, dgv.Rows [1].Height);
  2053. Assert.AreEqual (30, dgv.Rows [1].MinimumHeight);
  2054. }
  2055. }
  2056. [Test] // Novell bug 660986
  2057. public void TestDispose ()
  2058. {
  2059. DataGridView dgv = new DataGridView ();
  2060. dgv.Columns.Add ("TestColumn", "Test column");
  2061. dgv.Rows.Add ();
  2062. dgv.Dispose ();
  2063. try {
  2064. DataGridViewColumn col = dgv.Columns[0];
  2065. Assert.Fail ("#1");
  2066. }
  2067. catch (ArgumentOutOfRangeException) {
  2068. }
  2069. try {
  2070. DataGridViewRow row = dgv.Rows[0];
  2071. Assert.Fail ("#2");
  2072. }
  2073. catch (ArgumentOutOfRangeException) {
  2074. }
  2075. }
  2076. [Test] // Xamarin bug 3125
  2077. public void TestRemoveBug3125 ()
  2078. {
  2079. DataGridViewRow dgvr1 = new DataGridViewRow ();
  2080. DataGridViewRow dgvr2 = new DataGridViewRow ();
  2081. DataGridViewRow dgvr3 = new DataGridViewRow ();
  2082. Assert.IsNull (dgvr1.DataGridView, "#1");
  2083. Assert.IsNull (dgvr2.DataGridView, "#2");
  2084. Assert.IsNull (dgvr3.DataGridView, "#3");
  2085. DataGridView dgv = new DataGridView ();
  2086. // dgv needs column and cell or throws error
  2087. DataGridViewColumn dgvc1 = new DataGridViewColumn ();
  2088. DataGridViewCell cell = new DataGridViewTextBoxCell ();
  2089. dgvc1.CellTemplate = cell;
  2090. dgv.Columns.Add (dgvc1);
  2091. dgv.Rows.Add (dgvr1);
  2092. dgv.Rows.Add (dgvr2);
  2093. dgv.Rows.Add (dgvr3);
  2094. // was dgv.Clear () and that caused test build to fail
  2095. dgv.Rows.Clear (); // presumbly this was the intention?
  2096. Assert.IsNull (dgvr1.DataGridView, "#4");
  2097. Assert.IsNull (dgvr2.DataGridView, "#5");
  2098. Assert.IsNull (dgvr3.DataGridView, "#6");
  2099. }
  2100. }
  2101. [TestFixture]
  2102. public class DataGridViewControlCollectionTest
  2103. {
  2104. [Test]
  2105. public void TestClear ()
  2106. {
  2107. using (DataGridView dgv = new DataGridView ()) {
  2108. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection) dgv.Controls;
  2109. Control c1 = new Control ();
  2110. Control c2 = new Control ();
  2111. Control c3 = new Control ();
  2112. Assert.AreEqual (2, controls.Count, "#02");
  2113. controls.Add (c1);
  2114. controls.Add (c2);
  2115. controls.Add (c3);
  2116. Assert.AreEqual (5, controls.Count, "#02");
  2117. controls.Clear ();
  2118. Assert.AreEqual (3, controls.Count, "#03");
  2119. Assert.AreSame (c2, controls [2], "#04");
  2120. }
  2121. // Maybe MS should start writing unit-tests?
  2122. using (DataGridView dgv = new DataGridView ()) {
  2123. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2124. Control [] c = new Control [20];
  2125. for (int i = 0; i < c.Length; i++) {
  2126. c [i] = new Control ();
  2127. c [i].Text = "#" + i.ToString ();
  2128. }
  2129. Assert.AreEqual (2, controls.Count, "#02");
  2130. controls.AddRange (c);
  2131. Assert.AreEqual (22, controls.Count, "#02");
  2132. controls.Clear ();
  2133. Assert.AreEqual (12, controls.Count, "#03");
  2134. for (int i = 0; i < c.Length; i += 2) {
  2135. Assert.AreSame (c [i+1], controls [ (i / 2) + 2], "#A" + i.ToString ());
  2136. }
  2137. }
  2138. }
  2139. [Test]
  2140. public void TestCopyTo ()
  2141. {
  2142. using (DataGridView dgv = new DataGridView ()) {
  2143. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2144. Control c1 = new Control ();
  2145. Control c2 = new Control ();
  2146. Control c3 = new Control ();
  2147. Control [] copy = new Control [10];
  2148. Assert.AreEqual (2, controls.Count, "#01");
  2149. controls.AddRange (new Control [] { c1, c2, c3 });
  2150. Assert.AreEqual (5, controls.Count, "#01-b");
  2151. controls.CopyTo (copy, 0);
  2152. Assert.AreEqual (5, controls.Count, "#02");
  2153. Assert.AreEqual (10, copy.Length, "#03");
  2154. for (int i = 0; i < copy.Length; i++) {
  2155. if (i >= 5)
  2156. Assert.IsNull (copy [i], "#A" + i.ToString ());
  2157. else
  2158. Assert.IsNotNull (copy [i], "#B" + i.ToString ());
  2159. }
  2160. }
  2161. }
  2162. [Test]
  2163. [ExpectedException (typeof (NotSupportedException))]
  2164. public void TestInsert ()
  2165. {
  2166. using (DataGridView dgv = new DataGridView ()) {
  2167. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2168. controls.Insert (1, new Control ());
  2169. }
  2170. }
  2171. [Test]
  2172. public void TestRemove ()
  2173. {
  2174. using (DataGridView dgv = new DataGridView ()) {
  2175. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2176. Control c1 = new Control ();
  2177. Control c2 = new Control ();
  2178. Control c3 = new Control ();
  2179. Control [] copy = new Control [10];
  2180. controls.AddRange (new Control [] {c1, c2, c3});
  2181. controls.Remove (c2);
  2182. Assert.AreEqual (4, controls.Count, "#01");
  2183. controls.Remove (c2);
  2184. Assert.AreEqual (4, controls.Count, "#02");
  2185. controls.Remove (c1);
  2186. Assert.AreEqual (3, controls.Count, "#03");
  2187. controls.Remove (c3);
  2188. Assert.AreEqual (2, controls.Count, "#04");
  2189. controls.Remove (controls [0]);
  2190. controls.Remove (controls [1]);
  2191. Assert.AreEqual (2, controls.Count, "#05");
  2192. }
  2193. }
  2194. }
  2195. }
  2196. #endif