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