PageRenderTime 71ms CodeModel.GetById 17ms RepoModel.GetById 1ms 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
  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.Columns)
  1121. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1122. dgv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
  1123. dgv.Rows [1].Selected = true;
  1124. Assert.AreEqual (0, dgv.SelectedRows.Count, "4-1");
  1125. dgv.Rows [3].Selected = true;
  1126. Assert.AreEqual (0, dgv.SelectedRows.Count, "4-3");
  1127. dgv.Rows [2].Selected = true;
  1128. Assert.AreEqual (0, dgv.SelectedRows.Count, "4-6");
  1129. }
  1130. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1131. foreach (DataGridViewColumn col in dgv.Columns)
  1132. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1133. dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
  1134. dgv.Rows [1].Selected = true;
  1135. Assert.AreEqual (0, dgv.SelectedRows.Count, "5-1");
  1136. dgv.Rows [3].Selected = true;
  1137. Assert.AreEqual (0, dgv.SelectedRows.Count, "5-3");
  1138. dgv.Rows [2].Selected = true;
  1139. Assert.AreEqual (0, dgv.SelectedRows.Count, "5-6");
  1140. }
  1141. }
  1142. [Test] // bug #325979
  1143. public void SelectedRows_FindColumnByName ()
  1144. {
  1145. DataTable dt = new DataTable ();
  1146. dt.Columns.Add ("Date", typeof (DateTime));
  1147. dt.Columns.Add ("Registered", typeof (bool));
  1148. dt.Columns.Add ("Event", typeof (string));
  1149. DataRow row = dt.NewRow ();
  1150. row ["Date"] = new DateTime (2007, 2, 3);
  1151. row ["Event"] = "one";
  1152. row ["Registered"] = false;
  1153. dt.Rows.Add (row);
  1154. row = dt.NewRow ();
  1155. row ["Date"] = new DateTime (2008, 3, 4);
  1156. row ["Event"] = "two";
  1157. row ["Registered"] = true;
  1158. dt.Rows.Add (row);
  1159. DataGridView dgv = new DataGridView ();
  1160. dgv.DataSource = dt;
  1161. Form form = new Form ();
  1162. form.ShowInTaskbar = false;
  1163. form.Controls.Add (dgv);
  1164. form.Show ();
  1165. dgv.Rows [1].Selected = true;
  1166. DataGridViewCell cell = dgv.SelectedRows [0].Cells ["DaTE"];
  1167. Assert.IsNotNull (cell, "#A1");
  1168. Assert.IsNotNull (cell.OwningColumn, "#A2");
  1169. Assert.AreEqual ("Date", cell.OwningColumn.Name, "#A3");
  1170. Assert.IsNotNull (cell.Value, "#A4");
  1171. Assert.AreEqual (new DateTime (2008, 3, 4), cell.Value, "#A5");
  1172. cell = dgv.SelectedRows [0].Cells ["Event"];
  1173. Assert.IsNotNull (cell, "#B1");
  1174. Assert.IsNotNull (cell.OwningColumn, "#B2");
  1175. Assert.AreEqual ("Event", cell.OwningColumn.Name, "#B3");
  1176. Assert.IsNotNull (cell.Value, "#B3");
  1177. Assert.AreEqual ("two", cell.Value, "#B4");
  1178. form.Dispose ();
  1179. }
  1180. [Test]
  1181. public void SelectedColumnsTest ()
  1182. {
  1183. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1184. foreach (DataGridViewColumn col in dgv.Columns)
  1185. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1186. dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
  1187. // Prepare to test the SelectionChanged event.
  1188. selections = new List<List<int>> ();
  1189. List<List<int>> expectedSelections = new List<List<int>> ();
  1190. dgv.SelectionChanged += new EventHandler (DataGridView_ColumnSelectionChanged);
  1191. // Make sure there's no selection to begin with.
  1192. Assert.AreEqual (0, dgv.SelectedColumns.Count, "1-13");
  1193. // Select a column.
  1194. dgv.Columns [1].Selected = true;
  1195. Assert.AreEqual (1, dgv.SelectedColumns.Count, "1-1");
  1196. Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "1-2");
  1197. expectedSelections.Add (new List<int> { 1 });
  1198. // Select another column.
  1199. dgv.Columns [3].Selected = true;
  1200. Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-3");
  1201. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "1-4");
  1202. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-5");
  1203. expectedSelections.Add (new List<int> { 3, 1 });
  1204. // Select another column.
  1205. dgv.Columns [2].Selected = true;
  1206. Assert.AreEqual (3, dgv.SelectedColumns.Count, "1-6");
  1207. Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "1-7");
  1208. Assert.AreEqual (3, dgv.SelectedColumns [1].Index, "1-8");
  1209. Assert.AreEqual (1, dgv.SelectedColumns [2].Index, "1-9");
  1210. expectedSelections.Add (new List<int> { 2, 3, 1 });
  1211. // Unselect a column.
  1212. dgv.Columns [2].Selected = false;
  1213. Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-10");
  1214. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "1-11");
  1215. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-12");
  1216. expectedSelections.Add (new List<int> { 3, 1 });
  1217. // Delete a column.
  1218. // Since the column wasn't selected, it doesn't fire a
  1219. // SelectionChanged event.
  1220. dgv.Columns.RemoveAt (2);
  1221. Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-14");
  1222. Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "1-16");
  1223. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-17");
  1224. // Delete a selected column.
  1225. dgv.Columns.RemoveAt (2);
  1226. Assert.AreEqual (1, dgv.SelectedColumns.Count, "1-18");
  1227. Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "1-19");
  1228. expectedSelections.Add (new List<int> { 1 });
  1229. // Make sure the SelectionChanged event was called when expected.
  1230. string selectionsText = ListListIntToString (selections);
  1231. string expectedSelectionsText = ListListIntToString (expectedSelections);
  1232. Assert.AreEqual (expectedSelectionsText, selectionsText, "1-15");
  1233. }
  1234. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1235. foreach (DataGridViewColumn col in dgv.Columns)
  1236. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1237. dgv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
  1238. // Prepare to test the SelectionChanged event.
  1239. selections = new List<List<int>> ();
  1240. List<List<int>> expectedSelections = new List<List<int>> ();
  1241. dgv.SelectionChanged += new EventHandler (DataGridView_ColumnSelectionChanged);
  1242. // Make sure there's no selection to begin with.
  1243. Assert.AreEqual (0, dgv.SelectedColumns.Count, "2-10");
  1244. // Select a column.
  1245. dgv.Columns [1].Selected = true;
  1246. Assert.AreEqual (1, dgv.SelectedColumns.Count, "2-1");
  1247. Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "2-2");
  1248. expectedSelections.Add (new List<int> { 1 });
  1249. // Select another column.
  1250. dgv.Columns [3].Selected = true;
  1251. Assert.AreEqual (2, dgv.SelectedColumns.Count, "2-3");
  1252. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "2-4");
  1253. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "2-5");
  1254. expectedSelections.Add (new List<int> { 3, 1 });
  1255. // Select another column.
  1256. dgv.Columns [2].Selected = true;
  1257. Assert.AreEqual (3, dgv.SelectedColumns.Count, "2-6");
  1258. Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "2-7");
  1259. Assert.AreEqual (3, dgv.SelectedColumns [1].Index, "2-8");
  1260. Assert.AreEqual (1, dgv.SelectedColumns [2].Index, "2-9");
  1261. expectedSelections.Add (new List<int> { 2, 3, 1 });
  1262. // Unselect another column.
  1263. dgv.Columns [2].Selected = false;
  1264. Assert.AreEqual (2, dgv.SelectedColumns.Count, "2-11");
  1265. Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "2-12");
  1266. Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "2-13");
  1267. expectedSelections.Add (new List<int> { 3, 1 });
  1268. // Make sure the SelectionChanged event was called when expected.
  1269. string selectionsText = ListListIntToString (selections);
  1270. string expectedSelectionsText = ListListIntToString (expectedSelections);
  1271. Assert.AreEqual (expectedSelectionsText, selectionsText, "2-14");
  1272. }
  1273. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1274. dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
  1275. dgv.Columns [1].Selected = true;
  1276. Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-1");
  1277. dgv.Columns [3].Selected = true;
  1278. Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-3");
  1279. dgv.Columns [2].Selected = true;
  1280. Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-6");
  1281. }
  1282. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1283. foreach (DataGridViewColumn col in dgv.Columns)
  1284. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1285. dgv.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
  1286. dgv.Columns [1].Selected = true;
  1287. Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-1");
  1288. dgv.Columns [3].Selected = true;
  1289. Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-3");
  1290. dgv.Columns [2].Selected = true;
  1291. Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-6");
  1292. }
  1293. using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
  1294. foreach (DataGridViewColumn col in dgv.Columns)
  1295. col.SortMode = DataGridViewColumnSortMode.NotSortable;
  1296. dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
  1297. dgv.Columns [1].Selected = true;
  1298. Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-1");
  1299. dgv.Columns [3].Selected = true;
  1300. Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-3");
  1301. dgv.Columns [2].Selected = true;
  1302. Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-6");
  1303. }
  1304. }
  1305. [Test]
  1306. public void TopLeftHeaderCellTest ()
  1307. {
  1308. Assert.Ignore("Missing quite a few bits still");
  1309. using (DataGridView dgv = new DataGridView ()) {
  1310. DataGridViewHeaderCell cell = dgv.TopLeftHeaderCell;
  1311. cell = dgv.TopLeftHeaderCell;
  1312. Assert.IsNotNull (cell, "#01");
  1313. Assert.AreEqual (cell.DataGridView, dgv, "#02");
  1314. Assert.AreEqual ("DataGridViewTopLeftHeaderCell", cell.GetType ().Name, "#03");
  1315. Assert.IsNotNull (cell.AccessibilityObject, "#cell.AccessibilityObject");
  1316. Assert.AreEqual (-1, cell.ColumnIndex, "#cell.ColumnIndex");
  1317. // /* NIE for the moment... */ Assert.IsNotNull (cell.ContentBounds, "#cell.ContentBounds");
  1318. Assert.IsNull (cell.ContextMenuStrip, "#cell.ContextMenuStrip");
  1319. Assert.IsNotNull (cell.DataGridView, "#cell.DataGridView");
  1320. Assert.IsNull (cell.DefaultNewRowValue, "#cell.DefaultNewRowValue");
  1321. Assert.AreEqual (false, cell.Displayed, "#cell.Displayed");
  1322. // /* NIE for the moment... */ Assert.AreEqual (@"", cell.EditedFormattedValue, "#cell.EditedFormattedValue");
  1323. Assert.IsNotNull (cell.EditType, "#cell.EditType");
  1324. Assert.IsNotNull (cell.ErrorIconBounds, "#cell.ErrorIconBounds");
  1325. Assert.AreEqual (@"", cell.ErrorText, "#cell.ErrorText");
  1326. // /* NIE for the moment... */ Assert.AreEqual (@"", cell.FormattedValue, "#cell.FormattedValue");
  1327. Assert.IsNotNull (cell.FormattedValueType, "#cell.FormattedValueType");
  1328. // /* NIE for the moment... */ Assert.AreEqual (true, cell.Frozen, "#cell.Frozen");
  1329. Assert.AreEqual (false, cell.HasStyle, "#cell.HasStyle");
  1330. Assert.AreEqual (DataGridViewElementStates.Frozen | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Resizable | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Visible, cell.InheritedState, "#cell.InheritedState");
  1331. Assert.IsNotNull (cell.InheritedStyle, "#cell.InheritedStyle");
  1332. try {
  1333. object zxf = cell.IsInEditMode;
  1334. TestHelper.RemoveWarning (zxf);
  1335. Assert.Fail ("Expected 'System.InvalidOperationException', but no exception was thrown.", "#cell.IsInEditMode");
  1336. } catch (InvalidOperationException ex) {
  1337. Assert.AreEqual (@"Operation cannot be performed on a cell of a shared row.", ex.Message);
  1338. } catch (Exception ex) {
  1339. Assert.Fail ("Expected 'System.InvalidOperationException', got '" + ex.GetType ().FullName + "'.", "#cell.IsInEditMode");
  1340. }
  1341. Assert.IsNull (cell.OwningColumn, "#cell.OwningColumn");
  1342. Assert.IsNull (cell.OwningRow, "#cell.OwningRow");
  1343. Assert.IsNotNull (cell.PreferredSize, "#cell.PreferredSize");
  1344. Assert.AreEqual (true, cell.ReadOnly, "#cell.ReadOnly");
  1345. Assert.AreEqual (true, cell.Resizable, "#cell.Resizable");
  1346. Assert.AreEqual (-1, cell.RowIndex, "#cell.RowIndex");
  1347. Assert.AreEqual (false, cell.Selected, "#cell.Selected");
  1348. Assert.IsNotNull (cell.Size, "#cell.Size");
  1349. Assert.AreEqual (DataGridViewElementStates.None, cell.State, "#cell.State");
  1350. if (cell.HasStyle)
  1351. Assert.IsNotNull (cell.Style, "#cell.Style");
  1352. Assert.IsNull (cell.Tag, "#cell.Tag");
  1353. Assert.AreEqual (@"", cell.ToolTipText, "#cell.ToolTipText");
  1354. Assert.IsNull (cell.Value, "#cell.Value");
  1355. Assert.IsNotNull (cell.ValueType, "#cell.ValueType");
  1356. Assert.AreEqual (true, cell.Visible, "#cell.Visible");
  1357. }
  1358. }
  1359. [Test]
  1360. public void TestDefaultValues ()
  1361. {
  1362. DataGridView grid = new DataGridView ();
  1363. Assert.AreEqual (true, grid.AllowUserToAddRows, "#A1");
  1364. Assert.AreEqual (true, grid.AllowUserToDeleteRows, "#A2");
  1365. Assert.AreEqual (false, grid.AllowUserToOrderColumns, "#A3");
  1366. Assert.AreEqual (true, grid.AllowUserToResizeColumns, "#A4");
  1367. Assert.AreEqual (true, grid.AllowUserToResizeRows, "#A5");
  1368. Assert.AreEqual (new DataGridViewCellStyle(), grid.AlternatingRowsDefaultCellStyle, "#A6");
  1369. Assert.AreEqual (true, grid.AutoGenerateColumns, "#A7");
  1370. Assert.AreEqual (DataGridViewAutoSizeRowsMode.None, grid.AutoSizeRowsMode, "#A8");
  1371. Assert.AreEqual (Control.DefaultBackColor, grid.BackColor, "#A9");
  1372. Assert.AreEqual (SystemColors.AppWorkspace, grid.BackgroundColor, "#A10");
  1373. Assert.AreEqual (BorderStyle.FixedSingle, grid.BorderStyle, "#A11");
  1374. Assert.AreEqual (DataGridViewClipboardCopyMode.EnableWithAutoHeaderText, grid.ClipboardCopyMode, "#A12");
  1375. Assert.AreEqual (DataGridViewColumnHeadersHeightSizeMode.EnableResizing, grid.ColumnHeadersHeightSizeMode, "#A21");
  1376. Assert.AreEqual (true, grid.ColumnHeadersVisible, "#A22");
  1377. Assert.AreEqual (String.Empty, grid.DataMember, "#A23");
  1378. Assert.AreEqual (DataGridViewEditMode.EditOnKeystrokeOrF2, grid.EditMode, "#A31");
  1379. Assert.AreEqual (Control.DefaultFont, grid.Font, "#A32");
  1380. Assert.AreEqual (Control.DefaultForeColor, grid.ForeColor, "#A33");
  1381. Assert.AreEqual (Color.FromKnownColor(KnownColor.ControlDark), grid.GridColor, "#A34");
  1382. Assert.AreEqual (true, grid.MultiSelect, "#A35");
  1383. Assert.AreEqual (grid.Rows.Count - 1, grid.NewRowIndex, "#A36");
  1384. Assert.AreEqual (Padding.Empty, grid.Padding, "#A37");
  1385. Assert.AreEqual (false, grid.ReadOnly, "#A38");
  1386. Assert.AreEqual (true, grid.RowHeadersVisible, "#A39");
  1387. Assert.AreEqual (41, grid.RowHeadersWidth, "#A40");
  1388. Assert.AreEqual (DataGridViewSelectionMode.RowHeaderSelect, grid.SelectionMode, "#A41");
  1389. Assert.AreEqual (true, grid.ShowCellErrors, "#A42");
  1390. Assert.AreEqual (true, grid.ShowEditingIcon, "#A43");
  1391. Assert.AreEqual (Cursors.Default, grid.UserSetCursor, "#A44");
  1392. Assert.AreEqual (false, grid.VirtualMode, "#A45");
  1393. }
  1394. #region AutoSizeColumnsModeExceptions
  1395. [Test]
  1396. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1397. public void TestAutoSizeColumnsModeInvalidEnumArgumentException ()
  1398. {
  1399. DataGridView grid = new DataGridView();
  1400. grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill | DataGridViewAutoSizeColumnsMode.None;
  1401. }
  1402. [Test]
  1403. [ExpectedException (typeof (InvalidOperationException))]
  1404. public void TestAutoSizeColumnsModeInvalidOperationException1 ()
  1405. {
  1406. DataGridView grid = new DataGridView ();
  1407. grid.ColumnHeadersVisible = false;
  1408. DataGridViewColumn col = new DataGridViewColumn ();
  1409. col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
  1410. grid.Columns.Add (col);
  1411. grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
  1412. }
  1413. [Test]
  1414. [ExpectedException (typeof (InvalidOperationException))]
  1415. public void TestAutoSizeColumnsModeInvalidOperationException2 ()
  1416. {
  1417. DataGridView grid = new DataGridView ();
  1418. DataGridViewColumn col = new DataGridViewColumn ();
  1419. col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
  1420. col.Frozen = true;
  1421. grid.Columns.Add (col);
  1422. grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
  1423. }
  1424. #endregion
  1425. #region AutoSizeRowsModeExceptions
  1426. [Test]
  1427. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1428. public void TestAutoSizeRowsModeInvalidEnumArgumentException ()
  1429. {
  1430. DataGridView grid = new DataGridView ();
  1431. grid.AutoSizeRowsMode = (DataGridViewAutoSizeRowsMode) 4;
  1432. }
  1433. [Test]
  1434. [ExpectedException (typeof (InvalidOperationException))]
  1435. public void TestAutoSizeRowsModeInvalidOperationException1 ()
  1436. {
  1437. DataGridView grid = new DataGridView ();
  1438. grid.RowHeadersVisible = false;
  1439. grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllHeaders;
  1440. }
  1441. [Test]
  1442. [ExpectedException (typeof (InvalidOperationException))]
  1443. public void TestAutoSizeRowsModeInvalidOperationException2 ()
  1444. {
  1445. DataGridView grid = new DataGridView ();
  1446. grid.RowHeadersVisible = false;
  1447. grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedHeaders;
  1448. }
  1449. #endregion
  1450. [Test]
  1451. public void AutoResizeColumTest ()
  1452. {
  1453. using (Form f = new Form ()) {
  1454. f.Show ();
  1455. using (DataGridView dgv = new DataGridView ()) {
  1456. f.Controls.Add (dgv);
  1457. DataGridViewColumn col, col2, col3;
  1458. Assert.AreEqual ("{Width=240, Height=150}", dgv.ClientSize.ToString (), "#01");
  1459. col = new DataGridViewColumn ();
  1460. col.MinimumWidth = 20;
  1461. col.FillWeight = 20;
  1462. col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  1463. col.CellTemplate = new DataGridViewTextBoxCell ();
  1464. Assert.AreEqual (100, col.Width, "#02");
  1465. dgv.Columns.Add (col);
  1466. Assert.AreEqual (197, col.Width, "#03");
  1467. col2 = new DataGridViewColumn ();
  1468. col2.MinimumWidth = 20;
  1469. col2.FillWeight = 40;
  1470. col2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  1471. col2.CellTemplate = new DataGridViewTextBoxCell (); ;
  1472. dgv.Columns.Add (col2);
  1473. Assert.AreEqual (66, col.Width, "#04");
  1474. Assert.AreEqual (131, col2.Width, "#05");
  1475. col3 = new DataGridViewColumn ();
  1476. col3.MinimumWidth = 20;
  1477. col3.FillWeight = 5;
  1478. col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
  1479. col3.CellTemplate = new DataGridViewTextBoxCell (); ;
  1480. dgv.Columns.Add (col3);
  1481. Assert.AreEqual (59, col.Width, "#04");
  1482. Assert.AreEqual (118, col2.Width, "#05");
  1483. Assert.AreEqual (20, col3.Width, "#05");
  1484. }
  1485. }
  1486. }
  1487. [Test]
  1488. public void ControlsTest ()
  1489. {
  1490. using (DataGridView grid = new DataGridView ()) {
  1491. Assert.AreEqual ("DataGridViewControlCollection", grid.Controls.GetType ().Name, "#01");
  1492. Assert.AreEqual (2, grid.Controls.Count, "#02");
  1493. }
  1494. }
  1495. [Test]
  1496. [ExpectedException (typeof (ArgumentException))]
  1497. public void TestBackgroundColorArgumentException ()
  1498. {
  1499. DataGridView grid = new DataGridView ();
  1500. grid.BackgroundColor = Color.Empty;
  1501. }
  1502. [Test]
  1503. [ExpectedException(typeof(InvalidEnumArgumentException))]
  1504. public void TestBorderStyleInvalidEnumArgumentException ()
  1505. {
  1506. DataGridView grid = new DataGridView ();
  1507. grid.BorderStyle = BorderStyle.FixedSingle | BorderStyle.Fixed3D;
  1508. }
  1509. [Test]
  1510. public void ColumnCount ()
  1511. {
  1512. DataGridView dgv = new DataGridView ();
  1513. dgv.RowCount = 10;
  1514. dgv.ColumnCount = 2;
  1515. Assert.AreEqual (10, dgv.RowCount, "A1");
  1516. Assert.AreEqual (2, dgv.ColumnCount, "A2");
  1517. dgv.ColumnCount = 1;
  1518. Assert.AreEqual (10, dgv.RowCount, "B1");
  1519. Assert.AreEqual (1, dgv.ColumnCount, "B2");
  1520. dgv.ColumnCount = 3;
  1521. Assert.AreEqual (10, dgv.RowCount, "C1");
  1522. Assert.AreEqual (3, dgv.ColumnCount, "C2");
  1523. dgv.ColumnCount = 0;
  1524. Assert.AreEqual (0, dgv.RowCount, "D1");
  1525. Assert.AreEqual (0, dgv.ColumnCount, "D2");
  1526. Assert.AreEqual (0, dgv.ColumnCount, "E1");
  1527. try {
  1528. dgv.ColumnCount = -1;
  1529. Assert.Fail ("F1");
  1530. } catch (ArgumentOutOfRangeException ex) {
  1531. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "F2");
  1532. Assert.IsNotNull (ex.Message, "F3");
  1533. Assert.IsNotNull (ex.ParamName, "F4");
  1534. Assert.AreEqual ("ColumnCount", ex.ParamName, "F5");
  1535. Assert.IsNull (ex.InnerException, "F6");
  1536. }
  1537. }
  1538. [Test]
  1539. public void ColumnCountIncrease ()
  1540. {
  1541. DataGridView dgv = new DataGridView ();
  1542. dgv.ColumnCount = 1;
  1543. // Increasing the ColumnCount adds TextBoxColumns, not generic columns
  1544. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[0].GetType ().ToString (), "A1");
  1545. }
  1546. [Test]
  1547. public void ColumnCountDecrease ()
  1548. {
  1549. DataGridView dgv = new DataGridView ();
  1550. dgv.ColumnCount = 6;
  1551. Assert.AreEqual (6, dgv.ColumnCount, "A1");
  1552. dgv.ColumnCount = 3;
  1553. Assert.AreEqual (3, dgv.ColumnCount, "A2");
  1554. // Increasing the ColumnCount adds TextBoxColumns, not generic columns
  1555. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[0].GetType ().ToString (), "A3");
  1556. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[1].GetType ().ToString (), "A4");
  1557. Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[2].GetType ().ToString (), "A5");
  1558. }
  1559. private class DataItem
  1560. {
  1561. public string Text {
  1562. get { return String.Empty; }
  1563. }
  1564. [Browsable (false)]
  1565. public string NotVisible {
  1566. get { return String.Empty; }
  1567. }
  1568. }
  1569. [Test]
  1570. public void NoDuplicateAutoGeneratedColumn ()
  1571. {
  1572. List<DataItem> dataList = new List<DataItem> ();
  1573. dataList.Add (new DataItem ());
  1574. dataList.Add (new DataItem ());
  1575. DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn ();
  1576. column.DataPropertyName = "Text";
  1577. column.HeaderText = "Custom Column";
  1578. grid.Columns.Add (column);
  1579. grid.DataSource = dataList;
  1580. // Test that the column autogeneration hasn't generated duplicate column
  1581. // for the property Text
  1582. Assert.AreEqual (1, grid.Columns.Count, "#1");
  1583. Assert.AreEqual ("Custom Column", grid.Columns[0].HeaderText, "#2");
  1584. }
  1585. [Test]
  1586. [ExpectedException (typeof (InvalidOperationException))]
  1587. public void TestColumnCountInvalidOperationException ()
  1588. {
  1589. DataGridView grid = new DataGridView ();
  1590. grid.DataSource = new ArrayList ();
  1591. grid.ColumnCount = 0;
  1592. }
  1593. [Test]
  1594. public void ColumnHeadersHeight ()
  1595. {
  1596. DataGridView grid = new DataGridView ();
  1597. Assert.AreEqual (23, grid.ColumnHeadersHeight, "#A1");
  1598. grid.ColumnHeadersHeight = 4;
  1599. Assert.AreEqual (4, grid.ColumnHeadersHeight, "#A2");
  1600. grid.ColumnHeadersHeight = 32768;
  1601. Assert.AreEqual (32768, grid.ColumnHeadersHeight, "#A3");
  1602. try {
  1603. grid.ColumnHeadersHeight = 3;
  1604. Assert.Fail ("#B1");
  1605. } catch (ArgumentOutOfRangeException ex) {
  1606. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  1607. Assert.IsNotNull (ex.Message, "#B3");
  1608. Assert.IsNotNull (ex.ParamName, "#B4");
  1609. Assert.AreEqual ("ColumnHeadersHeight", ex.ParamName, "#B5");
  1610. Assert.IsNull (ex.InnerException, "#B6");
  1611. }
  1612. try {
  1613. grid.ColumnHeadersHeight = 32769;
  1614. Assert.Fail ("#C1");
  1615. } catch (ArgumentOutOfRangeException ex) {
  1616. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  1617. Assert.IsNotNull (ex.Message, "#C3");
  1618. Assert.IsNotNull (ex.ParamName, "#C4");
  1619. Assert.AreEqual ("ColumnHeadersHeight", ex.ParamName, "#C5");
  1620. Assert.IsNull (ex.InnerException, "#C6");
  1621. }
  1622. }
  1623. [Test]
  1624. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1625. public void TestColumnHeadersHeightSizeModeInvalidEnumArgumentException ()
  1626. {
  1627. DataGridView grid = new DataGridView ();
  1628. grid.ColumnHeadersHeightSizeMode = (DataGridViewColumnHeadersHeightSizeMode) 3;
  1629. }
  1630. [Test]
  1631. public void RowHeadersWidth ()
  1632. {
  1633. DataGridView grid = new DataGridView();
  1634. Assert.AreEqual (41, grid.RowHeadersWidth, "#A1");
  1635. grid.RowHeadersWidth = 4;
  1636. Assert.AreEqual (4, grid.RowHeadersWidth, "#A2");
  1637. grid.RowHeadersWidth = 32768;
  1638. Assert.AreEqual (32768, grid.RowHeadersWidth, "#A3");
  1639. try {
  1640. grid.RowHeadersWidth = 3;
  1641. Assert.Fail ("#B1");
  1642. } catch (ArgumentOutOfRangeException ex) {
  1643. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
  1644. Assert.IsNotNull (ex.Message, "#B3");
  1645. Assert.IsNotNull (ex.ParamName, "#B4");
  1646. Assert.AreEqual ("RowHeadersWidth", ex.ParamName, "#B5");
  1647. Assert.IsNull (ex.InnerException, "#B6");
  1648. }
  1649. try {
  1650. grid.RowHeadersWidth = 32769;
  1651. Assert.Fail ("#C1");
  1652. } catch (ArgumentOutOfRangeException ex) {
  1653. Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
  1654. Assert.IsNotNull (ex.Message, "#C3");
  1655. Assert.IsNotNull (ex.ParamName, "#C4");
  1656. Assert.AreEqual ("RowHeadersWidth", ex.ParamName, "#C5");
  1657. Assert.IsNull (ex.InnerException, "#C6");
  1658. }
  1659. }
  1660. [Test]
  1661. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1662. public void TestDataGridViewRowHeadersWidthSizeModeInvalidEnumArgumentException () {
  1663. DataGridView grid = new DataGridView ();
  1664. grid.RowHeadersWidthSizeMode = (DataGridViewRowHeadersWidthSizeMode) 5;
  1665. }
  1666. [Test]
  1667. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1668. public void TestScrollBarsInvalidEnumArgumentException ()
  1669. {
  1670. DataGridView grid = new DataGridView ();
  1671. grid.ScrollBars = (ScrollBars) 4;
  1672. }
  1673. [Test]
  1674. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1675. public void TestSelectionModeInvalidEnumArgumentException ()
  1676. {
  1677. DataGridView grid = new DataGridView ();
  1678. grid.SelectionMode = (DataGridViewSelectionMode) 5;
  1679. }
  1680. [Test]
  1681. [ExpectedException (typeof (InvalidEnumArgumentException))]
  1682. public void TestAutoResizeRowsInvalidEnumArgumentException ()
  1683. {
  1684. DataGridView grid = new DataGridView ();
  1685. grid.AutoResizeRows ((DataGridViewAutoSizeRowsMode) 4);
  1686. }
  1687. [Test]
  1688. [ExpectedException (typeof (InvalidOperationException))]
  1689. public void TestAutoResizeRowsInvalidOperationException1 ()
  1690. {
  1691. DataGridView grid = new DataGridView ();
  1692. grid.RowHeadersVisible = false;
  1693. grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.AllHeaders);
  1694. }
  1695. [Test]
  1696. [ExpectedException (typeof (InvalidOperationException))]
  1697. public void TestAutoResizeRowsInvalidOperationException2 ()
  1698. {
  1699. DataGridView grid = new DataGridView ();
  1700. grid.RowHeadersVisible = false;
  1701. grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.DisplayedHeaders);
  1702. }
  1703. [Test]
  1704. [ExpectedException (typeof (ArgumentException))]
  1705. public void TestAutoResizeRowsArgumentException ()
  1706. {
  1707. DataGridView grid = new DataGridView ();
  1708. grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.None);
  1709. }
  1710. [Test]
  1711. public void DefaultSize ()
  1712. {
  1713. MockDataGridView grid = new MockDataGridView ();
  1714. Assert.AreEqual (new Size (240, 150), grid.default_size, "#1");
  1715. Assert.AreEqual (new Size (240, 150), grid.Size, "#2");
  1716. }
  1717. [Test]
  1718. public void ColumnHeadersDefaultCellStyle ()
  1719. {
  1720. DataGridView grid = new DataGridView();
  1721. Assert.AreEqual (SystemColors.Control, grid.ColumnHeadersDefaultCellStyle.BackColor, "#A1");
  1722. Assert.AreEqual (SystemColors.ControlText, grid.ColumnHeadersDefaultCellStyle.ForeColor, "#A2");
  1723. Assert.AreEqual (SystemColors.Highlight, grid.ColumnHeadersDefaultCellStyle.SelectionBackColor, "#A3");
  1724. Assert.AreEqual (SystemColors.HighlightText, grid.ColumnHeadersDefaultCellStyle.SelectionForeColor, "#A4");
  1725. Assert.AreSame (grid.Font, grid.ColumnHeadersDefaultCellStyle.Font, "#A5");
  1726. Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.ColumnHeadersDefaultCellStyle.Alignment, "#A6");
  1727. Assert.AreEqual (DataGridViewTriState.True, grid.ColumnHeadersDefaultCellStyle.WrapMode, "#A7");
  1728. }
  1729. [Test]
  1730. public void DefaultCellStyle ()
  1731. {
  1732. DataGridView grid = new DataGridView();
  1733. Assert.AreEqual (SystemColors.Window, grid.DefaultCellStyle.BackColor, "#A1");
  1734. Assert.AreEqual (SystemColors.WindowText, grid.DefaultCellStyle.ForeColor, "#A2");
  1735. Assert.AreEqual (SystemColors.Highlight, grid.DefaultCellStyle.SelectionBackColor, "#A3");
  1736. Assert.AreEqual (SystemColors.HighlightText, grid.DefaultCellStyle.SelectionForeColor, "#A4");
  1737. Assert.AreSame (grid.Font, grid.DefaultCellStyle.Font, "#A5");
  1738. Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.DefaultCellStyle.Alignment, "#A6");
  1739. Assert.AreEqual (DataGridViewTriState.False, grid.DefaultCellStyle.WrapMode, "#A7");
  1740. }
  1741. [Test]
  1742. public void MethodIsInputKey ()
  1743. {
  1744. string result = string.Empty;
  1745. string expected = "13;13;33;33;34;34;35;36;37;38;39;40;46;48;96;113;";
  1746. ExposeProtectedProperties dgv = new ExposeProtectedProperties ();
  1747. foreach (Keys k in Enum.GetValues (typeof (Keys)))
  1748. if (dgv.PublicIsInputKey (k))
  1749. result += ((int)k).ToString () + ";";
  1750. Assert.AreEqual (expected, result, "A1");
  1751. }
  1752. [Test]
  1753. public void MethodIsInputChar ()
  1754. {
  1755. bool result = false;
  1756. ExposeProtectedProperties dgv = new ExposeProtectedProperties ();
  1757. for (int i = 0; i < 255; i++)
  1758. if (!dgv.PublicIsInputChar ((char)i))
  1759. result = true;
  1760. // Basically, it always returns true
  1761. Assert.AreEqual (false, result, "A1");
  1762. }
  1763. [Test]
  1764. public void RowsDefaultCellStyle ()
  1765. {
  1766. DataGridView grid = new DataGridView();
  1767. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.BackColor, "#A1");
  1768. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.ForeColor, "#A2");
  1769. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.SelectionBackColor, "#A3");
  1770. Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.SelectionForeColor, "#A4");
  1771. Assert.IsNull(grid.RowsDefaultCellStyle.Font, "#A5");
  1772. Assert.AreEqual (DataGridViewContentAlignment.NotSet, grid.RowsDefaultCellStyle.Alignment, "#A6");
  1773. Assert.AreEqual (DataGridViewTriState.NotSet, grid.RowsDefaultCellStyle.WrapMode, "#A7");
  1774. }
  1775. [Test]
  1776. public void RowHeadersDefaultCellStyle ()
  1777. {
  1778. DataGridView grid = new DataGridView();
  1779. Assert.AreEqual (SystemColors.Control, grid.RowHeadersDefaultCellStyle.BackColor, "#A1");
  1780. Assert.AreEqual (SystemColors.ControlText, grid.RowHeadersDefaultCellStyle.ForeColor, "#A2");
  1781. Assert.AreEqual (SystemColors.Highlight, grid.RowHeadersDefaultCellStyle.SelectionBackColor, "#A3");
  1782. Assert.AreEqual (SystemColors.HighlightText, grid.RowHeadersDefaultCellStyle.SelectionForeColor, "#A4");
  1783. Assert.AreSame (grid.Font, grid.RowHeadersDefaultCellStyle.Font, "#A5");
  1784. Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.RowHeadersDefaultCellStyle.Alignment, "#A6");
  1785. Assert.AreEqual (DataGridViewTriState.True, grid.RowHeadersDefaultCellStyle.WrapMode, "#A7");
  1786. }
  1787. private class MockDataGridView : DataGridView
  1788. {
  1789. public Size default_size {
  1790. get { return base.DefaultSize; }
  1791. }
  1792. }
  1793. [Test]
  1794. public void bug_82326 ()
  1795. {
  1796. using (Form f = new Form ()) {
  1797. DataGridView _dataGrid;
  1798. DataGridViewTextBoxColumn _column;
  1799. _dataGrid = new DataGridView ();
  1800. _column = new DataGridViewTextBoxColumn ();
  1801. f.SuspendLayout ();
  1802. ((ISupportInitialize)(_dataGrid)).BeginInit ();
  1803. //
  1804. // _dataGrid
  1805. //
  1806. _dataGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
  1807. _dataGrid.Columns.Add (_column);
  1808. _dataGrid.RowTemplate.Height = 21;
  1809. _dataGrid.Location = new Point (12, 115);
  1810. _dataGrid.Size = new Size (268, 146);
  1811. _dataGrid.TabIndex = 0;
  1812. //
  1813. // _column
  1814. //
  1815. _column.HeaderText = "Column";
  1816. //
  1817. // MainForm
  1818. //
  1819. f.ClientSize = new Size (292, 273);
  1820. f.Controls.Add (_dataGrid);
  1821. ((ISupportInitialize)(_dataGrid)).EndInit ();
  1822. f.ResumeLayout (false);
  1823. f.Load += delegate (object sender, EventArgs e) { ((Control)sender).FindForm ().Close (); };
  1824. Application.Run (f);
  1825. }
  1826. }
  1827. [Test]
  1828. public void RowCountIncrease ()
  1829. {
  1830. DataGridView dgv = new DataGridView ();
  1831. dgv.RowCount = 3;
  1832. Assert.AreEqual (3, dgv.RowCount, "A1");
  1833. Assert.AreEqual (1, dgv.ColumnCount, "A2");
  1834. Assert.AreEqual (0, dgv.Rows[0].Index, "A3");
  1835. Assert.AreEqual (1, dgv.Rows[1].Index, "A4");
  1836. Assert.AreEqual (2, dgv.Rows[2].Index, "A5");
  1837. dgv.RowCount = 2;
  1838. Assert.AreEqual (2, dgv.RowCount, "B1");
  1839. Assert.AreEqual (1, dgv.ColumnCount, "B2");
  1840. Assert.AreEqual (0, dgv.Rows[0].Index, "B3");
  1841. Assert.AreEqual (1, dgv.Rows[1].Index, "B4");
  1842. dgv.RowCount = 6;
  1843. Assert.AreEqual (6, dgv.RowCount, "C1");
  1844. Assert.AreEqual (1, dgv.ColumnCount, "C2");
  1845. Assert.AreEqual (0, dgv.Rows[0].Index, "C3");
  1846. Assert.AreEqual (1, dgv.Rows[1].Index, "C4");
  1847. Assert.AreEqual (2, dgv.Rows[2].Index, "C5");
  1848. dgv.AllowUserToAddRows = false;
  1849. Assert.AreEqual (5, dgv.RowCount, "D1");
  1850. Assert.AreEqual (1, dgv.ColumnCount, "D2");
  1851. dgv.RowCount = 1;
  1852. Assert.AreEqual (1, dgv.RowCount, "E1");
  1853. Assert.AreEqual (1, dgv.ColumnCount, "E2");
  1854. Assert.AreEqual (0, dgv.Rows[0].Index, "E3");
  1855. dgv.RowCount = 8;
  1856. Assert.AreEqual (8, dgv.RowCount, "F1");
  1857. Assert.AreEqual (1, dgv.ColumnCount, "F2");
  1858. Assert.AreEqual (0, dgv.Rows[0].Index, "F3");
  1859. Assert.AreEqual (1, dgv.Rows[1].Index, "F4");
  1860. }
  1861. [Test]
  1862. public void RowCountDecrease ()
  1863. {
  1864. DataGridView dgv = new DataGridView ();
  1865. dgv.RowCount = 6;
  1866. Assert.AreEqual (6, dgv.RowCount, "A1");
  1867. Assert.AreEqual (1, dgv.ColumnCount, "A2");
  1868. dgv.RowCount = 3;
  1869. Assert.AreEqual (3, dgv.RowCount, "A3");
  1870. Assert.AreEqual (0, dgv.Rows[0].Index, "A4");
  1871. Assert.AreEqual (1, dgv.Rows[1].Index, "A5");
  1872. Assert.AreEqual (2, dgv.Rows[2].Index, "A6");
  1873. try {
  1874. dgv.RowCount = 0;
  1875. Assert.Fail ("C1");
  1876. } catch {}
  1877. dgv.RowCount = 6;
  1878. Assert.AreEqual (6, dgv.RowCount, "B1");
  1879. Assert.AreEqual (1, dgv.ColumnCount, "B2");
  1880. Assert.AreEqual (0, dgv.Rows[0].Index, "B3");
  1881. Assert.AreEqual (1, dgv.Rows[1].Index, "B4");
  1882. Assert.AreEqual (2, dgv.Rows[2].Index, "B5");
  1883. dgv.RowCount = 2;
  1884. Assert.AreEqual (2, dgv.RowCount, "C1");
  1885. Assert.AreEqual (1, dgv.ColumnCount, "C2");
  1886. Assert.AreEqual (0, dgv.Rows[0].Index, "C3");
  1887. Assert.AreEqual (1, dgv.Rows[1].Index, "C4");
  1888. dgv.AllowUserToAddRows = false;
  1889. Assert.AreEqual (1, dgv.RowCount, "D1");
  1890. Assert.AreEqual (1, dgv.ColumnCount, "D2");
  1891. Assert.AreEqual (0, dgv.Rows[0].Index, "D3");
  1892. dgv.RowCount = 6;
  1893. Assert.AreEqual (6, dgv.RowCount, "E1");
  1894. Assert.AreEqual (1, dgv.ColumnCount, "E2");
  1895. Assert.AreEqual (0, dgv.Rows[0].Index, "E3");
  1896. Assert.AreEqual (1, dgv.Rows[1].Index, "E4");
  1897. Assert.AreEqual (2, dgv.Rows[2].Index, "E5");
  1898. dgv.RowCount = 2;
  1899. Assert.AreEqual (2, dgv.RowCount, "F1");
  1900. Assert.AreEqual (1, dgv.ColumnCount, "F2");
  1901. Assert.AreEqual (0, dgv.Rows[0].Index, "F3");
  1902. Assert.AreEqual (1, dgv.Rows[1].Index, "F4");
  1903. }
  1904. [Test]
  1905. public void BindToReadonlyProperty ()
  1906. {
  1907. Form f = new Form ();
  1908. f.ShowInTaskbar = false;
  1909. DataGridView dgv = new DataGridView ();
  1910. List<cust> l = new List<cust> ();
  1911. l.Add (new cust ());
  1912. dgv.DataSource = l;
  1913. f.Controls.Add (dgv);
  1914. f.Show ();
  1915. Assert.AreEqual ("Name", dgv.Columns[0].Name, "A1");
  1916. Assert.AreEqual (true, dgv.Columns[0].ReadOnly, "A2");
  1917. f.Close ();
  1918. f.Dispose ();
  1919. }
  1920. class cust { public string Name { get { return "test"; } } }
  1921. [Test]
  1922. public void EnableHeadersVisualStylesDefaultValue ()
  1923. {
  1924. Assert.AreEqual (true, new DataGridView ().EnableHeadersVisualStyles);
  1925. }
  1926. [Test]
  1927. public void RowTemplate ()
  1928. {
  1929. DataGridView dgv = new DataGridView ();
  1930. Form f = new Form ();
  1931. f.Controls.Add (dgv);
  1932. f.Show ();
  1933. dgv.Columns.Add ("A1", "A1");
  1934. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A1");
  1935. Assert.IsNull (dgv.RowTemplate.DataGridView, "A2");
  1936. dgv.Columns.Add ("A2", "A2");
  1937. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A3");
  1938. dgv.Rows.Add (3, 6);
  1939. dgv.Columns.Remove ("A1");
  1940. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A4");
  1941. Assert.AreEqual (1, dgv.Rows[0].Cells.Count, "A5");
  1942. dgv.Columns.Clear ();
  1943. Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A6");
  1944. Assert.AreEqual (0, dgv.Rows.Count, "A7");
  1945. f.Close ();
  1946. f.Dispose ();
  1947. }
  1948. [Test]
  1949. public void ScrollToSelectionSynchronous()
  1950. {
  1951. DataGridView dgv = new DataGridView ();
  1952. dgv.RowCount = 1000;
  1953. dgv.CurrentCell = dgv[0, dgv.RowCount -1];
  1954. Rectangle rowRect = dgv.GetRowDisplayRectangle (dgv.RowCount - 1, false);
  1955. Assert.AreEqual (true, dgv.DisplayRectangle.Contains (rowRect), "#01");
  1956. }
  1957. [Test]
  1958. public void CurrentCell()
  1959. {
  1960. DataGridView dgv = new DataGridView ();
  1961. dgv.AllowUserToAddRows = false;
  1962. Assert.IsNull (dgv.CurrentCell, "A1");
  1963. dgv.RowCount = 10;
  1964. dgv.ColumnCount = 2;
  1965. Assert.AreEqual (10, dgv.RowCount, "B1");
  1966. Assert.AreEqual (2, dgv.ColumnCount, "B2");
  1967. Assert.IsNull (dgv.CurrentCell, "B3");
  1968. dgv.CurrentCell = dgv[1, 9];
  1969. Assert.IsNotNull (dgv.CurrentCell, "H1");
  1970. Assert.AreEqual (9, dgv.CurrentCell.RowIndex, "H2");
  1971. Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "H3");
  1972. dgv.CurrentCell = null;
  1973. Assert.IsNull (dgv.CurrentCell, "C1");
  1974. dgv.CurrentCell = dgv[1, 9];
  1975. Assert.IsNotNull (dgv.CurrentCell, "D1");
  1976. Assert.AreEqual (9, dgv.CurrentCell.RowIndex, "D2");
  1977. Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "D3");
  1978. dgv.RowCount = 9;
  1979. Assert.IsNotNull (dgv.CurrentCell, "E1");
  1980. Assert.AreEqual (8, dgv.CurrentCell.RowIndex, "E2");
  1981. Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "E3");
  1982. dgv.CurrentCell = dgv[0, 4];
  1983. dgv.RowCount = 2;
  1984. Assert.IsNotNull (dgv.CurrentCell, "F1");
  1985. Assert.AreEqual (1, dgv.CurrentCell.RowIndex, "F2");
  1986. Assert.AreEqual (0, dgv.CurrentCell.ColumnIndex, "F3");
  1987. dgv.RowCount = 0;
  1988. Assert.IsNull (dgv.CurrentCell, "P1");
  1989. dgv.RowCount = 10;
  1990. Assert.AreEqual (10, dgv.RowCount, "I1");
  1991. dgv.CurrentCell = dgv[0, 4];
  1992. dgv.ColumnCount = 0;
  1993. Assert.AreEqual (0, dgv.RowCount, "I2");
  1994. Assert.IsNull (dgv.CurrentCell, "I3");
  1995. dgv.RowCount = 0;
  1996. dgv.ColumnCount = 0;
  1997. dgv.CreateControl ();
  1998. dgv.ColumnCount = 2;
  1999. dgv.RowCount = 3;
  2000. Assert.IsNotNull (dgv.CurrentCell, "G1");
  2001. Assert.AreEqual (0, dgv.CurrentCell.RowIndex, "G1");
  2002. Assert.AreEqual (0, dgv.CurrentCell.ColumnIndex, "G1");
  2003. }
  2004. [Test]
  2005. public void DataSourceBindingContextDependency ()
  2006. {
  2007. List<DataItem> dataList = new List<DataItem> ();
  2008. dataList.Add (new DataItem ());
  2009. dataList.Add (new DataItem ());
  2010. DataGridView dgv = new DataGridView ();
  2011. dgv.DataSource = dataList;
  2012. Assert.IsNull (dgv.BindingContext, "#1");
  2013. Assert.IsFalse (dgv.IsHandleCreated, "#2");
  2014. Assert.AreEqual (0, dgv.RowCount, "#3");
  2015. dgv.DataSource = null;
  2016. Form form = new Form ();
  2017. form.Controls.Add (dgv);
  2018. dgv.DataSource = dataList;
  2019. Assert.IsNotNull (dgv.BindingContext, "#4");
  2020. Assert.IsFalse (dgv.IsHandleCreated, "#5");
  2021. Assert.AreEqual (2, dgv.RowCount, "#6");
  2022. dgv.Dispose ();
  2023. dgv = new DataGridView ();
  2024. dgv.DataSource = dataList;
  2025. Assert.IsNull (dgv.BindingContext, "#7");
  2026. Assert.IsFalse (dgv.IsHandleCreated, "#8");
  2027. Assert.AreEqual (0, dgv.RowCount, "#9");
  2028. dgv.CreateControl ();
  2029. Assert.IsNull (dgv.BindingContext, "#10");
  2030. Assert.IsTrue (dgv.IsHandleCreated, "#11");
  2031. Assert.AreEqual (0, dgv.RowCount, "#12");
  2032. }
  2033. [Test]
  2034. public void RowTemplateDataGridView ()
  2035. {
  2036. DataGridView gdv = new DataGridView ();
  2037. Assert.IsNull (gdv.RowTemplate.DataGridView, "#1");
  2038. }
  2039. [Test] // Xamarin bug 2392
  2040. public void RowHeightInVirtualMode ()
  2041. {
  2042. using (var dgv = new DataGridView ()) {
  2043. dgv.RowHeightInfoNeeded += (sender, e) => {
  2044. e.Height = 50;
  2045. e.MinimumHeight = 30;
  2046. };
  2047. dgv.VirtualMode = true;
  2048. dgv.RowCount = 2;
  2049. Assert.AreEqual (50, dgv.Rows [0].Height);
  2050. Assert.AreEqual (30, dgv.Rows [0].MinimumHeight);
  2051. Assert.AreEqual (50, dgv.Rows [1].Height);
  2052. Assert.AreEqual (30, dgv.Rows [1].MinimumHeight);
  2053. }
  2054. }
  2055. [Test] // Novell bug 660986
  2056. public void TestDispose ()
  2057. {
  2058. DataGridView dgv = new DataGridView ();
  2059. dgv.Columns.Add ("TestColumn", "Test column");
  2060. dgv.Rows.Add ();
  2061. dgv.Dispose ();
  2062. try {
  2063. DataGridViewColumn col = dgv.Columns[0];
  2064. Assert.Fail ("#1");
  2065. }
  2066. catch (ArgumentOutOfRangeException) {
  2067. }
  2068. try {
  2069. DataGridViewRow row = dgv.Rows[0];
  2070. Assert.Fail ("#2");
  2071. }
  2072. catch (ArgumentOutOfRangeException) {
  2073. }
  2074. }
  2075. private class MyDataGridView: DataGridView
  2076. {
  2077. public void SetCurrentCell ()
  2078. {
  2079. CurrentCell = Rows [1].Cells [1];
  2080. }
  2081. }
  2082. [Test]
  2083. public void TestDisposeWhenInEditMode_Xamarin19567 ()
  2084. {
  2085. var dgv = new MyDataGridView ();
  2086. dgv.EditMode = DataGridViewEditMode.EditOnEnter;
  2087. dgv.Columns.Add ("TestColumn", "Test column");
  2088. dgv.Columns.Add ("Column2", "Second column");
  2089. dgv.Rows.Add ();
  2090. dgv.Rows.Add ();
  2091. dgv.SetCurrentCell ();
  2092. // The Dispose() call will fail if #19567 is not fixed
  2093. dgv.Dispose ();
  2094. }
  2095. [Test] // Xamarin bug 3125
  2096. public void TestRemoveBug3125 ()
  2097. {
  2098. DataGridViewRow dgvr1 = new DataGridViewRow ();
  2099. DataGridViewRow dgvr2 = new DataGridViewRow ();
  2100. DataGridViewRow dgvr3 = new DataGridViewRow ();
  2101. Assert.IsNull (dgvr1.DataGridView, "#1");
  2102. Assert.IsNull (dgvr2.DataGridView, "#2");
  2103. Assert.IsNull (dgvr3.DataGridView, "#3");
  2104. DataGridView dgv = new DataGridView ();
  2105. // dgv needs column and cell or throws error
  2106. DataGridViewColumn dgvc1 = new DataGridViewColumn ();
  2107. DataGridViewCell cell = new DataGridViewTextBoxCell ();
  2108. dgvc1.CellTemplate = cell;
  2109. dgv.Columns.Add (dgvc1);
  2110. dgv.Rows.Add (dgvr1);
  2111. dgv.Rows.Add (dgvr2);
  2112. dgv.Rows.Add (dgvr3);
  2113. // was dgv.Clear () and that caused test build to fail
  2114. dgv.Rows.Clear (); // presumbly this was the intention?
  2115. Assert.IsNull (dgvr1.DataGridView, "#4");
  2116. Assert.IsNull (dgvr2.DataGridView, "#5");
  2117. Assert.IsNull (dgvr3.DataGridView, "#6");
  2118. }
  2119. [Test] // Xamarin bug #2394
  2120. public void Bug2394_RowHeightLessThanOldMinHeightVirtMode ()
  2121. {
  2122. using (var dgv = new DataGridView ()) {
  2123. dgv.VirtualMode = true;
  2124. dgv.RowCount = 1;
  2125. dgv.Rows [0].MinimumHeight = 5;
  2126. dgv.Rows [0].Height = 10;
  2127. dgv.RowHeightInfoNeeded += (sender, e) => {
  2128. // NOTE: the order is important here.
  2129. e.MinimumHeight = 2;
  2130. e.Height = 2;
  2131. };
  2132. dgv.UpdateRowHeightInfo (0, false);
  2133. Assert.AreEqual (2, dgv.Rows [0].Height);
  2134. Assert.AreEqual (2, dgv.Rows [0].MinimumHeight);
  2135. }
  2136. }
  2137. [Test] // Xamarin bug #2394
  2138. public void Bug2394_RowHeightLessThanMinHeightVirtMode ()
  2139. {
  2140. using (var dgv = new DataGridView ()) {
  2141. dgv.VirtualMode = true;
  2142. dgv.RowCount = 1;
  2143. dgv.Rows [0].Height = 10;
  2144. dgv.Rows [0].MinimumHeight = 5;
  2145. dgv.RowHeightInfoNeeded += (sender, e) => {
  2146. // Setting the height to a value less than the minimum height
  2147. // will be silently ignored and instead set to MinimumHeight.
  2148. e.Height = 2;
  2149. };
  2150. dgv.UpdateRowHeightInfo (0, false);
  2151. Assert.AreEqual(5, dgv.Rows[0].Height);
  2152. Assert.AreEqual(5, dgv.Rows[0].MinimumHeight);
  2153. }
  2154. }
  2155. [Test] // Xamarin bug #2394
  2156. public void Bug2394_MinHeightGreaterThanOldRowHeightVirtMode ()
  2157. {
  2158. using (var dgv = new DataGridView ()) {
  2159. dgv.VirtualMode = true;
  2160. dgv.RowCount = 1;
  2161. dgv.Rows [0].Height = 10;
  2162. dgv.Rows [0].MinimumHeight = 5;
  2163. dgv.RowHeightInfoNeeded += (sender, e) => {
  2164. e.MinimumHeight = 30;
  2165. e.Height = 40;
  2166. };
  2167. dgv.UpdateRowHeightInfo (0, false);
  2168. Assert.AreEqual (40, dgv.Rows [0].Height);
  2169. Assert.AreEqual (30, dgv.Rows [0].MinimumHeight);
  2170. }
  2171. }
  2172. [Test] // Xamarin bug #24372
  2173. public void Bug24372_first_row_index ()
  2174. {
  2175. Form form = new Form ();
  2176. DataGridView24372 dgv = new DataGridView24372 ();
  2177. dgv.Parent = form;
  2178. dgv.ColumnCount = 1;
  2179. dgv.RowCount = 100;
  2180. dgv.CurrentCell = dgv[0,50];
  2181. dgv.Focus ();
  2182. form.Show ();
  2183. dgv.Rows.Clear ();
  2184. form.Refresh ();
  2185. Application.DoEvents ();
  2186. if (dgv.HasException)
  2187. Assert.Fail("#A1");
  2188. form.Dispose ();
  2189. }
  2190. class DataGridView24372 : DataGridView
  2191. {
  2192. public bool HasException { get; private set; }
  2193. protected override void OnPaint (PaintEventArgs e)
  2194. {
  2195. HasException = false;
  2196. try {
  2197. base.OnPaint(e);
  2198. } catch (ArgumentOutOfRangeException ex) {
  2199. HasException = true;
  2200. }
  2201. }
  2202. }
  2203. }
  2204. [TestFixture]
  2205. public class DataGridViewControlCollectionTest
  2206. {
  2207. [Test]
  2208. public void TestClear ()
  2209. {
  2210. using (DataGridView dgv = new DataGridView ()) {
  2211. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection) dgv.Controls;
  2212. Control c1 = new Control ();
  2213. Control c2 = new Control ();
  2214. Control c3 = new Control ();
  2215. Assert.AreEqual (2, controls.Count, "#02");
  2216. controls.Add (c1);
  2217. controls.Add (c2);
  2218. controls.Add (c3);
  2219. Assert.AreEqual (5, controls.Count, "#02");
  2220. controls.Clear ();
  2221. Assert.AreEqual (3, controls.Count, "#03");
  2222. Assert.AreSame (c2, controls [2], "#04");
  2223. }
  2224. // Maybe MS should start writing unit-tests?
  2225. using (DataGridView dgv = new DataGridView ()) {
  2226. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2227. Control [] c = new Control [20];
  2228. for (int i = 0; i < c.Length; i++) {
  2229. c [i] = new Control ();
  2230. c [i].Text = "#" + i.ToString ();
  2231. }
  2232. Assert.AreEqual (2, controls.Count, "#02");
  2233. controls.AddRange (c);
  2234. Assert.AreEqual (22, controls.Count, "#02");
  2235. controls.Clear ();
  2236. Assert.AreEqual (12, controls.Count, "#03");
  2237. for (int i = 0; i < c.Length; i += 2) {
  2238. Assert.AreSame (c [i+1], controls [ (i / 2) + 2], "#A" + i.ToString ());
  2239. }
  2240. }
  2241. }
  2242. [Test]
  2243. public void TestCopyTo ()
  2244. {
  2245. using (DataGridView dgv = new DataGridView ()) {
  2246. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2247. Control c1 = new Control ();
  2248. Control c2 = new Control ();
  2249. Control c3 = new Control ();
  2250. Control [] copy = new Control [10];
  2251. Assert.AreEqual (2, controls.Count, "#01");
  2252. controls.AddRange (new Control [] { c1, c2, c3 });
  2253. Assert.AreEqual (5, controls.Count, "#01-b");
  2254. controls.CopyTo (copy, 0);
  2255. Assert.AreEqual (5, controls.Count, "#02");
  2256. Assert.AreEqual (10, copy.Length, "#03");
  2257. for (int i = 0; i < copy.Length; i++) {
  2258. if (i >= 5)
  2259. Assert.IsNull (copy [i], "#A" + i.ToString ());
  2260. else
  2261. Assert.IsNotNull (copy [i], "#B" + i.ToString ());
  2262. }
  2263. }
  2264. }
  2265. [Test]
  2266. [ExpectedException (typeof (NotSupportedException))]
  2267. public void TestInsert ()
  2268. {
  2269. using (DataGridView dgv = new DataGridView ()) {
  2270. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2271. controls.Insert (1, new Control ());
  2272. }
  2273. }
  2274. [Test]
  2275. public void TestRemove ()
  2276. {
  2277. using (DataGridView dgv = new DataGridView ()) {
  2278. DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
  2279. Control c1 = new Control ();
  2280. Control c2 = new Control ();
  2281. Control c3 = new Control ();
  2282. Control [] copy = new Control [10];
  2283. controls.AddRange (new Control [] {c1, c2, c3});
  2284. controls.Remove (c2);
  2285. Assert.AreEqual (4, controls.Count, "#01");
  2286. controls.Remove (c2);
  2287. Assert.AreEqual (4, controls.Count, "#02");
  2288. controls.Remove (c1);
  2289. Assert.AreEqual (3, controls.Count, "#03");
  2290. controls.Remove (c3);
  2291. Assert.AreEqual (2, controls.Count, "#04");
  2292. controls.Remove (controls [0]);
  2293. controls.Remove (controls [1]);
  2294. Assert.AreEqual (2, controls.Count, "#05");
  2295. }
  2296. }
  2297. }
  2298. }