PageRenderTime 71ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/mono/mono
C# | 2715 lines | 2643 code | 41 blank | 31 comment | 40 complexity | a925dd3d3adafe8632257fe5c91ac98c MD5 | raw file
Possible License(s): GPL-2.0, CC-BY-SA-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, Unlicense, Apache-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. using System;
  27. using System.Data;
  28. using System.Drawing;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.ComponentModel;
  32. using System.Diagnostics;
  33. using System.IO;
  34. using System.Runtime.InteropServices;
  35. using System.Text;
  36. using System.Windows.Forms;
  37. using NUnit.Framework;
  38. namespace MonoTests.System.Windows.Forms
  39. {
  40. [TestFixture]
  41. public class DataGridViewTest : TestHelper
  42. {
  43. // Send a mouse event in Win32.
  44. [DllImport ("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  45. private static extern void mouse_event (int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
  46. private const int MOUSEEVENTF_LEFTDOWN = 0x02;
  47. private const int MOUSEEVENTF_LEFTUP = 0x04;
  48. private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
  49. private const int MOUSEEVENTF_RIGHTUP = 0x10;
  50. private const int MOUSEEVENTF_ABSOLUTE = 0x8000;
  51. // Set the mouse-pointer position in Win32.
  52. [DllImport ("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  53. private static extern long SetCursorPos (int x, int y);
  54. // Convert from window coordinates to screen coordinates in Win32.
  55. [DllImport ("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
  56. private static extern bool ClientToScreen (IntPtr hWnd, ref Win32Point point);
  57. [StructLayout (LayoutKind.Sequential)]
  58. private struct Win32Point
  59. {
  60. public int x;
  61. public int y;
  62. };
  63. private DataGridView grid = null;
  64. [SetUp]
  65. protected override void SetUp()
  66. {
  67. grid = new DataGridView();
  68. base.SetUp ();
  69. }
  70. [TearDown]
  71. protected override void TearDown ()
  72. {
  73. grid.Dispose ();
  74. base.TearDown ();
  75. }
  76. [Test]
  77. [ExpectedException (typeof (InvalidOperationException), ExpectedMessage = "Generating Clipboard content is not supported when the ClipboardCopyMode property is Disable.")]
  78. public void GetClipboardContentsDisabled ()
  79. {
  80. using (DataGridView dgv = new DataGridView ()) {
  81. dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
  82. object o = dgv.GetClipboardContent ();
  83. }
  84. }
  85. private class ExposeProtectedProperties : DataGridView
  86. {
  87. public new Padding DefaultPadding { get { return base.DefaultPadding; } }
  88. public new Size DefaultSize { get { return base.DefaultSize; } }
  89. public new bool IsDoubleBuffered { get { return base.DoubleBuffered; } }
  90. public ControlStyles GetControlStyles ()
  91. {
  92. ControlStyles retval = (ControlStyles)0;
  93. foreach (ControlStyles cs in Enum.GetValues (typeof (ControlStyles)))
  94. if (this.GetStyle (cs) == true)
  95. retval |= cs;
  96. return retval;
  97. }
  98. public bool PublicIsInputKey (Keys keyData)
  99. {
  100. return base.IsInputKey (keyData);
  101. }
  102. public bool PublicIsInputChar (char charCode)
  103. {
  104. return base.IsInputChar (charCode);
  105. }
  106. }
  107. #region GenerateClipboardTest
  108. public static void GenerateClipboardTest ()
  109. {
  110. GenerateClipboardTest (false);
  111. GenerateClipboardTest (true);
  112. }
  113. public static string GenerateClipboardTest (bool headers)
  114. {
  115. StringBuilder result = new StringBuilder ();
  116. int tab = 0;
  117. string classname = headers ? "DataGridViewClipboardHeaderTest" : "DataGridViewClipboardTest";
  118. append (result, tab, "//");
  119. append (result, tab, "// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)");
  120. append (result, tab, "//");
  121. append (result, tab, "// Author:");
  122. append (result, tab, "// DataGridViewTest.GenerateClipboardTest ({0});", headers.ToString ().ToLower ());
  123. append (result, tab, "//");
  124. append (result, tab, "#if NET_2_0");
  125. append (result, tab, "using NUnit.Framework;");
  126. append (result, tab, "using System;");
  127. append (result, tab, "using System.Drawing;");
  128. append (result, tab, "using System.Windows.Forms;");
  129. append (result, tab, "using System.ComponentModel;");
  130. append (result, tab, "using System.Collections;");
  131. append (result, tab, "using System.Text;");
  132. append (result, tab, "using System.Collections.Generic;");
  133. append (result, tab, "using System.Diagnostics;");
  134. append (result, tab, "using System.IO;");
  135. append (result, tab, "namespace MonoTests.System.Windows.Forms {"); tab++;
  136. append (result, tab, "[TestFixture]");
  137. append (result, tab, "public class {0} {{", classname); tab++;
  138. append (result, tab, "[Test]");
  139. append (result, tab, "public void Test () {"); tab++;
  140. append (result, tab, "DataObject data;");
  141. append (result, tab, "DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell row_header_cell;");
  142. append (result, tab, "DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell col_header_cell;");
  143. //append (result, tab, "string csv = null, html = null, utext = null, text = null;");
  144. append (result, tab, "string code = null;");
  145. int counter;
  146. List<List<int>> selected_bands = new List<List<int>> ();
  147. List<List<CellSelection>> selected_cells = new List<List<CellSelection>> ();
  148. selected_bands.Add (new List<int> ());
  149. selected_bands.Add (new List<int> (new int [] { 0 }));
  150. selected_bands.Add (new List<int> (new int [] { 2 }));
  151. selected_bands.Add (new List<int> (new int [] { 1, 2 }));
  152. selected_bands.Add (new List<int> (new int [] { 1, 3 }));
  153. selected_cells.Add (new List<CellSelection> ());
  154. selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (0, 0, true) }));
  155. selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (2, 2, false) }));
  156. selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (0, 0, false), new CellSelection (2, 2, true) }));
  157. foreach (DataGridViewClipboardCopyMode copymode in Enum.GetValues (typeof (DataGridViewClipboardCopyMode))) {
  158. if (copymode == DataGridViewClipboardCopyMode.Disable)
  159. continue;
  160. counter = 0;
  161. foreach (DataGridViewSelectionMode selectionmode in Enum.GetValues (typeof (DataGridViewSelectionMode))) {
  162. bool is_row_selectable, is_col_selectable, is_cell_selectable;
  163. is_row_selectable = selectionmode == DataGridViewSelectionMode.RowHeaderSelect || selectionmode == DataGridViewSelectionMode.FullRowSelect;
  164. is_col_selectable = selectionmode == DataGridViewSelectionMode.ColumnHeaderSelect || selectionmode == DataGridViewSelectionMode.FullColumnSelect;
  165. is_cell_selectable = selectionmode == DataGridViewSelectionMode.CellSelect || selectionmode == DataGridViewSelectionMode.ColumnHeaderSelect || selectionmode == DataGridViewSelectionMode.RowHeaderSelect;
  166. foreach (List<int> cols in selected_bands) {
  167. if (!is_col_selectable && cols.Count > 0)
  168. continue;
  169. foreach (List<int> rows in selected_bands) {
  170. if (!is_row_selectable && rows.Count > 0)
  171. continue;
  172. foreach (List<CellSelection> cells in selected_cells) {
  173. if (!is_cell_selectable && cells.Count > 0)
  174. continue;
  175. using (DataGridView dgv = DataGridViewCommon.CreateAndFillForClipboard ()) {
  176. dgv.SelectionMode = selectionmode;
  177. dgv.ClipboardCopyMode = copymode;
  178. bool any_selected = false;
  179. if (is_col_selectable && cols.Count > 0) {
  180. foreach (int c in cols) {
  181. dgv.Columns [c].Selected = true;
  182. any_selected = true;
  183. }
  184. }
  185. if (is_row_selectable && rows.Count > 0) {
  186. foreach (int r in rows) {
  187. dgv.Rows [r].Selected = true;
  188. any_selected = true;
  189. }
  190. }
  191. if (is_cell_selectable && cells.Count > 0) {
  192. foreach (CellSelection selection in cells) {
  193. DataGridViewCell cell = dgv.Rows [selection.Row].Cells [selection.Col];
  194. if (cell.Selected != selection.Selected) {
  195. cell.Selected = selection.Selected;
  196. any_selected = true;
  197. }
  198. }
  199. }
  200. if (any_selected == false && !(cols.Count == 0 && rows.Count == 0 && cells.Count == 0)) {
  201. continue;
  202. }
  203. generate_case (result, dgv, copymode.ToString () + "#" + (counter++).ToString (), headers);
  204. }
  205. }
  206. }
  207. }
  208. }
  209. }
  210. append (result, --tab, "}");
  211. append (result, --tab, "}");
  212. append (result, --tab, "}");
  213. append (result, tab, "#endif"); ;
  214. throw new NotImplementedException ("Where am I?");
  215. // Uncomment the following line, change the path, and comment out the exception.
  216. //File.WriteAllText (@"Z:\mono\head\mcs\class\SWF\Test\System.Windows.Forms\" + classname + ".cs", result.ToString ());
  217. return string.Empty;
  218. }
  219. private static string tabs (int t) { return new string ('\t', t); }
  220. private static void append (StringBuilder result, int tab, string text) { result.Append (tabs (tab) + text + "\n"); }
  221. private static void append (StringBuilder result, int tab, string text, params object [] args) { result.Append (tabs (tab) + string.Format (text, args) + "\n"); }
  222. private static string cs_encode (string literal, string newline) {
  223. bool has_newlines = literal.Contains ("\r\n");
  224. bool format_string = has_newlines;
  225. literal = literal.Replace ("\\", "\\\\");
  226. literal = literal.Replace ("\"", "\\\"");
  227. literal = literal.Replace ("\t", "\\t");
  228. if (has_newlines) {
  229. if (newline == @"""\r\n""") {
  230. literal = literal.Replace ("\r\n", @"\r\n");
  231. format_string = false;
  232. } else {
  233. literal = literal.Replace ("\r\n", "{0}");
  234. }
  235. }
  236. literal = "\"" + literal + "\"";
  237. if (format_string) {
  238. return "string.Format (" + literal/*.Replace ("{", "{{").Replace ("}", "}}")*/ + ", " + newline + ")";
  239. } else {
  240. return literal;
  241. }
  242. }
  243. private static string cs_encode (string literal) {
  244. return cs_encode (literal, "Environment.NewLine");
  245. }
  246. private class CellSelection {
  247. public bool Selected;
  248. public int Row;
  249. public int Col;
  250. public CellSelection (int Row, int Col, bool Selected) {
  251. this.Selected = Selected;
  252. this.Row = Row;
  253. this.Col = Col;
  254. }
  255. }
  256. static private void generate_case (StringBuilder result, DataGridView dgv, string message, bool headers)
  257. {
  258. Console.WriteLine (message + ", current length: " + result.Length.ToString ());
  259. Debug.WriteLine (message + ", current length: " + result.Length.ToString ());
  260. if (headers) {
  261. if (dgv.SelectionMode != DataGridViewSelectionMode.CellSelect)
  262. return;
  263. if (dgv.ClipboardCopyMode != DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText)
  264. return;
  265. }
  266. int tab = 3;
  267. DataObject data;
  268. string csv = null, html = null, utext = null, text = null;
  269. string code = null;
  270. DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell row_header_cell;
  271. DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell col_header_cell;
  272. int counter = 0;
  273. append (result, tab, "using (DataGridView dgv = DataGridViewCommon.CreateAndFillForClipboard ()) {");
  274. tab++;
  275. append (result, tab, "dgv.SelectionMode = DataGridViewSelectionMode.{0};", dgv.SelectionMode.ToString ());
  276. append (result, tab, "dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.{0};", dgv.ClipboardCopyMode.ToString ());
  277. switch (dgv.SelectionMode) {
  278. case DataGridViewSelectionMode.FullRowSelect:
  279. foreach (DataGridViewRow row in dgv.Rows) {
  280. if (row.Selected) {
  281. append (result, tab, "dgv.Rows [{0}].Selected = true;", row.Index);
  282. }
  283. }
  284. break;
  285. case DataGridViewSelectionMode.FullColumnSelect:
  286. foreach (DataGridViewColumn col in dgv.Columns) {
  287. if (col.Selected) {
  288. append (result, tab, "dgv.Columns [{0}].Selected = true;", col.Index);
  289. }
  290. }
  291. break;
  292. case DataGridViewSelectionMode.ColumnHeaderSelect:
  293. case DataGridViewSelectionMode.RowHeaderSelect:
  294. case DataGridViewSelectionMode.CellSelect:
  295. if (dgv.SelectionMode == DataGridViewSelectionMode.RowHeaderSelect) {
  296. foreach (DataGridViewRow row in dgv.Rows) {
  297. if (row.Selected) {
  298. append (result, tab, "dgv.Rows [{0}].Selected = true;", row.Index);
  299. }
  300. }
  301. }
  302. if (dgv.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect) {
  303. foreach (DataGridViewColumn col in dgv.Columns) {
  304. if (col.Selected) {
  305. append (result, tab, "dgv.Columns [{0}].Selected = true;", col.Index);
  306. }
  307. }
  308. }
  309. for (int r = 0; r < dgv.RowCount; r++) {
  310. for (int c = 0; c < dgv.ColumnCount; c++) {
  311. bool rowS = dgv.Rows [r].Selected;
  312. bool colS = dgv.Columns [c].Selected;
  313. bool cellS = dgv.Rows [r].Cells [c].Selected;
  314. if ((rowS || colS) && !cellS) {
  315. append (result, tab, "dgv.Rows [{0}].Cells [{1}].Selected = false;", r, c);
  316. } else if ((!rowS && !colS) && cellS) {
  317. append (result, tab, "dgv.Rows [{0}].Cells [{1}].Selected = true;", r, c);
  318. }
  319. }
  320. }
  321. break;
  322. }
  323. if (!headers) {
  324. data = dgv.GetClipboardContent ();
  325. append (result, tab, "data = dgv.GetClipboardContent ();");
  326. if (data == null) {
  327. append (result, tab, "Assert.IsNull (data, {0});", cs_encode ("#" + message + "-" + (counter++).ToString ()));
  328. } else {
  329. append (result, tab, "Assert.IsNotNull (data, {0});", cs_encode ("#" + message + "-" + (counter++).ToString ()));
  330. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  331. html = data.GetData (DataFormats.Html) as string;
  332. utext = data.GetData (DataFormats.UnicodeText) as string;
  333. text = data.GetData (DataFormats.Text) as string;
  334. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.CommaSeparatedValue), {1});", cs_encode (csv), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  335. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.Html), {1});", cs_encode (html, @"""\r\n"""), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  336. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.UnicodeText), {1});", cs_encode (utext), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  337. append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.Text), {1});", cs_encode (text), cs_encode ("#" + message + "-" + (counter++).ToString ()));
  338. }
  339. } else {
  340. bool [] bools = new bool [] { true, false };
  341. string [] formats = new string [] { DataFormats.Text, DataFormats.UnicodeText, DataFormats.Html, DataFormats.CommaSeparatedValue };
  342. foreach (bool a in bools) {
  343. foreach (bool b in bools) {
  344. foreach (bool c in bools) {
  345. foreach (bool d in bools) {
  346. foreach (string format in formats) {
  347. bool did_selected = false;
  348. bool did_unselected = false;
  349. foreach (DataGridViewRow row in dgv.Rows) {
  350. int i = row.Index;
  351. if (row.Selected) {
  352. if (did_selected)
  353. continue;
  354. did_selected = true;
  355. } else {
  356. if (did_unselected)
  357. continue;
  358. did_unselected = true;
  359. }
  360. row_header_cell = row.HeaderCell as DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell;
  361. if (row_header_cell == null) {
  362. append (result, tab, "Assert.IsNull (dgv.Rows [{0}].Headercell, {1});", row.Index, cs_encode ("#" + message + "-" + (counter++).ToString ()));
  363. } else {
  364. append (result, tab, "row_header_cell = dgv.Rows [{0}].HeaderCell as DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell;", row.Index);
  365. code = cs_encode (row_header_cell.GetClipboardContentPublic (i, a, b, c, d, format) as string);
  366. 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);
  367. append (result, tab, "Assert.AreEqual ({0}, code, {1});", code, cs_encode ("#" + message + "-" + (counter++).ToString ()));
  368. }
  369. }
  370. }
  371. }
  372. }
  373. }
  374. }
  375. foreach (bool a in bools) {
  376. foreach (bool b in bools) {
  377. foreach (bool c in bools) {
  378. foreach (bool d in bools) {
  379. foreach (string format in formats) {
  380. bool did_selected = false;
  381. bool did_unselected = false;
  382. foreach (DataGridViewColumn col in dgv.Columns) {
  383. int i = -1;
  384. if (col.Index > 1)
  385. continue;
  386. if (col.Selected) {
  387. if (did_selected)
  388. continue;
  389. did_selected = true;
  390. } else {
  391. if (did_unselected)
  392. continue;
  393. did_unselected = true;
  394. }
  395. col_header_cell = col.HeaderCell as DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell;
  396. append (result, tab, "col_header_cell = dgv.Columns [{0}].HeaderCell as DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell;", col.Index);
  397. code = cs_encode (col_header_cell.GetClipboardContentPublic (i, a, b, c, d, format) as string);
  398. 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);
  399. append (result, tab, "Assert.AreEqual ({0}, code, {1});", code, cs_encode ("#" + message + "-" + (counter++).ToString ()));
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. }
  407. tab--;
  408. append (result, tab, "}");
  409. }
  410. #endregion GenerateClipboardTest
  411. [Test]
  412. public void GetClipboardContents ()
  413. {
  414. DataObject data;
  415. string csv, html, utext, text;
  416. using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
  417. data = dgv.GetClipboardContent ();
  418. Assert.IsNull (data, "#01");
  419. dgv.Rows [0].Cells [0].Selected = true;
  420. data = dgv.GetClipboardContent ();
  421. Assert.IsNotNull (data, "#B1");
  422. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
  423. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
  424. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  425. html = data.GetData (DataFormats.Html) as string;
  426. utext = data.GetData (DataFormats.UnicodeText) as string;
  427. text = data.GetData (DataFormats.Text) as string;
  428. Assert.AreEqual ("Cell A1", csv, "CSV B");
  429. Assert.AreEqual ("Cell A1", utext, "UTEXT B");
  430. Assert.AreEqual ("Cell A1", text, "TEXT B");
  431. Assert.AreEqual (string.Format(@"Version:1.0{0}" +
  432. "StartHTML:00000097{0}" +
  433. "EndHTML:00000211{0}" +
  434. "StartFragment:00000133{0}" +
  435. "EndFragment:00000175{0}" +
  436. "<HTML>{0}" +
  437. "<BODY>{0}" +
  438. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD></TR></TABLE>{0}" +
  439. "<!--EndFragment-->{0}" +
  440. "</BODY>{0}" +
  441. "</HTML>", "\r\n"), html, "HTML B");
  442. dgv.Rows [1].Cells [1].Selected = true;
  443. data = dgv.GetClipboardContent ();
  444. Assert.IsNotNull (data, "#C1");
  445. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
  446. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
  447. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  448. html = data.GetData (DataFormats.Html) as string;
  449. utext = data.GetData (DataFormats.UnicodeText) as string;
  450. text = data.GetData (DataFormats.Text) as string;
  451. Assert.AreEqual (string.Format("Cell A1,{0},Cell B2", Environment.NewLine), csv, "CSV C");
  452. Assert.AreEqual (string.Format("Cell A1\t{0}\tCell B2", Environment.NewLine), utext, "UTEXT C");
  453. Assert.AreEqual (string.Format("Cell A1\t{0}\tCell B2", Environment.NewLine), text, "TEXT C");
  454. string tmp;
  455. tmp = string.Format(@"Version:1.0{0}" +
  456. "StartHTML:00000097{0}" +
  457. "EndHTML:00000266{0}" +
  458. "StartFragment:00000133{0}" +
  459. "EndFragment:00000230{0}" +
  460. "<HTML>{0}" +
  461. "<BODY>{0}" +
  462. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  463. "<!--EndFragment-->{0}" +
  464. "</BODY>{0}" +
  465. "</HTML>", "\r\n");
  466. Assert.AreEqual (string.Format(@"Version:1.0{0}" +
  467. "StartHTML:00000097{0}" +
  468. "EndHTML:00000266{0}" +
  469. "StartFragment:00000133{0}" +
  470. "EndFragment:00000230{0}" +
  471. "<HTML>{0}" +
  472. "<BODY>{0}" +
  473. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  474. "<!--EndFragment-->{0}" +
  475. "</BODY>{0}" +
  476. "</HTML>", "\r\n"), html, "HTML C");
  477. }
  478. }
  479. [Test]
  480. public void GetClipboardContents_HeadersAlways ()
  481. {
  482. DataObject data;
  483. string csv, html, utext, text;
  484. using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
  485. dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
  486. data = dgv.GetClipboardContent ();
  487. Assert.IsNull (data, "#01");
  488. dgv.Rows [0].Cells [0].Selected = true;
  489. data = dgv.GetClipboardContent ();
  490. Assert.IsNotNull (data, "#B1");
  491. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
  492. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
  493. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  494. html = data.GetData (DataFormats.Html) as string;
  495. utext = data.GetData (DataFormats.UnicodeText) as string;
  496. text = data.GetData (DataFormats.Text) as string;
  497. Assert.AreEqual (string.Format (",A{0},Cell A1", Environment.NewLine), csv, "CSV B");
  498. Assert.AreEqual (string.Format ("\tA{0}\tCell A1", Environment.NewLine), utext, "UTEXT B");
  499. Assert.AreEqual (string.Format ("\tA{0}\tCell A1", Environment.NewLine), text, "TEXT B");
  500. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  501. "StartHTML:00000097{0}" +
  502. "EndHTML:00000281{0}" +
  503. "StartFragment:00000133{0}" +
  504. "EndFragment:00000245{0}" +
  505. "<HTML>{0}" +
  506. "<BODY>{0}" +
  507. "<!--StartFragment--><TABLE><THEAD><TH>&nbsp;</TH><TH>A</TH></THEAD><TR><TD ALIGN=\"center\">&nbsp;</TD><TD>Cell A1</TD></TR></TABLE>{0}" +
  508. "<!--EndFragment-->{0}" +
  509. "</BODY>{0}" +
  510. "</HTML>", "\r\n"), html, "HTML B");
  511. dgv.Rows [1].Cells [1].Selected = true;
  512. data = dgv.GetClipboardContent ();
  513. Assert.IsNotNull (data, "#C1");
  514. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
  515. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
  516. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  517. html = data.GetData (DataFormats.Html) as string;
  518. utext = data.GetData (DataFormats.UnicodeText) as string;
  519. text = data.GetData (DataFormats.Text) as string;
  520. Assert.AreEqual (string.Format (",A,B{0},Cell A1,{0},,Cell B2", Environment.NewLine), csv, "CSV C");
  521. Assert.AreEqual (string.Format ("\tA\tB{0}\tCell A1\t{0}\t\tCell B2", Environment.NewLine), utext, "UTEXT C");
  522. Assert.AreEqual (string.Format ("\tA\tB{0}\tCell A1\t{0}\t\tCell B2", Environment.NewLine), text, "TEXT C");
  523. string tmp;
  524. tmp = string.Format (@"Version:1.0{0}" +
  525. "StartHTML:00000097{0}" +
  526. "EndHTML:00000266{0}" +
  527. "StartFragment:00000133{0}" +
  528. "EndFragment:00000230{0}" +
  529. "<HTML>{0}" +
  530. "<BODY>{0}" +
  531. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  532. "<!--EndFragment-->{0}" +
  533. "</BODY>{0}" +
  534. "</HTML>", "\r\n");
  535. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  536. "StartHTML:00000097{0}" +
  537. "EndHTML:00000376{0}" +
  538. "StartFragment:00000133{0}" +
  539. "EndFragment:00000340{0}" +
  540. "<HTML>{0}" +
  541. "<BODY>{0}" +
  542. "<!--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}" +
  543. "<!--EndFragment-->{0}" +
  544. "</BODY>{0}" +
  545. "</HTML>", "\r\n"), html, "HTML C");
  546. }
  547. }
  548. [Test]
  549. public void GetClipboardContents_HeadersNever ()
  550. {
  551. DataObject data;
  552. string csv, html, utext, text;
  553. using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
  554. dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
  555. data = dgv.GetClipboardContent ();
  556. Assert.IsNull (data, "#01");
  557. dgv.Rows [0].Cells [0].Selected = true;
  558. data = dgv.GetClipboardContent ();
  559. Assert.IsNotNull (data, "#B1");
  560. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
  561. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
  562. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  563. html = data.GetData (DataFormats.Html) as string;
  564. utext = data.GetData (DataFormats.UnicodeText) as string;
  565. text = data.GetData (DataFormats.Text) as string;
  566. Assert.AreEqual ("Cell A1", csv, "CSV B");
  567. Assert.AreEqual ("Cell A1", utext, "UTEXT B");
  568. Assert.AreEqual ("Cell A1", text, "TEXT B");
  569. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  570. "StartHTML:00000097{0}" +
  571. "EndHTML:00000211{0}" +
  572. "StartFragment:00000133{0}" +
  573. "EndFragment:00000175{0}" +
  574. "<HTML>{0}" +
  575. "<BODY>{0}" +
  576. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD></TR></TABLE>{0}" +
  577. "<!--EndFragment-->{0}" +
  578. "</BODY>{0}" +
  579. "</HTML>", "\r\n"), html, "HTML B");
  580. dgv.Rows [1].Cells [1].Selected = true;
  581. data = dgv.GetClipboardContent ();
  582. Assert.IsNotNull (data, "#C1");
  583. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
  584. Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
  585. csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
  586. html = data.GetData (DataFormats.Html) as string;
  587. utext = data.GetData (DataFormats.UnicodeText) as string;
  588. text = data.GetData (DataFormats.Text) as string;
  589. Assert.AreEqual (string.Format ("Cell A1,{0},Cell B2", Environment.NewLine), csv, "CSV C");
  590. Assert.AreEqual (string.Format ("Cell A1\t{0}\tCell B2", Environment.NewLine), utext, "UTEXT C");
  591. Assert.AreEqual (string.Format ("Cell A1\t{0}\tCell B2", Environment.NewLine), text, "TEXT C");
  592. string tmp;
  593. tmp = string.Format (@"Version:1.0{0}" +
  594. "StartHTML:00000097{0}" +
  595. "EndHTML:00000266{0}" +
  596. "StartFragment:00000133{0}" +
  597. "EndFragment:00000230{0}" +
  598. "<HTML>{0}" +
  599. "<BODY>{0}" +
  600. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  601. "<!--EndFragment-->{0}" +
  602. "</BODY>{0}" +
  603. "</HTML>", "\r\n");
  604. Assert.AreEqual (string.Format (@"Version:1.0{0}" +
  605. "StartHTML:00000097{0}" +
  606. "EndHTML:00000266{0}" +
  607. "StartFragment:00000133{0}" +
  608. "EndFragment:00000230{0}" +
  609. "<HTML>{0}" +
  610. "<BODY>{0}" +
  611. "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
  612. "<!--EndFragment-->{0}" +
  613. "</BODY>{0}" +
  614. "</HTML>", "\r\n"), html, "HTML C");
  615. }
  616. }
  617. [Test]
  618. public void EditingRow ()
  619. {
  620. using (DataGridView dgv = new DataGridView ()) {
  621. Assert.AreEqual (true, dgv.AllowUserToAddRows, "1");
  622. Assert.AreEqual (0, dgv.RowCount, "2");
  623. Assert.AreEqual (-1, dgv.NewRowIndex, "3");
  624. dgv.Columns.Add ("A", "B");
  625. Assert.AreEqual (1, dgv.RowCount, "4");
  626. int added;
  627. added = dgv.Rows.Add ("a");
  628. Assert.AreEqual (0, added, "5");
  629. }
  630. }
  631. [Test] // bug 82226
  632. public void EditingRowAfterAddingColumns ()
  633. {
  634. using (DataGridView _dataGridView = new DataGridView ()) {
  635. DataGridViewTextBoxColumn _nameTextBoxColumn;
  636. DataGridViewTextBoxColumn _firstNameTextBoxColumn;
  637. //
  638. // _nameTextBoxColumn
  639. //
  640. _nameTextBoxColumn = new DataGridViewTextBoxColumn ();
  641. _nameTextBoxColumn.HeaderText = "Name";
  642. _dataGridView.Columns.Add (_nameTextBoxColumn);
  643. //
  644. // _firstNameTextBoxColumn
  645. //
  646. _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
  647. _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  648. _firstNameTextBoxColumn.HeaderText = "First Name";
  649. _dataGridView.Columns.Add (_firstNameTextBoxColumn);
  650. _dataGridView.Rows.Add ("de Icaza", "Miguel");
  651. _dataGridView.Rows.Add ("Toshok", "Chris");
  652. _dataGridView.Rows.Add ("Harper", "Jackson");
  653. Assert.AreEqual (4, _dataGridView.RowCount, "#01");
  654. Assert.AreEqual (2, _dataGridView.Rows [3].Cells.Count, "#02");
  655. }
  656. }
  657. // For testing the editing-control-showing event.
  658. int editingControlShowingTest_FoundColumns;
  659. private void DataGridView_EditingControlShowingTest (object sender,
  660. DataGridViewEditingControlShowingEventArgs e)
  661. {
  662. DataGridView dgv = sender as DataGridView;
  663. if (dgv.CurrentCellAddress.X == 0)
  664. {
  665. // This is the name combo-box column.
  666. // Remember that the event-handler was called for
  667. // this column.
  668. editingControlShowingTest_FoundColumns |= 1;
  669. // Get the combo-box and the column.
  670. ComboBox cb = e.Control as ComboBox;
  671. DataGridViewComboBoxColumn col
  672. = dgv.Columns[0] as DataGridViewComboBoxColumn;
  673. // Since ObjectCollection doesn't support ToArray(), make
  674. // a list of the items in the combo-box and in the column.
  675. List<string> itemList = new List<string> ();
  676. foreach (string item in cb.Items)
  677. itemList.Add (item);
  678. List<string> expectedItemList = new List<string> ();
  679. foreach (string item in col.Items)
  680. expectedItemList.Add (item);
  681. // Make sure the combo-box has the list of allowed
  682. // items from the column.
  683. string items = string.Join (",", itemList.ToArray ());
  684. string expectedItems = string.Join (",", expectedItemList.ToArray ());
  685. Assert.AreEqual (expectedItems, items, "1-1");
  686. // Make sure the combo-box has the right selected item.
  687. Assert.AreEqual ("Boswell", cb.Text, "1-2");
  688. }
  689. else if (dgv.CurrentCellAddress.X == 1)
  690. {
  691. // This is the first-name text-box column.
  692. // Remember that the event-handler was called for
  693. // this column.
  694. editingControlShowingTest_FoundColumns |= 2;
  695. // Get the text-box.
  696. TextBox tb = e.Control as TextBox;
  697. // Make sure the text-box has the right contents.
  698. Assert.AreEqual ("Miguel", tb.Text, "1-3");
  699. }
  700. else if (dgv.CurrentCellAddress.X == 2)
  701. {
  702. // This is the chosen check-box column.
  703. // Remember that the event-handler was called for
  704. // this column.
  705. editingControlShowingTest_FoundColumns |= 4;
  706. // Get the check-box.
  707. CheckBox tb = e.Control as CheckBox;
  708. // Make sure the check-box has the right contents.
  709. Assert.AreEqual (CheckState.Checked, tb.CheckState, "1-4");
  710. }
  711. else
  712. Assert.AreEqual (0, 1, "1-5");
  713. }
  714. [Test] // Xamarin bug 5419
  715. public void EditingControlShowingTest_Unbound ()
  716. {
  717. using (DataGridView _dataGridView = new DataGridView ()) {
  718. DataGridViewComboBoxColumn _nameComboBoxColumn;
  719. DataGridViewTextBoxColumn _firstNameTextBoxColumn;
  720. DataGridViewCheckBoxColumn _chosenCheckBoxColumn;
  721. // Add the event-handler.
  722. _dataGridView.EditingControlShowing
  723. += new DataGridViewEditingControlShowingEventHandler
  724. (DataGridView_EditingControlShowingTest);
  725. // No columns have been found in the event-handler yet.
  726. editingControlShowingTest_FoundColumns = 0;
  727. // _nameComboBoxColumn
  728. _nameComboBoxColumn = new DataGridViewComboBoxColumn ();
  729. _nameComboBoxColumn.HeaderText = "Name";
  730. _dataGridView.Columns.Add (_nameComboBoxColumn);
  731. // _firstNameTextBoxColumn
  732. _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
  733. _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  734. _firstNameTextBoxColumn.HeaderText = "First Name";
  735. _dataGridView.Columns.Add (_firstNameTextBoxColumn);
  736. // _chosenCheckBoxColumn
  737. _chosenCheckBoxColumn = new DataGridViewCheckBoxColumn ();
  738. _chosenCheckBoxColumn.HeaderText = "Chosen";
  739. _dataGridView.Columns.Add (_chosenCheckBoxColumn);
  740. // .NET requires that all possible values for combo-boxes in a column
  741. // are added to the column.
  742. _nameComboBoxColumn.Items.Add ("de Icaza");
  743. _nameComboBoxColumn.Items.Add ("Toshok");
  744. _nameComboBoxColumn.Items.Add ("Harper");
  745. _nameComboBoxColumn.Items.Add ("Boswell");
  746. // Set up the contents of the data-grid.
  747. _dataGridView.Rows.Add ("de Icaza", "Miguel", true);
  748. _dataGridView.Rows.Add ("Toshok", "Chris", false);
  749. _dataGridView.Rows.Add ("Harper", "Jackson", false);
  750. _dataGridView.Rows.Add ("Boswell", "Steven", true);
  751. // Edit a combo-box cell.
  752. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[0];
  753. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[0].Selected, "1-6");
  754. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-7");
  755. _dataGridView.CancelEdit();
  756. // Edit a text-box cell.
  757. _dataGridView.CurrentCell = _dataGridView.Rows[0].Cells[1];
  758. Assert.AreEqual (false, _dataGridView.Rows[3].Cells[0].Selected, "1-8");
  759. Assert.AreEqual (true, _dataGridView.Rows[0].Cells[1].Selected, "1-9");
  760. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-10");
  761. _dataGridView.CancelEdit();
  762. // Edit a check-box cell.
  763. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[2];
  764. Assert.AreEqual (false, _dataGridView.Rows[0].Cells[1].Selected, "1-11");
  765. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[2].Selected, "1-12");
  766. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-13");
  767. _dataGridView.CancelEdit();
  768. // Make sure the event-handler was called each time.
  769. // (DataGridViewCheckBoxCell isn't derived from Control, so the
  770. // EditingControlShowing event doesn't get called for it.)
  771. Assert.AreEqual (3, editingControlShowingTest_FoundColumns, "1-14");
  772. _dataGridView.Dispose();
  773. }
  774. }
  775. // A simple class, for testing the data-binding variant of the
  776. // editing-control-showing event.
  777. private class EcstRecord
  778. {
  779. string name;
  780. string firstName;
  781. bool chosen;
  782. public EcstRecord (string newName, string newFirstName, bool newChosen)
  783. {
  784. name = newName;
  785. firstName = newFirstName;
  786. chosen = newChosen;
  787. }
  788. public string Name
  789. {
  790. get { return name; }
  791. set { name = value; }
  792. }
  793. public string FirstName
  794. {
  795. get { return firstName; }
  796. set { firstName = value; }
  797. }
  798. public bool Chosen
  799. {
  800. get { return chosen; }
  801. set { chosen = value; }
  802. }
  803. };
  804. [Test] // Xamarin bug 5419
  805. public void EditingControlShowingTest_Bound ()
  806. {
  807. using (DataGridView _dataGridView = new DataGridView ()) {
  808. DataGridViewComboBoxColumn _nameComboBoxColumn;
  809. DataGridViewTextBoxColumn _firstNameTextBoxColumn;
  810. DataGridViewCheckBoxColumn _chosenCheckBoxColumn;
  811. _dataGridView.AutoGenerateColumns = false;
  812. // Add the event-handler.
  813. _dataGridView.EditingControlShowing
  814. += new DataGridViewEditingControlShowingEventHandler
  815. (DataGridView_EditingControlShowingTest);
  816. // No columns have been found in the event-handler yet.
  817. editingControlShowingTest_FoundColumns = 0;
  818. // _nameComboBoxColumn
  819. _nameComboBoxColumn = new DataGridViewComboBoxColumn ();
  820. _nameComboBoxColumn.HeaderText = "Name";
  821. _nameComboBoxColumn.DataPropertyName = "Name";
  822. _dataGridView.Columns.Add (_nameComboBoxColumn);
  823. // _firstNameTextBoxColumn
  824. _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
  825. _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  826. _firstNameTextBoxColumn.HeaderText = "First Name";
  827. _firstNameTextBoxColumn.DataPropertyName = "FirstName";
  828. _dataGridView.Columns.Add (_firstNameTextBoxColumn);
  829. // _chosenCheckBoxColumn
  830. _chosenCheckBoxColumn = new DataGridViewCheckBoxColumn ();
  831. _chosenCheckBoxColumn.HeaderText = "Chosen";
  832. _chosenCheckBoxColumn.DataPropertyName = "Chosen";
  833. _chosenCheckBoxColumn.FalseValue = "false";
  834. _chosenCheckBoxColumn.TrueValue = "true";
  835. _dataGridView.Columns.Add (_chosenCheckBoxColumn);
  836. // .NET requires that all possible values for combo-boxes in a column
  837. // are added to the column.
  838. _nameComboBoxColumn.Items.Add ("de Icaza");
  839. _nameComboBoxColumn.Items.Add ("Toshok");
  840. _nameComboBoxColumn.Items.Add ("Harper");
  841. _nameComboBoxColumn.Items.Add ("Boswell");
  842. // Set up the contents of the data-grid.
  843. BindingList<EcstRecord> boundData = new BindingList<EcstRecord> ();
  844. boundData.Add (new EcstRecord ("de Icaza", "Miguel", true));
  845. boundData.Add (new EcstRecord ("Toshok", "Chris", false));
  846. boundData.Add (new EcstRecord ("Harper", "Jackson", false));
  847. boundData.Add (new EcstRecord ("Boswell", "Steven", true));
  848. _dataGridView.DataSource = boundData;
  849. // For data binding to work, there needs to be a Form, apparently.
  850. Form form = new Form ();
  851. form.ShowInTaskbar = false;
  852. form.Controls.Add (_dataGridView);
  853. form.Show ();
  854. // Make sure the data-source took.
  855. // (Without the Form, instead of having four rows, the data grid
  856. // only has one row, and all its cell values are null.)
  857. Assert.AreEqual (boundData.Count, _dataGridView.Rows.Count, "1-6");
  858. // Edit a combo-box cell.
  859. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[0];
  860. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[0].Selected, "1-7");
  861. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-8");
  862. _dataGridView.CancelEdit();
  863. // Edit a text-box cell.
  864. _dataGridView.CurrentCell = _dataGridView.Rows[0].Cells[1];
  865. Assert.AreEqual (false, _dataGridView.Rows[3].Cells[0].Selected, "1-9");
  866. Assert.AreEqual (true, _dataGridView.Rows[0].Cells[1].Selected, "1-10");
  867. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-11");
  868. _dataGridView.CancelEdit();
  869. // Edit a check-box cell.
  870. _dataGridView.CurrentCell = _dataGridView.Rows[3].Cells[2];
  871. Assert.AreEqual (false, _dataGridView.Rows[0].Cells[1].Selected, "1-12");
  872. Assert.AreEqual (true, _dataGridView.Rows[3].Cells[2].Selected, "1-13");
  873. Assert.AreEqual (true, _dataGridView.BeginEdit (false), "1-14");
  874. _dataGridView.CancelEdit();
  875. // Make sure the event-handler was called each time.
  876. // (DataGridViewCheckBoxCell isn't derived from Control, so the
  877. // EditingControlShowing event doesn't get called for it.)
  878. Assert.AreEqual (3, editingControlShowingTest_FoundColumns, "1-14");
  879. // Get rid of the form.
  880. form.Close();
  881. }
  882. }
  883. [Test]
  884. public void bug_81918 ()
  885. {
  886. using (DataGridView dgv = new DataGridView ()) {
  887. DataGridViewColumn col = new DataGridViewComboBoxColumn ();
  888. dgv.Columns.Add (col);
  889. dgv.Rows.Add ("a");
  890. DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell) dgv [0, 0];
  891. }
  892. }
  893. // A custom data-grid-view, created solely so that
  894. // mouse clicks can be faked on it.
  895. private class ClickableDataGridView : DataGridView
  896. {
  897. public ClickableDataGridView ()
  898. : base ()
  899. {
  900. }
  901. internal void OnMouseDownInternal (MouseEventArgs e)
  902. {
  903. OnMouseDown (e);
  904. }
  905. internal void OnMouseUpInternal (MouseEventArgs e)
  906. {
  907. OnMouseUp (e);
  908. }
  909. };
  910. [Test]
  911. public void OneClickComboBoxCell ()
  912. {
  913. Form form = null;
  914. try
  915. {
  916. // Create a form, a text label, and a data-grid-view.
  917. form = new Form ();
  918. Label label = new Label ();
  919. label.Text = "Label";
  920. label.Parent = form;
  921. ClickableDataGridView dgv = new ClickableDataGridView ();
  922. dgv.Parent = form;
  923. // Create a combo-box column.
  924. DataGridViewComboBoxColumn cbCol = new DataGridViewComboBoxColumn ();
  925. cbCol.HeaderText = "Name";
  926. dgv.Columns.Add (cbCol);
  927. // .NET requires that all possible values for combo-boxes
  928. // in a column are added to the column.
  929. cbCol.Items.Add ("Item1");
  930. cbCol.Items.Add ("Item2");
  931. cbCol.Items.Add ("Item3");
  932. cbCol.Items.Add ("Item4");
  933. // Set up the contents of the data-grid.
  934. dgv.Rows.Add ("Item1");
  935. dgv.Rows.Add ("Item2");
  936. // Select the cell.
  937. dgv.CurrentCell = dgv.Rows[0].Cells[0];
  938. // Focus the data-grid-view. (Without this, its Leave
  939. // event won't get called when something outside of the
  940. // data-grid-view gets focused.)
  941. dgv.Focus ();
  942. // Show the form, let it draw.
  943. form.Show ();
  944. Application.DoEvents ();
  945. // Locate the drop-down button. (This code is taken from mono-winforms,
  946. // from the private method DataGridViewComboBoxCell.CalculateButtonArea(),
  947. // and was then hacked mercilessly.)
  948. Rectangle button_area = Rectangle.Empty;
  949. {
  950. int border = 3 /* ThemeEngine.Current.Border3DSize.Width */;
  951. const int button_width = 16;
  952. Rectangle text_area = dgv.GetCellDisplayRectangle (0, 0, false);
  953. button_area.X = text_area.Right - button_width - border;
  954. button_area.Y = text_area.Y + border;
  955. button_area.Width = button_width;
  956. button_area.Height = text_area.Height - 2 * border;
  957. }
  958. // Click on the drop-down button.
  959. int x = button_area.X + (button_area.Width / 2);
  960. int y = button_area.Y + (button_area.Height / 2);
  961. if (Environment.OSVersion.Platform == PlatformID.Win32NT
  962. && Type.GetType ("Mono.Runtime") == null)
  963. {
  964. // Calling OnMouseDownInternal () in Win32 doesn't work.
  965. // My best guess as to why is that the WinForms ComboBox
  966. // is a wrapper around the ComCtl control, e.g. similar
  967. // to the reason that Paint event-handlers don't work on
  968. // TreeView. So we go through all this rigamarole to
  969. // simulate a mouse click.
  970. // First, get the location of the desired mouse-click, in
  971. // data-grid-view coordinates.
  972. Win32Point ptGlobal = new Win32Point ();
  973. ptGlobal.x = x + dgv.Location.X;
  974. ptGlobal.y = y + dgv.Location.Y;
  975. // Convert that to screen coordinates.
  976. ClientToScreen (form.Handle, ref ptGlobal);
  977. // Move the mouse-pointer there. (Yes, this really appears
  978. // to be necessary.)
  979. SetCursorPos (ptGlobal.x, ptGlobal.y);
  980. // Convert screen coordinates to mouse coordinates.
  981. ptGlobal.x *= (65535 / SystemInformation.VirtualScreen.Width);
  982. ptGlobal.y *= (65535 / SystemInformation.VirtualScreen.Height);
  983. // Finally, fire a mouse-down and mouse-up event.
  984. mouse_event (MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE,
  985. ptGlobal.x, ptGlobal.y, 0, IntPtr.Zero);
  986. mouse_event (MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE,
  987. ptGlobal.x, ptGlobal.y, 0, IntPtr.Zero);
  988. // Let the system process these events.
  989. Application.DoEvents ();
  990. }
  991. else
  992. {
  993. // And this is how the same code is done under Linux.
  994. // (No one should wonder why I prefer Mono to MS Windows .NET ;-)
  995. MouseEventArgs me = new MouseEventArgs (MouseButtons.Left, 1, x, y, 0);
  996. DataGridViewCellMouseEventArgs cme = new DataGridViewCellMouseEventArgs (0, 0, x, y, me);
  997. dgv.OnMouseDownInternal (cme);
  998. dgv.OnMouseUpInternal (cme);
  999. }
  1000. // Make sure that created an editing control.
  1001. ComboBox cb = dgv.EditingControl as ComboBox;
  1002. Assert.AreNotEqual (null, cb, "1-1");
  1003. // Make sure that dropped down the menu.
  1004. Assert.AreEqual (true, cb.DroppedDown, "1-2");
  1005. // Close the menu.
  1006. cb.DroppedDown = false;
  1007. // Change the selection on the menu.
  1008. cb.SelectedIndex = 2 /* "Item3" */;
  1009. // Leave the data-grid-view.
  1010. label.Focus ();
  1011. // That should have ended editing and saved the value.
  1012. string cellValue = (string)(dgv.Rows[0].Cells[0].FormattedValue);
  1013. Assert.AreEqual ("Item3", cellValue, "1-3");
  1014. }
  1015. finally
  1016. {
  1017. if (form != null)
  1018. form.Close ();
  1019. }
  1020. }
  1021. // For testing row/column selection.
  1022. List<List<int>> selections;
  1023. void DataGridView_RowSelectionChanged (object sender, EventArgs e)
  1024. {
  1025. // Make a list of selected rows.
  1026. DataGridView dgv = sender as DataGridView;
  1027. List<int> selection = new List<int> ();
  1028. foreach (DataGridViewRow row in dgv.SelectedRows)
  1029. selection.Add (row.Index);
  1030. selections.Add (selection);
  1031. }
  1032. void DataGridView_ColumnSelectionChanged (object sender, EventArgs e)
  1033. {
  1034. // Make a list of selected columns.
  1035. DataGridView dgv = sender as DataGridView;
  1036. List<int> selection = new List<int> ();
  1037. foreach (DataGridViewColumn column in dgv.SelectedColumns)
  1038. selection.Add (column.Index);
  1039. selections.Add (selection);
  1040. }
  1041. // Used to generate printable representation of selections.
  1042. string ListListIntToString (List<List<int>> selections)
  1043. {
  1044. List<string> selectionsList = new List<string> ();
  1045. foreach (List<int> selection in selections)
  1046. {
  1047. List<string> selectionList = new List<string> ();
  1048. foreach (int selectionNo in selection)
  1049. selectionList.Add (selectionNo.ToString ("D"));
  1050. selectionsList.Add ("<" + string.Join (",", selectionList.ToArray()) + ">");
  1051. }
  1052. return string.Join (",", selectionsList.ToArray());
  1053. // (Here is the disallowed Linq version.)
  1054. /* return string.Join (",", (selections.Select ((List<int> x)
  1055. => "<" + string.Join (",", (x.Select ((int y)
  1056. => (y.ToString("D")))).ToArray()) + ">").ToArray())); */
  1057. }
  1058. [Test]
  1059. public void SelectedRowsTest ()
  1060. {
  1061. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1062. dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  1063. // Prepare to test the SelectionChanged event.
  1064. selections = new List<List<int>> ();
  1065. List<List<int>> expectedSelections = new List<List<int>> ();
  1066. dgv.SelectionChanged += new EventHandler (DataGridView_RowSelectionChanged);
  1067. // Make sure there's no selection to begin with.
  1068. Assert.AreEqual (0, dgv.SelectedRows.Count, "1-10");
  1069. // Select a row.
  1070. dgv.Rows [1].Selected = true;
  1071. Assert.AreEqual (1, dgv.SelectedRows.Count, "1-1");
  1072. Assert.AreEqual (1, dgv.SelectedRows [0].Index, "1-2");
  1073. expectedSelections.Add (new List<int> { 1 });
  1074. // Select another row.
  1075. dgv.Rows [3].Selected = true;
  1076. Assert.AreEqual (2, dgv.SelectedRows.Count, "1-3");
  1077. Assert.AreEqual (3, dgv.SelectedRows [0].Index, "1-4");
  1078. Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-5");
  1079. expectedSelections.Add (new List<int> { 3, 1 });
  1080. // Select another row.
  1081. dgv.Rows [2].Selected = true;
  1082. Assert.AreEqual (3, dgv.SelectedRows.Count, "1-6");
  1083. Assert.AreEqual (2, dgv.SelectedRows [0].Index, "1-7");
  1084. Assert.AreEqual (3, dgv.SelectedRows [1].Index, "1-8");
  1085. Assert.AreEqual (1, dgv.SelectedRows [2].Index, "1-9");
  1086. expectedSelections.Add (new List<int> { 2, 3, 1 });
  1087. // Unselect a row.
  1088. dgv.Rows [2].Selected = false;
  1089. Assert.AreEqual (2, dgv.SelectedRows.Count, "1-11");
  1090. Assert.AreEqual (3, dgv.SelectedRows [0].Index, "1-12");
  1091. Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-13");
  1092. expectedSelections.Add (new List<int> { 3, 1 });
  1093. // Delete a row.
  1094. // Since the row wasn't selected, it doesn't fire a
  1095. // SelectionChanged event.
  1096. dgv.Rows.RemoveAt (2);
  1097. Assert.AreEqual (2, dgv.SelectedRows.Count, "1-14");
  1098. Assert.AreEqual (2, dgv.SelectedRows [0].Index, "1-16");
  1099. Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-17");
  1100. // Delete a selected row.
  1101. dgv.Rows.RemoveAt (2);
  1102. Assert.AreEqual (1, dgv.SelectedRows.Count, "1-18");
  1103. Assert.AreEqual (1, dgv.SelectedRows [0].Index, "1-19");
  1104. expectedSelections.Add (new List<int> { 1 });
  1105. // Make sure the SelectionChanged event was called when expected.
  1106. string selectionsText = ListListIntToString (selections);
  1107. string expectedSelectionsText = ListListIntToString (expectedSelections);
  1108. Assert.AreEqual (expectedSelectionsText, selectionsText, "1-15");
  1109. }
  1110. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1111. dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
  1112. dgv.Rows [1].Selected = true;
  1113. Assert.AreEqual (0, dgv.SelectedRows.Count, "3-1");
  1114. dgv.Rows [3].Selected = true;
  1115. Assert.AreEqual (0, dgv.SelectedRows.Count, "3-3");
  1116. dgv.Rows [2].Selected = true;
  1117. Assert.AreEqual (0, dgv.SelectedRows.Count, "3-6");
  1118. }
  1119. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1120. foreach (DataGridViewColumn col in dgv.Col

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