PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms 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

Large files files are truncated, but you can click here to view the full file

  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

Large files files are truncated, but you can click here to view the full file