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