Fire the DataGridView.SelectionChanged event when the selected rows/columns changes...
[mono.git] / mcs / class / Managed.Windows.Forms / Test / System.Windows.Forms / DataGridViewTest.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 // 
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 // 
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2005, 2006, 2007 Novell, Inc. (http://www.novell.com)
21 //
22 // Author:
23 //      Pedro Martínez Juliá <pedromj@gmail.com>
24 //      Daniel Nauck    (dna(at)mono-project(dot)de)
25 //      Ivan N. Zlatev  <contact@i-nz.net>
26
27
28 #if NET_2_0
29
30 using System;
31 using System.Data;
32 using System.Drawing;
33 using System.Collections;
34 using System.Collections.Generic;
35 using System.ComponentModel;
36 using System.Diagnostics;
37 using System.IO;
38 using System.Text;
39 using System.Windows.Forms;
40
41 using NUnit.Framework;
42
43 namespace MonoTests.System.Windows.Forms
44 {
45         [TestFixture]
46         public class DataGridViewTest : TestHelper
47         {
48                 private DataGridView grid = null;
49
50                 [SetUp]
51                 protected override void SetUp()
52                 {
53                         grid = new DataGridView();
54                         base.SetUp ();
55                 }
56
57                 [TearDown]
58                 protected override void TearDown ()
59                 {
60                         grid.Dispose ();
61                         base.TearDown ();
62                 }
63
64                 [Test]
65                 [ExpectedException (typeof (InvalidOperationException), ExpectedMessage = "Generating Clipboard content is not supported when the ClipboardCopyMode property is Disable.")]
66                 public void GetClipboardContentsDisabled ()
67                 {
68                         using (DataGridView dgv = new DataGridView ()) {
69                                 dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.Disable;
70                                 object o = dgv.GetClipboardContent ();
71                         }
72                 }
73
74                 private class ExposeProtectedProperties : DataGridView
75                 {
76                         public new Padding DefaultPadding { get { return base.DefaultPadding; } }
77                         public new Size DefaultSize { get { return base.DefaultSize; } }
78                         public new bool IsDoubleBuffered { get { return base.DoubleBuffered; } }
79
80                         public ControlStyles GetControlStyles ()
81                         {
82                                 ControlStyles retval = (ControlStyles)0;
83
84                                 foreach (ControlStyles cs in Enum.GetValues (typeof (ControlStyles)))
85                                         if (this.GetStyle (cs) == true)
86                                                 retval |= cs;
87
88                                 return retval;
89                         }
90                         
91                         public bool PublicIsInputKey (Keys keyData)
92                         {
93                                 return base.IsInputKey (keyData);
94                         }
95                         
96                         public bool PublicIsInputChar (char charCode)
97                         {
98                                 return base.IsInputChar (charCode);
99                         }
100                 }
101
102 #region GenerateClipboardTest
103                 public static void GenerateClipboardTest ()
104                 {
105                         GenerateClipboardTest (false);
106                         GenerateClipboardTest (true);
107                 }
108
109                 public static string GenerateClipboardTest (bool headers)
110                 {
111                         StringBuilder result = new StringBuilder ();
112
113                         int tab = 0;
114                         string classname = headers ? "DataGridViewClipboardHeaderTest" : "DataGridViewClipboardTest";
115
116                         append (result, tab, "//");
117                         append (result, tab, "// Copyright (c) 2007 Novell, Inc. (http://www.novell.com)");
118                         append (result, tab, "//");
119                         append (result, tab, "// Author:");
120                         append (result, tab, "//        DataGridViewTest.GenerateClipboardTest ({0});", headers.ToString ().ToLower ());
121                         append (result, tab, "//");
122                         append (result, tab, "#if NET_2_0");
123                         append (result, tab, "using NUnit.Framework;");
124                         append (result, tab, "using System;");
125                         append (result, tab, "using System.Drawing;");
126                         append (result, tab, "using System.Windows.Forms;");
127                         append (result, tab, "using System.ComponentModel;");
128                         append (result, tab, "using System.Collections;");
129                         append (result, tab, "using System.Text;");
130                         append (result, tab, "using System.Collections.Generic;");
131                         append (result, tab, "using System.Diagnostics;");
132                         append (result, tab, "using System.IO;");
133                         append (result, tab, "namespace MonoTests.System.Windows.Forms {"); tab++;
134                         append (result, tab, "[TestFixture]");
135                         append (result, tab, "public class {0} {{", classname); tab++;
136                         append (result, tab, "[Test]");
137                         append (result, tab, "public void Test () {"); tab++;
138
139
140                         append (result, tab, "DataObject data;");
141                         append (result, tab, "DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell row_header_cell;");
142                         append (result, tab, "DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell col_header_cell;");
143                         //append (result, tab, "string csv = null, html = null, utext = null, text = null;");
144                         append (result, tab, "string code = null;");
145
146                         int counter;
147
148                         List<List<int>> selected_bands = new List<List<int>> ();
149                         List<List<CellSelection>> selected_cells = new List<List<CellSelection>> ();
150
151                         selected_bands.Add (new List<int> ());
152                         selected_bands.Add (new List<int> (new int [] { 0 }));
153                         selected_bands.Add (new List<int> (new int [] { 2 }));
154                         selected_bands.Add (new List<int> (new int [] { 1, 2 }));
155                         selected_bands.Add (new List<int> (new int [] { 1, 3 }));
156
157                         selected_cells.Add (new List<CellSelection> ());
158                         selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (0, 0, true) }));
159                         selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (2, 2, false) }));
160                         selected_cells.Add (new List<CellSelection> (new CellSelection [] { new CellSelection (0, 0, false), new CellSelection (2, 2, true) }));
161
162                         foreach (DataGridViewClipboardCopyMode copymode in Enum.GetValues (typeof (DataGridViewClipboardCopyMode))) {
163                                 if (copymode == DataGridViewClipboardCopyMode.Disable)
164                                         continue;
165
166                                 counter = 0;
167                                 foreach (DataGridViewSelectionMode selectionmode in Enum.GetValues (typeof (DataGridViewSelectionMode))) {
168                                         bool is_row_selectable, is_col_selectable, is_cell_selectable;
169
170                                         is_row_selectable = selectionmode == DataGridViewSelectionMode.RowHeaderSelect || selectionmode == DataGridViewSelectionMode.FullRowSelect;
171                                         is_col_selectable = selectionmode == DataGridViewSelectionMode.ColumnHeaderSelect || selectionmode == DataGridViewSelectionMode.FullColumnSelect;
172                                         is_cell_selectable = selectionmode == DataGridViewSelectionMode.CellSelect || selectionmode == DataGridViewSelectionMode.ColumnHeaderSelect || selectionmode == DataGridViewSelectionMode.RowHeaderSelect;
173
174                                         foreach (List<int> cols in selected_bands) {
175                                                 if (!is_col_selectable && cols.Count > 0)
176                                                         continue;
177
178                                                 foreach (List<int> rows in selected_bands) {
179                                                         if (!is_row_selectable && rows.Count > 0)
180                                                                 continue;
181
182                                                         foreach (List<CellSelection> cells in selected_cells) {
183                                                                 if (!is_cell_selectable && cells.Count > 0)
184                                                                         continue;
185
186                                                                 using (DataGridView dgv = DataGridViewCommon.CreateAndFillForClipboard ()) {
187
188                                                                         dgv.SelectionMode = selectionmode;
189                                                                         dgv.ClipboardCopyMode = copymode;
190                                                                         bool any_selected = false;
191                                                                         if (is_col_selectable && cols.Count > 0) {
192                                                                                 foreach (int c in cols) {
193                                                                                         dgv.Columns [c].Selected = true;
194                                                                                         any_selected = true;
195                                                                                 }
196                                                                         }
197                                                                         if (is_row_selectable && rows.Count > 0) {
198                                                                                 foreach (int r in rows) {
199                                                                                         dgv.Rows [r].Selected = true;
200                                                                                         any_selected = true;
201                                                                                 }
202                                                                         }
203                                                                         if (is_cell_selectable && cells.Count > 0) {
204                                                                                 foreach (CellSelection selection in cells) {
205                                                                                         DataGridViewCell cell = dgv.Rows [selection.Row].Cells [selection.Col];
206                                                                                         if (cell.Selected != selection.Selected) {
207                                                                                                 cell.Selected = selection.Selected;
208                                                                                                 any_selected = true;
209                                                                                         }
210                                                                                 }
211                                                                         }
212
213                                                                         if (any_selected == false && !(cols.Count == 0 && rows.Count == 0 && cells.Count == 0)) {
214                                                                                 continue;
215                                                                         }
216
217                                                                         generate_case (result, dgv, copymode.ToString () + "#" + (counter++).ToString (), headers);
218                                                                 }
219                                                         }
220                                                 }
221                                         }
222                                 }
223                         }
224
225                         append (result, --tab, "}");
226                         append (result, --tab, "}");
227                         append (result, --tab, "}");
228                         append (result, tab, "#endif"); ;
229
230                         throw new NotImplementedException ("Where am I?");
231                         // Uncomment the following line, change the path, and comment out the exception.
232                         //File.WriteAllText (@"Z:\mono\head\mcs\class\SWF\Test\System.Windows.Forms\" + classname + ".cs", result.ToString ());
233
234                         return string.Empty;
235                 }
236                 
237                 private static string tabs (int t) { return new string ('\t', t); }
238                 private static void append (StringBuilder result, int tab, string text) { result.Append (tabs (tab) + text + "\n"); }
239                 private static void append (StringBuilder result, int tab, string text, params object [] args) { result.Append (tabs (tab) + string.Format (text, args) + "\n"); }
240                 private static string cs_encode (string literal, string newline) {
241                         bool has_newlines = literal.Contains ("\r\n");
242                         bool format_string = has_newlines;
243
244                         literal = literal.Replace ("\\", "\\\\");
245                         literal = literal.Replace ("\"", "\\\"");
246                         literal = literal.Replace ("\t", "\\t");
247                         
248                         if (has_newlines) {
249                                 if (newline == @"""\r\n""") {
250                                         literal = literal.Replace ("\r\n", @"\r\n");
251                                         format_string = false;
252                                 } else {
253                                         literal = literal.Replace ("\r\n", "{0}");
254                                 }
255                         }
256
257                         literal = "\"" + literal + "\"";
258
259                         if (format_string) {
260                                 return "string.Format (" + literal/*.Replace ("{", "{{").Replace ("}", "}}")*/ + ", " + newline + ")";
261                         } else {
262                                 return literal;
263                         }
264                 }
265                 
266                 private static string cs_encode (string literal) {
267                         return cs_encode (literal, "Environment.NewLine");
268                 }
269                 
270                 private class CellSelection {
271                         public bool Selected;
272                         public int Row;
273                         public int Col;
274                         public CellSelection (int Row, int Col, bool Selected) {
275                                 this.Selected = Selected;
276                                 this.Row = Row;
277                                 this.Col = Col;
278                         }
279                 }
280                 
281                 static private void generate_case (StringBuilder result, DataGridView dgv, string message, bool headers)
282                 {
283                         Console.WriteLine (message + ", current length: " + result.Length.ToString ());
284                         Debug.WriteLine (message + ", current length: " + result.Length.ToString ());
285                         
286                         if (headers) {
287                                 if (dgv.SelectionMode != DataGridViewSelectionMode.CellSelect)
288                                         return;
289                                 if (dgv.ClipboardCopyMode != DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText)
290                                         return;
291                         }
292                         
293                         int tab = 3;
294                         DataObject data;
295                         string csv = null, html = null, utext = null, text = null;
296                         string code = null;
297                         DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell row_header_cell;
298                         DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell col_header_cell;
299                         int counter = 0;
300                         
301                         append (result, tab, "using (DataGridView dgv = DataGridViewCommon.CreateAndFillForClipboard ()) {");
302                         tab++;
303                         
304                         append (result, tab, "dgv.SelectionMode = DataGridViewSelectionMode.{0};", dgv.SelectionMode.ToString ());
305                         append (result, tab, "dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.{0};", dgv.ClipboardCopyMode.ToString ());
306                         
307                         switch (dgv.SelectionMode) {
308                         case DataGridViewSelectionMode.FullRowSelect:
309                                 foreach (DataGridViewRow row in dgv.Rows) {
310                                         if (row.Selected) {
311                                                 append (result, tab, "dgv.Rows [{0}].Selected = true;", row.Index);
312                                         }
313                                 }
314                                 break;
315                         case DataGridViewSelectionMode.FullColumnSelect:
316                                 foreach (DataGridViewColumn col in dgv.Columns) {
317                                         if (col.Selected) {
318                                                 append (result, tab, "dgv.Columns [{0}].Selected = true;", col.Index);
319                                         }
320                                 }
321                                 break;
322                         case DataGridViewSelectionMode.ColumnHeaderSelect:
323                         case DataGridViewSelectionMode.RowHeaderSelect:
324                         case DataGridViewSelectionMode.CellSelect:
325                                 if (dgv.SelectionMode == DataGridViewSelectionMode.RowHeaderSelect) {
326                                         foreach (DataGridViewRow row in dgv.Rows) {
327                                                 if (row.Selected) {
328                                                         append (result, tab, "dgv.Rows [{0}].Selected = true;", row.Index);
329                                                 }
330                                         }
331                                 }
332                                 if (dgv.SelectionMode == DataGridViewSelectionMode.ColumnHeaderSelect) {
333                                         foreach (DataGridViewColumn col in dgv.Columns) {
334                                                 if (col.Selected) {
335                                                         append (result, tab, "dgv.Columns [{0}].Selected = true;", col.Index);
336                                                 }
337                                         }
338                                 }
339                                 for (int r = 0; r < dgv.RowCount; r++) {
340                                         for (int c = 0; c < dgv.ColumnCount; c++) {
341                                                 bool rowS = dgv.Rows [r].Selected;
342                                                 bool colS = dgv.Columns [c].Selected;
343                                                 bool cellS = dgv.Rows [r].Cells [c].Selected;
344                                                 
345                                                 if ((rowS || colS) && !cellS) {
346                                                         append (result, tab, "dgv.Rows [{0}].Cells [{1}].Selected = false;", r, c);
347                                                 } else if ((!rowS && !colS) && cellS) {
348                                                         append (result, tab, "dgv.Rows [{0}].Cells [{1}].Selected = true;", r, c);
349                                                 }
350                                         }
351                                 }
352                                 break;
353                         }
354                         
355                         if (!headers) {
356                                 data = dgv.GetClipboardContent ();
357                                 append (result, tab, "data = dgv.GetClipboardContent ();");
358                                 
359                                 if (data == null) {
360                                         append (result, tab, "Assert.IsNull (data, {0});", cs_encode ("#" + message + "-" + (counter++).ToString ()));
361                                 } else {
362                                         append (result, tab, "Assert.IsNotNull (data, {0});", cs_encode ("#" + message + "-" + (counter++).ToString ()));
363                                         
364                                         csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
365                                         html = data.GetData (DataFormats.Html) as string;
366                                         utext = data.GetData (DataFormats.UnicodeText) as string;
367                                         text = data.GetData (DataFormats.Text) as string;
368                                         
369                                         append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.CommaSeparatedValue), {1});", cs_encode (csv), cs_encode ("#" + message + "-" + (counter++).ToString ()));
370                                         append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.Html), {1});", cs_encode (html, @"""\r\n"""), cs_encode ("#" + message + "-" + (counter++).ToString ()));
371                                         append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.UnicodeText), {1});", cs_encode (utext), cs_encode ("#" + message + "-" + (counter++).ToString ()));
372                                         append (result, tab, "Assert.AreEqual ({0}, data.GetData (DataFormats.Text), {1});", cs_encode (text), cs_encode ("#" + message + "-" + (counter++).ToString ()));
373                                 }
374                         } else {
375                                 bool [] bools = new bool [] { true, false };
376                                 string [] formats = new string [] { DataFormats.Text, DataFormats.UnicodeText, DataFormats.Html, DataFormats.CommaSeparatedValue };
377
378
379                                 foreach (bool a in bools) {
380                                         foreach (bool b in bools) {
381                                                 foreach (bool c in bools) {
382                                                         foreach (bool d in bools) {
383                                                                 foreach (string format in formats) {
384                                                                         bool did_selected = false;
385                                                                         bool did_unselected = false;
386                                                                         foreach (DataGridViewRow row in dgv.Rows) {
387                                                                                 int i = row.Index;
388                                                                                 if (row.Selected) {
389                                                                                         if (did_selected)
390                                                                                                 continue;
391                                                                                         did_selected = true;
392                                                                                 } else {
393                                                                                         if (did_unselected)
394                                                                                                 continue;
395                                                                                         did_unselected = true;
396                                                                                 }
397                                                                                 row_header_cell = row.HeaderCell as DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell;
398                                                                                 if (row_header_cell == null) {
399                                                                                         append (result, tab, "Assert.IsNull (dgv.Rows [{0}].Headercell, {1});", row.Index, cs_encode ("#" + message + "-" + (counter++).ToString ()));
400                                                                                 } else {
401                                                                                         append (result, tab, "row_header_cell = dgv.Rows [{0}].HeaderCell as DataGridViewRowHeaderTest.DataGridViewRowHeaderClipboardCell;", row.Index);
402                                                                                         code = cs_encode (row_header_cell.GetClipboardContentPublic (i, a, b, c, d, format) as string);
403                                                                                         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);
404                                                                                         append (result, tab, "Assert.AreEqual ({0}, code, {1});", code, cs_encode ("#" + message + "-" + (counter++).ToString ()));
405                                                                                 }
406                                                                         }
407                                                                 }
408                                                         }
409                                                 }
410                                         }
411                                 }
412
413                                 foreach (bool a in bools) {
414                                         foreach (bool b in bools) {
415                                                 foreach (bool c in bools) {
416                                                         foreach (bool d in bools) {
417                                                                 foreach (string format in formats) {
418                                                                         bool did_selected = false;
419                                                                         bool did_unselected = false;
420                                                                         foreach (DataGridViewColumn col in dgv.Columns) {
421                                                                                 int i = -1;
422                                                                                 if (col.Index > 1)
423                                                                                         continue;
424                                                                                 if (col.Selected) {
425                                                                                         if (did_selected)
426                                                                                                 continue;
427                                                                                         did_selected = true;
428                                                                                 } else {
429                                                                                         if (did_unselected)
430                                                                                                 continue;
431                                                                                         did_unselected = true;
432                                                                                 }
433                                                                                 col_header_cell = col.HeaderCell as DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell;
434                                                                                 append (result, tab, "col_header_cell = dgv.Columns [{0}].HeaderCell as DataGridViewColumnHeaderTest.DataGridViewColumnHeaderClipboardCell;", col.Index);
435                                                                                 code = cs_encode (col_header_cell.GetClipboardContentPublic (i, a, b, c, d, format) as string);
436                                                                                 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);
437                                                                                 append (result, tab, "Assert.AreEqual ({0}, code, {1});", code, cs_encode ("#" + message + "-" + (counter++).ToString ()));
438                                                                         }
439                                                                 }
440                                                         }
441                                                 }
442                                         }
443                                 }
444                         }
445                         tab--;
446                         append (result, tab, "}");
447                 }
448 #endregion GenerateClipboardTest
449
450                 [Test]
451                 public void GetClipboardContents ()
452                 {
453                         DataObject data;
454                         string csv, html, utext, text;
455                         
456                         using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
457                                 data = dgv.GetClipboardContent ();      
458                                 Assert.IsNull (data, "#01");
459                                 
460                                 dgv.Rows [0].Cells [0].Selected = true;
461                                 
462                                 data = dgv.GetClipboardContent ();
463                                 Assert.IsNotNull (data, "#B1");
464
465                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
466                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
467                                 
468                                 csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
469                                 html = data.GetData (DataFormats.Html) as string;
470                                 utext = data.GetData (DataFormats.UnicodeText) as string;
471                                 text = data.GetData (DataFormats.Text) as string;
472
473                                 Assert.AreEqual ("Cell A1", csv, "CSV B");
474                                 Assert.AreEqual ("Cell A1", utext, "UTEXT B");
475                                 Assert.AreEqual ("Cell A1", text, "TEXT B");
476                                 Assert.AreEqual (string.Format(@"Version:1.0{0}" + 
477 "StartHTML:00000097{0}" + 
478 "EndHTML:00000211{0}" + 
479 "StartFragment:00000133{0}" + 
480 "EndFragment:00000175{0}" + 
481 "<HTML>{0}" + 
482 "<BODY>{0}" + 
483 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD></TR></TABLE>{0}" + 
484 "<!--EndFragment-->{0}" + 
485 "</BODY>{0}" + 
486 "</HTML>", "\r\n"), html, "HTML B");
487
488                                 dgv.Rows [1].Cells [1].Selected = true;
489
490                                 data = dgv.GetClipboardContent ();
491                                 Assert.IsNotNull (data, "#C1");
492
493                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
494                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
495
496                                 csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
497                                 html = data.GetData (DataFormats.Html) as string;
498                                 utext = data.GetData (DataFormats.UnicodeText) as string;
499                                 text = data.GetData (DataFormats.Text) as string;
500
501                                 Assert.AreEqual (string.Format("Cell A1,{0},Cell B2", Environment.NewLine), csv, "CSV C");
502                                 Assert.AreEqual (string.Format("Cell A1\t{0}\tCell B2", Environment.NewLine), utext, "UTEXT C");
503                                 Assert.AreEqual (string.Format("Cell A1\t{0}\tCell B2", Environment.NewLine), text, "TEXT C");
504                                 string tmp;
505                                 tmp = string.Format(@"Version:1.0{0}" +
506 "StartHTML:00000097{0}" +
507 "EndHTML:00000266{0}" +
508 "StartFragment:00000133{0}" +
509 "EndFragment:00000230{0}" +
510 "<HTML>{0}" +
511 "<BODY>{0}" +
512 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
513 "<!--EndFragment-->{0}" +
514 "</BODY>{0}" +
515 "</HTML>", "\r\n");
516
517                                 Assert.AreEqual (string.Format(@"Version:1.0{0}" +
518 "StartHTML:00000097{0}" +
519 "EndHTML:00000266{0}" +
520 "StartFragment:00000133{0}" +
521 "EndFragment:00000230{0}" +
522 "<HTML>{0}" +
523 "<BODY>{0}" +
524 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
525 "<!--EndFragment-->{0}" +
526 "</BODY>{0}" +
527 "</HTML>", "\r\n"), html, "HTML C");
528                         }
529                 }
530
531                 [Test]
532                 public void GetClipboardContents_HeadersAlways ()
533                 {
534                         DataObject data;
535                         string csv, html, utext, text;
536
537                         using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
538                                 dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText;
539                                 data = dgv.GetClipboardContent ();
540                                 Assert.IsNull (data, "#01");
541
542                                 dgv.Rows [0].Cells [0].Selected = true;
543
544                                 data = dgv.GetClipboardContent ();
545                                 Assert.IsNotNull (data, "#B1");
546
547                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
548                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
549
550                                 csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
551                                 html = data.GetData (DataFormats.Html) as string;
552                                 utext = data.GetData (DataFormats.UnicodeText) as string;
553                                 text = data.GetData (DataFormats.Text) as string;
554
555                                 Assert.AreEqual (string.Format (",A{0},Cell A1", Environment.NewLine), csv, "CSV B");
556                                 Assert.AreEqual (string.Format ("\tA{0}\tCell A1", Environment.NewLine), utext, "UTEXT B");
557                                 Assert.AreEqual (string.Format ("\tA{0}\tCell A1", Environment.NewLine), text, "TEXT B");
558                                 Assert.AreEqual (string.Format (@"Version:1.0{0}" +
559 "StartHTML:00000097{0}" +
560 "EndHTML:00000281{0}" +
561 "StartFragment:00000133{0}" +
562 "EndFragment:00000245{0}" +
563 "<HTML>{0}" +
564 "<BODY>{0}" +
565 "<!--StartFragment--><TABLE><THEAD><TH>&nbsp;</TH><TH>A</TH></THEAD><TR><TD ALIGN=\"center\">&nbsp;</TD><TD>Cell A1</TD></TR></TABLE>{0}" +
566 "<!--EndFragment-->{0}" +
567 "</BODY>{0}" +
568 "</HTML>", "\r\n"), html, "HTML B");
569
570                                 dgv.Rows [1].Cells [1].Selected = true;
571
572                                 data = dgv.GetClipboardContent ();
573                                 Assert.IsNotNull (data, "#C1");
574
575                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
576                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
577
578                                 csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
579                                 html = data.GetData (DataFormats.Html) as string;
580                                 utext = data.GetData (DataFormats.UnicodeText) as string;
581                                 text = data.GetData (DataFormats.Text) as string;
582
583                                 Assert.AreEqual (string.Format (",A,B{0},Cell A1,{0},,Cell B2", Environment.NewLine), csv, "CSV C");
584                                 Assert.AreEqual (string.Format ("\tA\tB{0}\tCell A1\t{0}\t\tCell B2", Environment.NewLine), utext, "UTEXT C");
585                                 Assert.AreEqual (string.Format ("\tA\tB{0}\tCell A1\t{0}\t\tCell B2", Environment.NewLine), text, "TEXT C");
586                                 string tmp;
587                                 tmp = string.Format (@"Version:1.0{0}" +
588 "StartHTML:00000097{0}" +
589 "EndHTML:00000266{0}" +
590 "StartFragment:00000133{0}" +
591 "EndFragment:00000230{0}" +
592 "<HTML>{0}" +
593 "<BODY>{0}" +
594 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
595 "<!--EndFragment-->{0}" +
596 "</BODY>{0}" +
597 "</HTML>", "\r\n");
598
599                                 Assert.AreEqual (string.Format (@"Version:1.0{0}" +
600 "StartHTML:00000097{0}" +
601 "EndHTML:00000376{0}" +
602 "StartFragment:00000133{0}" +
603 "EndFragment:00000340{0}" +
604 "<HTML>{0}" +
605 "<BODY>{0}" +
606 "<!--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}" +
607 "<!--EndFragment-->{0}" +
608 "</BODY>{0}" +
609 "</HTML>", "\r\n"), html, "HTML C");
610                         }
611                 }
612
613                 [Test]
614                 public void GetClipboardContents_HeadersNever ()
615                 {
616                         DataObject data;
617                         string csv, html, utext, text;
618
619                         using (DataGridView dgv = DataGridViewCommon.CreateAndFill ()) {
620                                 dgv.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
621                                 data = dgv.GetClipboardContent ();
622                                 Assert.IsNull (data, "#01");
623
624                                 dgv.Rows [0].Cells [0].Selected = true;
625
626                                 data = dgv.GetClipboardContent ();
627                                 Assert.IsNotNull (data, "#B1");
628
629                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#B2");
630                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#B3");
631
632                                 csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
633                                 html = data.GetData (DataFormats.Html) as string;
634                                 utext = data.GetData (DataFormats.UnicodeText) as string;
635                                 text = data.GetData (DataFormats.Text) as string;
636
637                                 Assert.AreEqual ("Cell A1", csv, "CSV B");
638                                 Assert.AreEqual ("Cell A1", utext, "UTEXT B");
639                                 Assert.AreEqual ("Cell A1", text, "TEXT B");
640                                 Assert.AreEqual (string.Format (@"Version:1.0{0}" +
641 "StartHTML:00000097{0}" +
642 "EndHTML:00000211{0}" +
643 "StartFragment:00000133{0}" +
644 "EndFragment:00000175{0}" +
645 "<HTML>{0}" +
646 "<BODY>{0}" +
647 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD></TR></TABLE>{0}" +
648 "<!--EndFragment-->{0}" +
649 "</BODY>{0}" +
650 "</HTML>", "\r\n"), html, "HTML B");
651
652                                 dgv.Rows [1].Cells [1].Selected = true;
653
654                                 data = dgv.GetClipboardContent ();
655                                 Assert.IsNotNull (data, "#C1");
656
657                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (), "#C2");
658                                 Assert.AreEqual (new string [] { DataFormats.CommaSeparatedValue, DataFormats.Html, DataFormats.UnicodeText, DataFormats.Text }, data.GetFormats (true), "#C3");
659
660                                 csv = data.GetData (DataFormats.CommaSeparatedValue) as string;
661                                 html = data.GetData (DataFormats.Html) as string;
662                                 utext = data.GetData (DataFormats.UnicodeText) as string;
663                                 text = data.GetData (DataFormats.Text) as string;
664
665                                 Assert.AreEqual (string.Format ("Cell A1,{0},Cell B2", Environment.NewLine), csv, "CSV C");
666                                 Assert.AreEqual (string.Format ("Cell A1\t{0}\tCell B2", Environment.NewLine), utext, "UTEXT C");
667                                 Assert.AreEqual (string.Format ("Cell A1\t{0}\tCell B2", Environment.NewLine), text, "TEXT C");
668                                 string tmp;
669                                 tmp = string.Format (@"Version:1.0{0}" +
670 "StartHTML:00000097{0}" +
671 "EndHTML:00000266{0}" +
672 "StartFragment:00000133{0}" +
673 "EndFragment:00000230{0}" +
674 "<HTML>{0}" +
675 "<BODY>{0}" +
676 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
677 "<!--EndFragment-->{0}" +
678 "</BODY>{0}" +
679 "</HTML>", "\r\n");
680
681                                 Assert.AreEqual (string.Format (@"Version:1.0{0}" +
682 "StartHTML:00000097{0}" +
683 "EndHTML:00000266{0}" +
684 "StartFragment:00000133{0}" +
685 "EndFragment:00000230{0}" +
686 "<HTML>{0}" +
687 "<BODY>{0}" +
688 "<!--StartFragment--><TABLE><TR><TD>Cell A1</TD><TD>&nbsp;</TD></TR><TR><TD>&nbsp;</TD><TD>Cell B2</TD></TR></TABLE>{0}" +
689 "<!--EndFragment-->{0}" +
690 "</BODY>{0}" +
691 "</HTML>", "\r\n"), html, "HTML C");
692                         }
693                 }
694                 
695                 [Test]
696                 public void EditingRow ()
697                 {
698                         using (DataGridView dgv = new DataGridView ()) {
699                                 Assert.AreEqual (true, dgv.AllowUserToAddRows, "1");
700                                 Assert.AreEqual (0, dgv.RowCount, "2");
701                                 Assert.AreEqual (-1, dgv.NewRowIndex, "3");
702                                 dgv.Columns.Add ("A", "B");
703                                 Assert.AreEqual (1, dgv.RowCount, "4");
704                                 
705                                 int added;
706                                 added = dgv.Rows.Add ("a");
707                                 Assert.AreEqual (0, added, "5");
708                         }
709                 }
710
711                 [Test] // bug 82226
712                 public void EditingRowAfterAddingColumns ()
713                 {
714                         using (DataGridView _dataGridView = new DataGridView ()) {
715                                 DataGridViewTextBoxColumn _nameTextBoxColumn;
716                                 DataGridViewTextBoxColumn _firstNameTextBoxColumn;
717                                 // 
718                                 // _nameTextBoxColumn
719                                 // 
720                                 _nameTextBoxColumn = new DataGridViewTextBoxColumn ();
721                                 _nameTextBoxColumn.HeaderText = "Name";
722                                 _dataGridView.Columns.Add (_nameTextBoxColumn);
723                                 // 
724                                 // _firstNameTextBoxColumn
725                                 // 
726                                 _firstNameTextBoxColumn = new DataGridViewTextBoxColumn ();
727                                 _firstNameTextBoxColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
728                                 _firstNameTextBoxColumn.HeaderText = "First Name";
729                                 _dataGridView.Columns.Add (_firstNameTextBoxColumn);
730
731                                 _dataGridView.Rows.Add ("de Icaza", "Miguel");
732                                 _dataGridView.Rows.Add ("Toshok", "Chris");
733                                 _dataGridView.Rows.Add ("Harper", "Jackson");
734                                 
735                                 Assert.AreEqual (4, _dataGridView.RowCount, "#01");
736                                 Assert.AreEqual (2, _dataGridView.Rows [3].Cells.Count, "#02");
737                         }
738                 }
739
740                 [Test]
741                 public void bug_81918 ()
742                 {
743                         using (DataGridView dgv = new DataGridView ()) {
744                                 DataGridViewColumn col = new DataGridViewComboBoxColumn ();
745                                 
746                                 dgv.Columns.Add (col);
747                                 
748                                 dgv.Rows.Add ("a");
749                                 
750                                 DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell) dgv [0, 0];
751                         }
752                 }
753
754                 // For testing row/column selection.
755                 List<List<int>> selections;
756                 void DataGridView_RowSelectionChanged (object sender, EventArgs e)
757                 {
758                         // Make a list of selected rows.
759                         DataGridView dgv = sender as DataGridView;
760                         List<int> selection = new List<int> ();
761                         foreach (DataGridViewRow row in dgv.SelectedRows)
762                                 selection.Add (row.Index);
763                         selections.Add (selection);
764                 }
765                 void DataGridView_ColumnSelectionChanged (object sender, EventArgs e)
766                 {
767                         // Make a list of selected columns.
768                         DataGridView dgv = sender as DataGridView;
769                         List<int> selection = new List<int> ();
770                         foreach (DataGridViewColumn column in dgv.SelectedColumns)
771                                 selection.Add (column.Index);
772                         selections.Add (selection);
773                 }
774
775                 // Used to generate printable representation of selections.
776                 string ListListIntToString (List<List<int>> selections)
777                 {
778                         List<string> selectionsList = new List<string> ();
779                         foreach (List<int> selection in selections)
780                         {
781                                 List<string> selectionList = new List<string> ();
782                                 foreach (int selectionNo in selection)
783                                         selectionList.Add (selectionNo.ToString ("D"));
784                                 selectionsList.Add ("<" + string.Join (",", selectionList.ToArray()) + ">");
785                         }
786                         return string.Join (",", selectionsList.ToArray());
787
788                         // (Here is the disallowed Linq version.)
789                         /* return string.Join (",", (selections.Select ((List<int> x)
790                                 => "<" + string.Join (",", (x.Select ((int y)
791                                         => (y.ToString("D")))).ToArray()) + ">").ToArray())); */
792                 }
793
794                 [Test]
795                 public void SelectedRowsTest ()
796                 {
797                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
798                                 dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
799
800                                 // Prepare to test the SelectionChanged event.
801                                 selections = new List<List<int>> ();
802                                 List<List<int>> expectedSelections = new List<List<int>> ();
803                                 dgv.SelectionChanged += new EventHandler (DataGridView_RowSelectionChanged);
804
805                                 // Make sure there's no selection to begin with.
806                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "1-10");
807
808                                 // Select a row.
809                                 dgv.Rows [1].Selected = true;
810                                 Assert.AreEqual (1, dgv.SelectedRows.Count, "1-1");
811                                 Assert.AreEqual (1, dgv.SelectedRows [0].Index, "1-2");
812                                 expectedSelections.Add (new List<int> { 1 });
813
814                                 // Select another row.
815                                 dgv.Rows [3].Selected = true;
816                                 Assert.AreEqual (2, dgv.SelectedRows.Count, "1-3");
817                                 Assert.AreEqual (3, dgv.SelectedRows [0].Index, "1-4");
818                                 Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-5");
819                                 expectedSelections.Add (new List<int> { 3, 1 });
820
821                                 // Select another row.
822                                 dgv.Rows [2].Selected = true;
823                                 Assert.AreEqual (3, dgv.SelectedRows.Count, "1-6");
824                                 Assert.AreEqual (2, dgv.SelectedRows [0].Index, "1-7");
825                                 Assert.AreEqual (3, dgv.SelectedRows [1].Index, "1-8");
826                                 Assert.AreEqual (1, dgv.SelectedRows [2].Index, "1-9");
827                                 expectedSelections.Add (new List<int> { 2, 3, 1 });
828
829                                 // Unselect a row.
830                                 dgv.Rows [2].Selected = false;
831                                 Assert.AreEqual (2, dgv.SelectedRows.Count, "1-11");
832                                 Assert.AreEqual (3, dgv.SelectedRows [0].Index, "1-12");
833                                 Assert.AreEqual (1, dgv.SelectedRows [1].Index, "1-13");
834                                 expectedSelections.Add (new List<int> { 3, 1 });
835
836                                 // Delete a row.  This clears the selection.
837                                 dgv.Rows.RemoveAt (2);
838                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "1-14");
839                                 expectedSelections.Add (new List<int> { });
840
841                                 // Make sure the SelectionChanged event was called when expected.
842                                 string selectionsText = ListListIntToString (selections);
843                                 string expectedSelectionsText = ListListIntToString (expectedSelections);
844                                 Assert.AreEqual (expectedSelectionsText, selectionsText, "1-15");
845                         }
846
847                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
848                                 dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
849                                 dgv.Rows [1].Selected = true;
850                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "3-1");
851                                 dgv.Rows [3].Selected = true;
852                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "3-3");
853                                 dgv.Rows [2].Selected = true;
854                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "3-6");
855                         }
856
857                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
858                                 foreach (DataGridViewColumn col in dgv.Columns)
859                                         col.SortMode = DataGridViewColumnSortMode.NotSortable;
860                                 dgv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
861                                 dgv.Rows [1].Selected = true;
862                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "4-1");
863                                 dgv.Rows [3].Selected = true;
864                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "4-3");
865                                 dgv.Rows [2].Selected = true;
866                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "4-6");
867                         }
868
869                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
870                                 foreach (DataGridViewColumn col in dgv.Columns)
871                                         col.SortMode = DataGridViewColumnSortMode.NotSortable;
872                                 dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
873                                 dgv.Rows [1].Selected = true;
874                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "5-1");
875                                 dgv.Rows [3].Selected = true;
876                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "5-3");
877                                 dgv.Rows [2].Selected = true;
878                                 Assert.AreEqual (0, dgv.SelectedRows.Count, "5-6");
879                         }
880                 }
881
882                 [Test] // bug #325979
883                 public void SelectedRows_FindColumnByName ()
884                 {
885                         DataTable dt = new DataTable ();
886                         dt.Columns.Add ("Date", typeof (DateTime));
887                         dt.Columns.Add ("Registered", typeof (bool));
888                         dt.Columns.Add ("Event", typeof (string));
889
890                         DataRow row = dt.NewRow ();
891                         row ["Date"] = new DateTime (2007, 2, 3);
892                         row ["Event"] = "one";
893                         row ["Registered"] = false;
894                         dt.Rows.Add (row);
895
896                         row = dt.NewRow ();
897                         row ["Date"] = new DateTime (2008, 3, 4);
898                         row ["Event"] = "two";
899                         row ["Registered"] = true;
900                         dt.Rows.Add (row);
901
902                         DataGridView dgv = new DataGridView ();
903                         dgv.DataSource = dt;
904
905                         Form form = new Form ();
906                         form.ShowInTaskbar = false;
907                         form.Controls.Add (dgv);
908                         form.Show ();
909
910                         dgv.Rows [1].Selected = true;
911
912                         DataGridViewCell cell = dgv.SelectedRows [0].Cells ["DaTE"];
913                         Assert.IsNotNull (cell, "#A1");
914                         Assert.IsNotNull (cell.OwningColumn, "#A2");
915                         Assert.AreEqual ("Date", cell.OwningColumn.Name, "#A3");
916                         Assert.IsNotNull (cell.Value, "#A4");
917                         Assert.AreEqual (new DateTime (2008, 3, 4), cell.Value, "#A5");
918
919                         cell = dgv.SelectedRows [0].Cells ["Event"];
920                         Assert.IsNotNull (cell, "#B1");
921                         Assert.IsNotNull (cell.OwningColumn, "#B2");
922                         Assert.AreEqual ("Event", cell.OwningColumn.Name, "#B3");
923                         Assert.IsNotNull (cell.Value, "#B3");
924                         Assert.AreEqual ("two", cell.Value, "#B4");
925
926                         form.Dispose ();
927                 }
928
929                 [Test]
930                 public void SelectedColumnsTest ()
931                 {
932                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
933                                 foreach (DataGridViewColumn col in dgv.Columns)
934                                         col.SortMode = DataGridViewColumnSortMode.NotSortable;
935                                 dgv.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
936
937                                 // Prepare to test the SelectionChanged event.
938                                 selections = new List<List<int>> ();
939                                 List<List<int>> expectedSelections = new List<List<int>> ();
940                                 dgv.SelectionChanged += new EventHandler (DataGridView_ColumnSelectionChanged);
941
942                                 // Make sure there's no selection to begin with.
943                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "1-13");
944
945                                 // Select a column.
946                                 dgv.Columns [1].Selected = true;
947                                 Assert.AreEqual (1, dgv.SelectedColumns.Count, "1-1");
948                                 Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "1-2");
949                                 expectedSelections.Add (new List<int> { 1 });
950
951                                 // Select another column.
952                                 dgv.Columns [3].Selected = true;
953                                 Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-3");
954                                 Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "1-4");
955                                 Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-5");
956                                 expectedSelections.Add (new List<int> { 3, 1 });
957
958                                 // Select another column.
959                                 dgv.Columns [2].Selected = true;
960                                 Assert.AreEqual (3, dgv.SelectedColumns.Count, "1-6");
961                                 Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "1-7");
962                                 Assert.AreEqual (3, dgv.SelectedColumns [1].Index, "1-8");
963                                 Assert.AreEqual (1, dgv.SelectedColumns [2].Index, "1-9");
964                                 expectedSelections.Add (new List<int> { 2, 3, 1 });
965
966                                 // Unselect a column.
967                                 dgv.Columns [2].Selected = false;
968                                 Assert.AreEqual (2, dgv.SelectedColumns.Count, "1-10");
969                                 Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "1-11");
970                                 Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "1-12");
971                                 expectedSelections.Add (new List<int> { 3, 1 });
972
973                                 // Delete a column.  This clears the selection.
974                                 // The event handler gets called twice.
975                                 dgv.Columns.RemoveAt (2);
976                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "1-14");
977                                 expectedSelections.Add (new List<int> { });
978                                 expectedSelections.Add (new List<int> { });
979
980                                 // Make sure the SelectionChanged event was called when expected.
981                                 string selectionsText = ListListIntToString (selections);
982                                 string expectedSelectionsText = ListListIntToString (expectedSelections);
983                                 Assert.AreEqual (expectedSelectionsText, selectionsText, "1-15");
984                         }
985
986                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
987                                 foreach (DataGridViewColumn col in dgv.Columns)
988                                         col.SortMode = DataGridViewColumnSortMode.NotSortable;
989                                 dgv.SelectionMode = DataGridViewSelectionMode.ColumnHeaderSelect;
990
991                                 // Prepare to test the SelectionChanged event.
992                                 selections = new List<List<int>> ();
993                                 List<List<int>> expectedSelections = new List<List<int>> ();
994                                 dgv.SelectionChanged += new EventHandler (DataGridView_ColumnSelectionChanged);
995
996                                 // Make sure there's no selection to begin with.
997                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "2-10");
998
999                                 // Select a column.
1000                                 dgv.Columns [1].Selected = true;
1001                                 Assert.AreEqual (1, dgv.SelectedColumns.Count, "2-1");
1002                                 Assert.AreEqual (1, dgv.SelectedColumns [0].Index, "2-2");
1003                                 expectedSelections.Add (new List<int> { 1 });
1004
1005                                 // Select another column.
1006                                 dgv.Columns [3].Selected = true;
1007                                 Assert.AreEqual (2, dgv.SelectedColumns.Count, "2-3");
1008                                 Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "2-4");
1009                                 Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "2-5");
1010                                 expectedSelections.Add (new List<int> { 3, 1 });
1011
1012                                 // Select another column.
1013                                 dgv.Columns [2].Selected = true;
1014                                 Assert.AreEqual (3, dgv.SelectedColumns.Count, "2-6");
1015                                 Assert.AreEqual (2, dgv.SelectedColumns [0].Index, "2-7");
1016                                 Assert.AreEqual (3, dgv.SelectedColumns [1].Index, "2-8");
1017                                 Assert.AreEqual (1, dgv.SelectedColumns [2].Index, "2-9");
1018                                 expectedSelections.Add (new List<int> { 2, 3, 1 });
1019
1020                                 // Unselect another column.
1021                                 dgv.Columns [2].Selected = false;
1022                                 Assert.AreEqual (2, dgv.SelectedColumns.Count, "2-11");
1023                                 Assert.AreEqual (3, dgv.SelectedColumns [0].Index, "2-12");
1024                                 Assert.AreEqual (1, dgv.SelectedColumns [1].Index, "2-13");
1025                                 expectedSelections.Add (new List<int> { 3, 1 });
1026
1027                                 // Make sure the SelectionChanged event was called when expected.
1028                                 string selectionsText = ListListIntToString (selections);
1029                                 string expectedSelectionsText = ListListIntToString (expectedSelections);
1030                                 Assert.AreEqual (expectedSelectionsText, selectionsText, "2-14");
1031                         }
1032
1033                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
1034                                 dgv.SelectionMode = DataGridViewSelectionMode.CellSelect;
1035                                 dgv.Columns [1].Selected = true;
1036                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-1");
1037                                 dgv.Columns [3].Selected = true;
1038                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-3");
1039                                 dgv.Columns [2].Selected = true;
1040                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "3-6");
1041                         }
1042
1043                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
1044                                 foreach (DataGridViewColumn col in dgv.Columns)
1045                                         col.SortMode = DataGridViewColumnSortMode.NotSortable;
1046                                 dgv.SelectionMode = DataGridViewSelectionMode.RowHeaderSelect;
1047                                 dgv.Columns [1].Selected = true;
1048                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-1");
1049                                 dgv.Columns [3].Selected = true;
1050                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-3");
1051                                 dgv.Columns [2].Selected = true;
1052                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "4-6");
1053                         }
1054
1055                         using (DataGridView dgv = DataGridViewCommon.CreateAndFillBig ()) {
1056                                 foreach (DataGridViewColumn col in dgv.Columns)
1057                                         col.SortMode = DataGridViewColumnSortMode.NotSortable;
1058                                 dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
1059                                 dgv.Columns [1].Selected = true;
1060                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-1");
1061                                 dgv.Columns [3].Selected = true;
1062                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-3");
1063                                 dgv.Columns [2].Selected = true;
1064                                 Assert.AreEqual (0, dgv.SelectedColumns.Count, "5-6");
1065                         }
1066                 }
1067
1068                 [Test]
1069                 public void TopLeftHeaderCellTest ()
1070                 {
1071                         Assert.Ignore("Missing quite a few bits still");
1072                         
1073                         using (DataGridView dgv = new DataGridView ()) {
1074                                 DataGridViewHeaderCell cell = dgv.TopLeftHeaderCell;
1075                                 
1076                                 cell = dgv.TopLeftHeaderCell;
1077
1078                                 Assert.IsNotNull (cell, "#01");
1079                                 Assert.AreEqual (cell.DataGridView, dgv, "#02");
1080                                 Assert.AreEqual ("DataGridViewTopLeftHeaderCell", cell.GetType ().Name, "#03");
1081                                                                 
1082                                 Assert.IsNotNull (cell.AccessibilityObject, "#cell.AccessibilityObject");
1083                                 Assert.AreEqual (-1, cell.ColumnIndex, "#cell.ColumnIndex");
1084                                 // /* NIE for the moment... */ Assert.IsNotNull (cell.ContentBounds, "#cell.ContentBounds");
1085                                 Assert.IsNull (cell.ContextMenuStrip, "#cell.ContextMenuStrip");
1086                                 Assert.IsNotNull (cell.DataGridView, "#cell.DataGridView");
1087                                 Assert.IsNull (cell.DefaultNewRowValue, "#cell.DefaultNewRowValue");
1088                                 Assert.AreEqual (false, cell.Displayed, "#cell.Displayed");
1089                                 // /* NIE for the moment... */ Assert.AreEqual (@"", cell.EditedFormattedValue, "#cell.EditedFormattedValue");
1090                                 Assert.IsNotNull (cell.EditType, "#cell.EditType");
1091                                 Assert.IsNotNull (cell.ErrorIconBounds, "#cell.ErrorIconBounds");
1092                                 Assert.AreEqual (@"", cell.ErrorText, "#cell.ErrorText");
1093                                 // /* NIE for the moment... */ Assert.AreEqual (@"", cell.FormattedValue, "#cell.FormattedValue");
1094                                 Assert.IsNotNull (cell.FormattedValueType, "#cell.FormattedValueType");
1095                                 // /* NIE for the moment... */ Assert.AreEqual (true, cell.Frozen, "#cell.Frozen");
1096                                 Assert.AreEqual (false, cell.HasStyle, "#cell.HasStyle");
1097                                 Assert.AreEqual (DataGridViewElementStates.Frozen | DataGridViewElementStates.ReadOnly | DataGridViewElementStates.Resizable | DataGridViewElementStates.ResizableSet | DataGridViewElementStates.Visible, cell.InheritedState, "#cell.InheritedState");
1098                                 Assert.IsNotNull (cell.InheritedStyle, "#cell.InheritedStyle");
1099                                 try {
1100                                         object zxf = cell.IsInEditMode;
1101                                         TestHelper.RemoveWarning (zxf);
1102                                         Assert.Fail ("Expected 'System.InvalidOperationException', but no exception was thrown.", "#cell.IsInEditMode");
1103                                 } catch (InvalidOperationException ex) {
1104                                         Assert.AreEqual (@"Operation cannot be performed on a cell of a shared row.", ex.Message);
1105                                 } catch (Exception ex) {
1106                                         Assert.Fail ("Expected 'System.InvalidOperationException', got '" + ex.GetType ().FullName + "'.", "#cell.IsInEditMode");
1107                                 }
1108                                 Assert.IsNull (cell.OwningColumn, "#cell.OwningColumn");
1109                                 Assert.IsNull (cell.OwningRow, "#cell.OwningRow");
1110                                 Assert.IsNotNull (cell.PreferredSize, "#cell.PreferredSize");
1111                                 Assert.AreEqual (true, cell.ReadOnly, "#cell.ReadOnly");
1112                                 Assert.AreEqual (true, cell.Resizable, "#cell.Resizable");
1113                                 Assert.AreEqual (-1, cell.RowIndex, "#cell.RowIndex");
1114                                 Assert.AreEqual (false, cell.Selected, "#cell.Selected");
1115                                 Assert.IsNotNull (cell.Size, "#cell.Size");
1116                                 Assert.AreEqual (DataGridViewElementStates.None, cell.State, "#cell.State");
1117                                 if (cell.HasStyle)
1118                                         Assert.IsNotNull (cell.Style, "#cell.Style");
1119                                 Assert.IsNull (cell.Tag, "#cell.Tag");
1120                                 Assert.AreEqual (@"", cell.ToolTipText, "#cell.ToolTipText");
1121                                 Assert.IsNull (cell.Value, "#cell.Value");
1122                                 Assert.IsNotNull (cell.ValueType, "#cell.ValueType");
1123                                 Assert.AreEqual (true, cell.Visible, "#cell.Visible");
1124                         }
1125                 }
1126
1127                 [Test]
1128                 public void TestDefaultValues ()
1129                 {
1130                         DataGridView grid = new DataGridView ();
1131                         Assert.AreEqual (true, grid.AllowUserToAddRows, "#A1");
1132                         Assert.AreEqual (true, grid.AllowUserToDeleteRows, "#A2");
1133                         Assert.AreEqual (false, grid.AllowUserToOrderColumns, "#A3");
1134                         Assert.AreEqual (true, grid.AllowUserToResizeColumns, "#A4");
1135                         Assert.AreEqual (true, grid.AllowUserToResizeRows, "#A5");
1136                         Assert.AreEqual (new DataGridViewCellStyle(), grid.AlternatingRowsDefaultCellStyle, "#A6");
1137                         Assert.AreEqual (true, grid.AutoGenerateColumns, "#A7");
1138                         Assert.AreEqual (DataGridViewAutoSizeRowsMode.None, grid.AutoSizeRowsMode, "#A8");
1139                         Assert.AreEqual (Control.DefaultBackColor, grid.BackColor, "#A9");
1140                         Assert.AreEqual (SystemColors.AppWorkspace, grid.BackgroundColor, "#A10");
1141                         Assert.AreEqual (BorderStyle.FixedSingle, grid.BorderStyle, "#A11");
1142                         Assert.AreEqual (DataGridViewClipboardCopyMode.EnableWithAutoHeaderText, grid.ClipboardCopyMode, "#A12");
1143                         Assert.AreEqual (DataGridViewColumnHeadersHeightSizeMode.EnableResizing, grid.ColumnHeadersHeightSizeMode, "#A21");
1144                         Assert.AreEqual (true, grid.ColumnHeadersVisible, "#A22");
1145                         Assert.AreEqual (String.Empty, grid.DataMember, "#A23");
1146                         Assert.AreEqual (DataGridViewEditMode.EditOnKeystrokeOrF2, grid.EditMode, "#A31");
1147                         Assert.AreEqual (Control.DefaultFont, grid.Font, "#A32");
1148                         Assert.AreEqual (Control.DefaultForeColor, grid.ForeColor, "#A33");
1149                         Assert.AreEqual (Color.FromKnownColor(KnownColor.ControlDark), grid.GridColor, "#A34");
1150                         Assert.AreEqual (true, grid.MultiSelect, "#A35");
1151                         Assert.AreEqual (grid.Rows.Count - 1, grid.NewRowIndex, "#A36");
1152                         Assert.AreEqual (Padding.Empty, grid.Padding, "#A37");
1153                         Assert.AreEqual (false, grid.ReadOnly, "#A38");
1154                         Assert.AreEqual (true, grid.RowHeadersVisible, "#A39");
1155                         Assert.AreEqual (41, grid.RowHeadersWidth, "#A40");
1156                         Assert.AreEqual (DataGridViewSelectionMode.RowHeaderSelect, grid.SelectionMode, "#A41");
1157                         Assert.AreEqual (true, grid.ShowCellErrors, "#A42");
1158                         Assert.AreEqual (true, grid.ShowEditingIcon, "#A43");
1159                         Assert.AreEqual (Cursors.Default, grid.UserSetCursor, "#A44");
1160                         Assert.AreEqual (false, grid.VirtualMode, "#A45");
1161                 }
1162
1163 #region AutoSizeColumnsModeExceptions
1164
1165                 [Test]
1166                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1167                 public void TestAutoSizeColumnsModeInvalidEnumArgumentException ()
1168                 {
1169                         DataGridView grid = new DataGridView();
1170                         grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill | DataGridViewAutoSizeColumnsMode.None;
1171                 }
1172
1173                 [Test]
1174                 [ExpectedException (typeof (InvalidOperationException))]
1175                 public void TestAutoSizeColumnsModeInvalidOperationException1 ()
1176                 {
1177                         DataGridView grid = new DataGridView ();
1178                         grid.ColumnHeadersVisible = false;
1179                         DataGridViewColumn col = new DataGridViewColumn ();
1180                         col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
1181                         grid.Columns.Add (col);
1182                         grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
1183                 }
1184
1185                 [Test]
1186                 [ExpectedException (typeof (InvalidOperationException))]
1187                 public void TestAutoSizeColumnsModeInvalidOperationException2 ()
1188                 {
1189                         DataGridView grid = new DataGridView ();
1190                         DataGridViewColumn col = new DataGridViewColumn ();
1191                         col.AutoSizeMode = DataGridViewAutoSizeColumnMode.NotSet;
1192                         col.Frozen = true;
1193                         grid.Columns.Add (col);
1194                         grid.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
1195                 }
1196
1197 #endregion
1198
1199 #region AutoSizeRowsModeExceptions
1200
1201                 [Test]
1202                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1203                 public void TestAutoSizeRowsModeInvalidEnumArgumentException ()
1204                 {
1205                         DataGridView grid = new DataGridView ();
1206                         grid.AutoSizeRowsMode = (DataGridViewAutoSizeRowsMode) 4;
1207                 }
1208
1209                 [Test]
1210                 [ExpectedException (typeof (InvalidOperationException))]
1211                 public void TestAutoSizeRowsModeInvalidOperationException1 ()
1212                 {
1213                         DataGridView grid = new DataGridView ();
1214                         grid.RowHeadersVisible = false;
1215                         grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllHeaders;
1216                 }
1217
1218                 [Test]
1219                 [ExpectedException (typeof (InvalidOperationException))]
1220                 public void TestAutoSizeRowsModeInvalidOperationException2 ()
1221                 {
1222                         DataGridView grid = new DataGridView ();
1223                         grid.RowHeadersVisible = false;
1224                         grid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.DisplayedHeaders;
1225                 }
1226
1227 #endregion
1228
1229                 [Test]
1230                 public void AutoResizeColumTest ()
1231                 {
1232                         using (Form f = new Form ()) {
1233                                 f.Show ();
1234                                 using (DataGridView dgv = new DataGridView ()) {
1235                                         f.Controls.Add (dgv);
1236                                         
1237                                         DataGridViewColumn col, col2, col3;
1238                                         
1239                                         Assert.AreEqual ("{Width=240, Height=150}", dgv.ClientSize.ToString (), "#01");
1240                                         
1241                                         col = new DataGridViewColumn ();
1242                                         col.MinimumWidth = 20;
1243                                         col.FillWeight = 20;
1244                                         col.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
1245                                         col.CellTemplate = new DataGridViewTextBoxCell ();
1246                                         
1247                                         Assert.AreEqual (100, col.Width, "#02");
1248                                         dgv.Columns.Add (col);
1249                                         
1250                                         Assert.AreEqual (197, col.Width, "#03");
1251
1252                                         col2 = new DataGridViewColumn ();
1253                                         col2.MinimumWidth = 20;
1254                                         col2.FillWeight = 40;
1255                                         col2.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
1256                                         col2.CellTemplate = new DataGridViewTextBoxCell (); ;
1257                                         dgv.Columns.Add (col2);
1258
1259                                         Assert.AreEqual (66, col.Width, "#04");
1260                                         Assert.AreEqual (131, col2.Width, "#05");
1261
1262                                         col3 = new DataGridViewColumn ();
1263                                         col3.MinimumWidth = 20;
1264                                         col3.FillWeight = 5;
1265                                         col3.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
1266                                         col3.CellTemplate = new DataGridViewTextBoxCell (); ;
1267                                         dgv.Columns.Add (col3);
1268
1269                                         Assert.AreEqual (59, col.Width, "#04");
1270                                         Assert.AreEqual (118, col2.Width, "#05");
1271                                         Assert.AreEqual (20, col3.Width, "#05");
1272                                 }
1273                         }
1274                 }
1275
1276                 [Test]
1277                 public void ControlsTest ()
1278                 {
1279                         using (DataGridView grid = new DataGridView ()) {
1280                                 Assert.AreEqual ("DataGridViewControlCollection", grid.Controls.GetType ().Name, "#01");
1281                                 Assert.AreEqual (2, grid.Controls.Count, "#02");
1282                         }
1283                 }
1284         
1285                 [Test]
1286                 [ExpectedException (typeof (ArgumentException))]
1287                 public void TestBackgroundColorArgumentException ()
1288                 {
1289                         DataGridView grid = new DataGridView ();
1290                         grid.BackgroundColor = Color.Empty;
1291                 }
1292
1293                 [Test]
1294                 [ExpectedException(typeof(InvalidEnumArgumentException))]
1295                 public void TestBorderStyleInvalidEnumArgumentException ()
1296                 {
1297                         DataGridView grid = new DataGridView ();
1298                         grid.BorderStyle = BorderStyle.FixedSingle | BorderStyle.Fixed3D;
1299                 }
1300
1301                 [Test]
1302                 public void ColumnCount ()
1303                 {
1304                         DataGridView dgv = new DataGridView ();
1305
1306                         dgv.RowCount = 10;
1307                         dgv.ColumnCount = 2;
1308
1309                         Assert.AreEqual (10, dgv.RowCount, "A1");
1310                         Assert.AreEqual (2, dgv.ColumnCount, "A2");
1311
1312                         dgv.ColumnCount = 1;
1313
1314                         Assert.AreEqual (10, dgv.RowCount, "B1");
1315                         Assert.AreEqual (1, dgv.ColumnCount, "B2");
1316
1317                         dgv.ColumnCount = 3;
1318
1319                         Assert.AreEqual (10, dgv.RowCount, "C1");
1320                         Assert.AreEqual (3, dgv.ColumnCount, "C2");
1321
1322
1323                         dgv.ColumnCount = 0;
1324
1325                         Assert.AreEqual (0, dgv.RowCount, "D1");
1326                         Assert.AreEqual (0, dgv.ColumnCount, "D2");
1327
1328                         Assert.AreEqual (0, dgv.ColumnCount, "E1");
1329
1330                         try {
1331                                 dgv.ColumnCount = -1;
1332                                 Assert.Fail ("F1");
1333                         } catch (ArgumentOutOfRangeException ex) {
1334                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "F2");
1335                                 Assert.IsNotNull (ex.Message, "F3");
1336                                 Assert.IsNotNull (ex.ParamName, "F4");
1337                                 Assert.AreEqual ("ColumnCount", ex.ParamName, "F5");
1338                                 Assert.IsNull (ex.InnerException, "F6");
1339                         }
1340                 }
1341
1342                 [Test]
1343                 public void ColumnCountIncrease ()
1344                 {
1345                         DataGridView dgv = new DataGridView ();
1346                         dgv.ColumnCount = 1;
1347                         
1348                         // Increasing the ColumnCount adds TextBoxColumns, not generic columns
1349                         Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[0].GetType ().ToString (), "A1");
1350                 }
1351
1352
1353                 [Test]
1354                 public void ColumnCountDecrease ()
1355                 {
1356                         DataGridView dgv = new DataGridView ();
1357                         dgv.ColumnCount = 6;
1358                         Assert.AreEqual (6, dgv.ColumnCount, "A1");
1359
1360                         dgv.ColumnCount = 3;
1361                         Assert.AreEqual (3, dgv.ColumnCount, "A2");
1362                         
1363                         // Increasing the ColumnCount adds TextBoxColumns, not generic columns
1364                         Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[0].GetType ().ToString (), "A3");
1365                         Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[1].GetType ().ToString (), "A4");
1366                         Assert.AreEqual ("System.Windows.Forms.DataGridViewTextBoxColumn", dgv.Columns[2].GetType ().ToString (), "A5");
1367                 }
1368
1369                 private class DataItem
1370                 {
1371                         public string Text {
1372                                 get { return String.Empty; }
1373                         }
1374
1375                         [Browsable (false)]
1376                         public string NotVisible {
1377                                 get { return String.Empty; }
1378                         }
1379                 }
1380
1381                 [Test]
1382                 public void NoDuplicateAutoGeneratedColumn ()
1383                 {
1384                         List<DataItem> dataList = new List<DataItem> ();
1385                         dataList.Add (new DataItem ());
1386                         dataList.Add (new DataItem ());
1387
1388                         DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn ();
1389                         column.DataPropertyName = "Text";
1390                         column.HeaderText = "Custom Column";
1391                         grid.Columns.Add (column);
1392
1393                         grid.DataSource = dataList;
1394                         // Test that the column autogeneration hasn't generated duplicate column 
1395                         // for the property Text
1396                         Assert.AreEqual (1, grid.Columns.Count, "#1");
1397                         Assert.AreEqual ("Custom Column", grid.Columns[0].HeaderText, "#2");
1398                 }
1399
1400                 [Test]
1401                 [ExpectedException (typeof (InvalidOperationException))]
1402                 public void TestColumnCountInvalidOperationException ()
1403                 {
1404                         DataGridView grid = new DataGridView ();
1405                         grid.DataSource = new ArrayList ();
1406                         grid.ColumnCount = 0;
1407                 }
1408
1409                 [Test]
1410                 public void ColumnHeadersHeight ()
1411                 {
1412                         DataGridView grid = new DataGridView ();
1413                         Assert.AreEqual (23, grid.ColumnHeadersHeight, "#A1");
1414                         grid.ColumnHeadersHeight = 4;
1415                         Assert.AreEqual (4, grid.ColumnHeadersHeight, "#A2");
1416                         grid.ColumnHeadersHeight = 32768;
1417                         Assert.AreEqual (32768, grid.ColumnHeadersHeight, "#A3");
1418
1419                         try {
1420                                 grid.ColumnHeadersHeight = 3;
1421                                 Assert.Fail ("#B1");
1422                         } catch (ArgumentOutOfRangeException ex) {
1423                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
1424                                 Assert.IsNotNull (ex.Message, "#B3");
1425                                 Assert.IsNotNull (ex.ParamName, "#B4");
1426                                 Assert.AreEqual ("ColumnHeadersHeight", ex.ParamName, "#B5");
1427                                 Assert.IsNull (ex.InnerException, "#B6");
1428                         }
1429
1430                         try {
1431                                 grid.ColumnHeadersHeight = 32769;
1432                                 Assert.Fail ("#C1");
1433                         } catch (ArgumentOutOfRangeException ex) {
1434                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
1435                                 Assert.IsNotNull (ex.Message, "#C3");
1436                                 Assert.IsNotNull (ex.ParamName, "#C4");
1437                                 Assert.AreEqual ("ColumnHeadersHeight", ex.ParamName, "#C5");
1438                                 Assert.IsNull (ex.InnerException, "#C6");
1439                         }
1440                 }
1441
1442                 [Test]
1443                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1444                 public void TestColumnHeadersHeightSizeModeInvalidEnumArgumentException ()
1445                 {
1446                         DataGridView grid = new DataGridView ();
1447                         grid.ColumnHeadersHeightSizeMode = (DataGridViewColumnHeadersHeightSizeMode) 3;
1448                 }
1449
1450                 [Test]
1451                 public void RowHeadersWidth ()
1452                 {
1453                         DataGridView grid = new DataGridView();
1454                         Assert.AreEqual (41, grid.RowHeadersWidth, "#A1");
1455                         grid.RowHeadersWidth = 4;
1456                         Assert.AreEqual (4, grid.RowHeadersWidth, "#A2");
1457                         grid.RowHeadersWidth = 32768;
1458                         Assert.AreEqual (32768, grid.RowHeadersWidth, "#A3");
1459
1460                         try {
1461                                 grid.RowHeadersWidth = 3;
1462                                 Assert.Fail ("#B1");
1463                         } catch (ArgumentOutOfRangeException ex) {
1464                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#B2");
1465                                 Assert.IsNotNull (ex.Message, "#B3");
1466                                 Assert.IsNotNull (ex.ParamName, "#B4");
1467                                 Assert.AreEqual ("RowHeadersWidth", ex.ParamName, "#B5");
1468                                 Assert.IsNull (ex.InnerException, "#B6");
1469                         }
1470
1471                         try {
1472                                 grid.RowHeadersWidth = 32769;
1473                                 Assert.Fail ("#C1");
1474                         } catch (ArgumentOutOfRangeException ex) {
1475                                 Assert.AreEqual (typeof (ArgumentOutOfRangeException), ex.GetType (), "#C2");
1476                                 Assert.IsNotNull (ex.Message, "#C3");
1477                                 Assert.IsNotNull (ex.ParamName, "#C4");
1478                                 Assert.AreEqual ("RowHeadersWidth", ex.ParamName, "#C5");
1479                                 Assert.IsNull (ex.InnerException, "#C6");
1480                         }
1481                 }
1482
1483                 [Test]
1484                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1485                 public void TestDataGridViewRowHeadersWidthSizeModeInvalidEnumArgumentException () {
1486                         DataGridView grid = new DataGridView ();
1487                         grid.RowHeadersWidthSizeMode = (DataGridViewRowHeadersWidthSizeMode) 5;
1488                 }
1489
1490                 [Test]
1491                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1492                 public void TestScrollBarsInvalidEnumArgumentException ()
1493                 {
1494                         DataGridView grid = new DataGridView ();
1495                         grid.ScrollBars = (ScrollBars) 4;
1496                 }
1497
1498                 [Test]
1499                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1500                 public void TestSelectionModeInvalidEnumArgumentException ()
1501                 {
1502                         DataGridView grid = new DataGridView ();
1503                         grid.SelectionMode = (DataGridViewSelectionMode) 5;
1504                 }
1505
1506                 [Test]
1507                 [ExpectedException (typeof (InvalidEnumArgumentException))]
1508                 public void TestAutoResizeRowsInvalidEnumArgumentException ()
1509                 {
1510                         DataGridView grid = new DataGridView ();
1511                         grid.AutoResizeRows ((DataGridViewAutoSizeRowsMode) 4);
1512                 }
1513
1514                 [Test]
1515                 [ExpectedException (typeof (InvalidOperationException))]
1516                 public void TestAutoResizeRowsInvalidOperationException1 ()
1517                 {
1518                         DataGridView grid = new DataGridView ();
1519                         grid.RowHeadersVisible = false;
1520                         grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.AllHeaders);
1521                 }
1522
1523                 [Test]
1524                 [ExpectedException (typeof (InvalidOperationException))]
1525                 public void TestAutoResizeRowsInvalidOperationException2 ()
1526                 {
1527                         DataGridView grid = new DataGridView ();
1528                         grid.RowHeadersVisible = false;
1529                         grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.DisplayedHeaders);
1530                 }
1531
1532                 [Test]
1533                 [ExpectedException (typeof (ArgumentException))]
1534                 public void TestAutoResizeRowsArgumentException ()
1535                 {
1536                         DataGridView grid = new DataGridView ();
1537                         grid.AutoResizeRows (DataGridViewAutoSizeRowsMode.None);
1538                 }
1539
1540                 [Test]
1541                 public void DefaultSize ()
1542                 {
1543                         MockDataGridView grid = new MockDataGridView ();
1544                         Assert.AreEqual (new Size (240, 150), grid.default_size, "#1");
1545                         Assert.AreEqual (new Size (240, 150), grid.Size, "#2");
1546                 }
1547
1548                 [Test]
1549                 public void ColumnHeadersDefaultCellStyle ()
1550                 {
1551                         DataGridView grid = new DataGridView();
1552                         Assert.AreEqual (SystemColors.Control, grid.ColumnHeadersDefaultCellStyle.BackColor, "#A1");
1553                         Assert.AreEqual (SystemColors.WindowText,  grid.ColumnHeadersDefaultCellStyle.ForeColor, "#A2");
1554                         Assert.AreEqual (SystemColors.Highlight, grid.ColumnHeadersDefaultCellStyle.SelectionBackColor, "#A3");
1555                         Assert.AreEqual (SystemColors.HighlightText, grid.ColumnHeadersDefaultCellStyle.SelectionForeColor, "#A4");
1556                         Assert.AreSame (grid.Font, grid.ColumnHeadersDefaultCellStyle.Font, "#A5");
1557                         Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.ColumnHeadersDefaultCellStyle.Alignment, "#A6");
1558                         Assert.AreEqual (DataGridViewTriState.True, grid.ColumnHeadersDefaultCellStyle.WrapMode, "#A7");
1559                 }
1560
1561                 [Test]
1562                 public void DefaultCellStyle ()
1563                 {
1564                         DataGridView grid = new DataGridView();
1565                         Assert.AreEqual (SystemColors.Window, grid.DefaultCellStyle.BackColor, "#A1");
1566                         Assert.AreEqual (SystemColors.ControlText,  grid.DefaultCellStyle.ForeColor, "#A2");
1567                         Assert.AreEqual (SystemColors.Highlight, grid.DefaultCellStyle.SelectionBackColor, "#A3");
1568                         Assert.AreEqual (SystemColors.HighlightText, grid.DefaultCellStyle.SelectionForeColor, "#A4");
1569                         Assert.AreSame (grid.Font, grid.DefaultCellStyle.Font, "#A5");
1570                         Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.DefaultCellStyle.Alignment, "#A6");
1571                         Assert.AreEqual (DataGridViewTriState.False, grid.DefaultCellStyle.WrapMode, "#A7");
1572                 }
1573
1574                 [Test]
1575                 public void MethodIsInputKey ()
1576                 {
1577                         string result = string.Empty;
1578                         string expected = "13;13;33;33;34;34;35;36;37;38;39;40;46;48;96;113;";
1579
1580                         ExposeProtectedProperties dgv = new ExposeProtectedProperties ();
1581                 
1582                         foreach (Keys k in Enum.GetValues (typeof (Keys)))
1583                                 if (dgv.PublicIsInputKey (k))
1584                                         result += ((int)k).ToString () + ";";
1585
1586                         Assert.AreEqual (expected, result, "A1");
1587                 }
1588
1589                 [Test]
1590                 public void MethodIsInputChar ()
1591                 {
1592                         bool result = false;
1593
1594                         ExposeProtectedProperties dgv = new ExposeProtectedProperties ();
1595
1596                         for (int i = 0; i < 255; i++)
1597                                 if (!dgv.PublicIsInputChar ((char)i))
1598                                         result = true;
1599
1600                         // Basically, it always returns true
1601                         Assert.AreEqual (false, result, "A1");
1602                 }
1603
1604                 [Test]
1605                 public void RowsDefaultCellStyle ()
1606                 {
1607                         DataGridView grid = new DataGridView();
1608                         Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.BackColor, "#A1");
1609                         Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.ForeColor, "#A2");
1610                         Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.SelectionBackColor, "#A3");
1611                         Assert.AreEqual (Color.Empty, grid.RowsDefaultCellStyle.SelectionForeColor, "#A4");
1612                         Assert.IsNull(grid.RowsDefaultCellStyle.Font, "#A5");
1613                         Assert.AreEqual (DataGridViewContentAlignment.NotSet, grid.RowsDefaultCellStyle.Alignment, "#A6");
1614                         Assert.AreEqual (DataGridViewTriState.NotSet, grid.RowsDefaultCellStyle.WrapMode, "#A7");
1615                 }
1616
1617                 [Test]
1618                 public void RowHeadersDefaultCellStyle ()
1619                 {
1620                         DataGridView grid = new DataGridView();
1621                         Assert.AreEqual (SystemColors.Control, grid.RowHeadersDefaultCellStyle.BackColor, "#A1");
1622                         Assert.AreEqual (SystemColors.WindowText, grid.RowHeadersDefaultCellStyle.ForeColor, "#A2");
1623                         Assert.AreEqual (SystemColors.Highlight, grid.RowHeadersDefaultCellStyle.SelectionBackColor, "#A3");
1624                         Assert.AreEqual (SystemColors.HighlightText, grid.RowHeadersDefaultCellStyle.SelectionForeColor, "#A4");
1625                         Assert.AreSame (grid.Font, grid.RowHeadersDefaultCellStyle.Font, "#A5");
1626                         Assert.AreEqual (DataGridViewContentAlignment.MiddleLeft, grid.RowHeadersDefaultCellStyle.Alignment, "#A6");
1627                         Assert.AreEqual (DataGridViewTriState.True, grid.RowHeadersDefaultCellStyle.WrapMode, "#A7");
1628                 }
1629
1630                 private class MockDataGridView : DataGridView
1631                 {
1632                         public Size default_size {
1633                                 get { return base.DefaultSize; }
1634                         }
1635                 }
1636
1637                 [Test]
1638                 public void bug_82326 ()
1639                 {
1640                         using (Form f = new Form ()) {
1641                                 DataGridView _dataGrid;
1642                                 DataGridViewTextBoxColumn _column;
1643
1644                                 _dataGrid = new DataGridView ();
1645                                 _column = new DataGridViewTextBoxColumn ();
1646                                 f.SuspendLayout ();
1647                                 ((ISupportInitialize)(_dataGrid)).BeginInit ();
1648                                 // 
1649                                 // _dataGrid
1650                                 // 
1651                                 _dataGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
1652                                 _dataGrid.Columns.Add (_column);
1653                                 _dataGrid.RowTemplate.Height = 21;
1654                                 _dataGrid.Location = new Point (12, 115);
1655                                 _dataGrid.Size = new Size (268, 146);
1656                                 _dataGrid.TabIndex = 0;
1657                                 // 
1658                                 // _column
1659                                 // 
1660                                 _column.HeaderText = "Column";
1661                                 // 
1662                                 // MainForm
1663                                 // 
1664                                 f.ClientSize = new Size (292, 273);
1665                                 f.Controls.Add (_dataGrid);
1666                                 ((ISupportInitialize)(_dataGrid)).EndInit ();
1667                                 f.ResumeLayout (false);
1668                                 f.Load += delegate (object sender, EventArgs e) { ((Control)sender).FindForm ().Close (); };
1669
1670                                 Application.Run (f);
1671                         }
1672                 }
1673                 
1674                 [Test]
1675                 public void RowCountIncrease ()
1676                 {
1677                         DataGridView dgv = new DataGridView ();
1678                         dgv.RowCount = 3;
1679                         
1680                         Assert.AreEqual (3, dgv.RowCount, "A1");
1681                         Assert.AreEqual (1, dgv.ColumnCount, "A2");
1682
1683                         Assert.AreEqual (0, dgv.Rows[0].Index, "A3");
1684                         Assert.AreEqual (1, dgv.Rows[1].Index, "A4");
1685                         Assert.AreEqual (2, dgv.Rows[2].Index, "A5");
1686
1687
1688                         dgv.RowCount = 2;
1689                         
1690                         Assert.AreEqual (2, dgv.RowCount, "B1");
1691                         Assert.AreEqual (1, dgv.ColumnCount, "B2");
1692
1693                         Assert.AreEqual (0, dgv.Rows[0].Index, "B3");
1694                         Assert.AreEqual (1, dgv.Rows[1].Index, "B4");
1695
1696                         dgv.RowCount = 6;
1697                         
1698                         Assert.AreEqual (6, dgv.RowCount, "C1");
1699                         Assert.AreEqual (1, dgv.ColumnCount, "C2");
1700
1701                         Assert.AreEqual (0, dgv.Rows[0].Index, "C3");
1702                         Assert.AreEqual (1, dgv.Rows[1].Index, "C4");
1703                         Assert.AreEqual (2, dgv.Rows[2].Index, "C5");
1704
1705                         dgv.AllowUserToAddRows = false;
1706
1707                         Assert.AreEqual (5, dgv.RowCount, "D1");
1708                         Assert.AreEqual (1, dgv.ColumnCount, "D2");
1709
1710                         dgv.RowCount = 1;
1711                         
1712                         Assert.AreEqual (1, dgv.RowCount, "E1");
1713                         Assert.AreEqual (1, dgv.ColumnCount, "E2");
1714
1715                         Assert.AreEqual (0, dgv.Rows[0].Index, "E3");
1716
1717                         dgv.RowCount = 8;
1718                         
1719                         Assert.AreEqual (8, dgv.RowCount, "F1");
1720                         Assert.AreEqual (1, dgv.ColumnCount, "F2");
1721
1722                         Assert.AreEqual (0, dgv.Rows[0].Index, "F3");
1723                         Assert.AreEqual (1, dgv.Rows[1].Index, "F4");
1724                 }
1725
1726                 [Test]
1727                 public void RowCountDecrease ()
1728                 {
1729                         DataGridView dgv = new DataGridView ();
1730                         dgv.RowCount = 6;
1731                         
1732                         Assert.AreEqual (6, dgv.RowCount, "A1");
1733                         Assert.AreEqual (1, dgv.ColumnCount, "A2");
1734
1735                         dgv.RowCount = 3;
1736                         Assert.AreEqual (3, dgv.RowCount, "A3");
1737                         Assert.AreEqual (0, dgv.Rows[0].Index, "A4");
1738                         Assert.AreEqual (1, dgv.Rows[1].Index, "A5");
1739                         Assert.AreEqual (2, dgv.Rows[2].Index, "A6");
1740
1741                         try {
1742                                 dgv.RowCount = 0;
1743                                 Assert.Fail ("C1");
1744                         } catch {}
1745
1746
1747                         dgv.RowCount = 6;
1748                         
1749                         Assert.AreEqual (6, dgv.RowCount, "B1");
1750                         Assert.AreEqual (1, dgv.ColumnCount, "B2");
1751
1752                         Assert.AreEqual (0, dgv.Rows[0].Index, "B3");
1753                         Assert.AreEqual (1, dgv.Rows[1].Index, "B4");
1754                         Assert.AreEqual (2, dgv.Rows[2].Index, "B5");
1755
1756
1757                         dgv.RowCount = 2;
1758                         
1759                         Assert.AreEqual (2, dgv.RowCount, "C1");
1760                         Assert.AreEqual (1, dgv.ColumnCount, "C2");
1761
1762                         Assert.AreEqual (0, dgv.Rows[0].Index, "C3");
1763                         Assert.AreEqual (1, dgv.Rows[1].Index, "C4");
1764
1765                         dgv.AllowUserToAddRows = false;
1766
1767                         Assert.AreEqual (1, dgv.RowCount, "D1");
1768                         Assert.AreEqual (1, dgv.ColumnCount, "D2");
1769
1770                         Assert.AreEqual (0, dgv.Rows[0].Index, "D3");
1771
1772                         dgv.RowCount = 6;
1773                         
1774                         Assert.AreEqual (6, dgv.RowCount, "E1");
1775                         Assert.AreEqual (1, dgv.ColumnCount, "E2");
1776
1777                         Assert.AreEqual (0, dgv.Rows[0].Index, "E3");
1778                         Assert.AreEqual (1, dgv.Rows[1].Index, "E4");
1779                         Assert.AreEqual (2, dgv.Rows[2].Index, "E5");
1780
1781
1782                         dgv.RowCount = 2;
1783                         
1784                         Assert.AreEqual (2, dgv.RowCount, "F1");
1785                         Assert.AreEqual (1, dgv.ColumnCount, "F2");
1786
1787                         Assert.AreEqual (0, dgv.Rows[0].Index, "F3");
1788                         Assert.AreEqual (1, dgv.Rows[1].Index, "F4");
1789
1790                 }
1791
1792                 [Test]
1793                 public void BindToReadonlyProperty ()
1794                 {
1795                         Form f = new Form ();
1796                         f.ShowInTaskbar = false;
1797                         DataGridView dgv = new DataGridView ();
1798                         
1799                         List<cust> l = new List<cust> ();
1800                         l.Add (new cust ());
1801
1802                         dgv.DataSource = l;
1803                         f.Controls.Add (dgv);
1804                         
1805                         f.Show ();
1806                         
1807                         Assert.AreEqual ("Name", dgv.Columns[0].Name, "A1");
1808                         Assert.AreEqual (true, dgv.Columns[0].ReadOnly, "A2");
1809                         
1810                         f.Close ();
1811                         f.Dispose ();
1812                 }
1813
1814                 class cust { public string Name { get { return "test"; } } }
1815         
1816                 [Test]
1817                 public void EnableHeadersVisualStylesDefaultValue ()
1818                 {
1819                         Assert.AreEqual (true, new DataGridView ().EnableHeadersVisualStyles);
1820                 }
1821
1822                 [Test]
1823                 public void RowTemplate ()
1824                 {
1825                         DataGridView dgv = new DataGridView ();
1826
1827                         Form f = new Form ();
1828                         f.Controls.Add (dgv);
1829                         f.Show ();
1830
1831                         dgv.Columns.Add ("A1", "A1");
1832                         Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A1");
1833                         Assert.IsNull (dgv.RowTemplate.DataGridView, "A2");
1834
1835                         dgv.Columns.Add ("A2", "A2");
1836                         Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A3");
1837
1838                         dgv.Rows.Add (3, 6);
1839
1840                         dgv.Columns.Remove ("A1");
1841
1842                         Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A4");
1843                         Assert.AreEqual (1, dgv.Rows[0].Cells.Count, "A5");
1844
1845                         dgv.Columns.Clear ();
1846
1847                         Assert.AreEqual (0, dgv.RowTemplate.Cells.Count, "A6");
1848                         Assert.AreEqual (0, dgv.Rows.Count, "A7");
1849
1850                         f.Close ();
1851                         f.Dispose ();
1852                 }
1853
1854                 [Test]
1855                 public void ScrollToSelectionSynchronous()
1856                 {
1857                         DataGridView dgv = new DataGridView ();
1858                         dgv.RowCount = 1000;
1859                         dgv.CurrentCell = dgv[0, dgv.RowCount -1];              
1860                         Rectangle rowRect = dgv.GetRowDisplayRectangle (dgv.RowCount - 1, false);
1861                         Assert.AreEqual (true, dgv.DisplayRectangle.Contains (rowRect), "#01");
1862                 }
1863
1864                 [Test]
1865                 public void CurrentCell()
1866                 {
1867                         DataGridView dgv = new DataGridView ();
1868                         dgv.AllowUserToAddRows = false;
1869
1870                         Assert.IsNull (dgv.CurrentCell, "A1");
1871
1872                         dgv.RowCount = 10;
1873                         dgv.ColumnCount = 2;
1874                         Assert.AreEqual (10, dgv.RowCount, "B1");
1875                         Assert.AreEqual (2, dgv.ColumnCount, "B2");
1876                         Assert.IsNull (dgv.CurrentCell, "B3");
1877
1878                         dgv.CurrentCell = dgv[1, 9];
1879                         Assert.IsNotNull (dgv.CurrentCell, "H1");
1880                         Assert.AreEqual (9, dgv.CurrentCell.RowIndex, "H2");
1881                         Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "H3");
1882                         
1883                         dgv.CurrentCell = null;
1884                         Assert.IsNull (dgv.CurrentCell, "C1");
1885
1886                         dgv.CurrentCell = dgv[1, 9];
1887                         Assert.IsNotNull (dgv.CurrentCell, "D1");
1888                         Assert.AreEqual (9, dgv.CurrentCell.RowIndex, "D2");
1889                         Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "D3");
1890
1891                         dgv.RowCount = 9;
1892                         Assert.IsNotNull (dgv.CurrentCell, "E1");
1893                         Assert.AreEqual (8, dgv.CurrentCell.RowIndex, "E2");
1894                         Assert.AreEqual (1, dgv.CurrentCell.ColumnIndex, "E3");
1895
1896                         dgv.CurrentCell = dgv[0, 4];
1897                         dgv.RowCount = 2;
1898                         Assert.IsNotNull (dgv.CurrentCell, "F1");
1899                         Assert.AreEqual (1, dgv.CurrentCell.RowIndex, "F2");
1900                         Assert.AreEqual (0, dgv.CurrentCell.ColumnIndex, "F3");
1901
1902                         dgv.RowCount = 0;
1903                         Assert.IsNull (dgv.CurrentCell, "P1");
1904
1905                         dgv.RowCount = 10;
1906                         Assert.AreEqual (10, dgv.RowCount, "I1");
1907                         dgv.CurrentCell = dgv[0, 4];
1908                         dgv.ColumnCount = 0;
1909                         Assert.AreEqual (0, dgv.RowCount, "I2");
1910                         Assert.IsNull (dgv.CurrentCell, "I3");
1911
1912                         dgv.RowCount = 0;
1913                         dgv.ColumnCount = 0;
1914                         dgv.CreateControl ();
1915                         dgv.ColumnCount = 2;
1916                         dgv.RowCount = 3;
1917
1918                         Assert.IsNotNull (dgv.CurrentCell, "G1");
1919                         Assert.AreEqual (0, dgv.CurrentCell.RowIndex, "G1");
1920                         Assert.AreEqual (0, dgv.CurrentCell.ColumnIndex, "G1");
1921                 }
1922
1923                 [Test]
1924                 public void DataSourceBindingContextDependency ()
1925                 {
1926                         List<DataItem> dataList = new List<DataItem> ();
1927                         dataList.Add (new DataItem ());
1928                         dataList.Add (new DataItem ());
1929
1930                         DataGridView dgv = new DataGridView ();
1931                         dgv.DataSource = dataList;
1932                         Assert.IsNull (dgv.BindingContext, "#1");
1933                         Assert.IsFalse (dgv.IsHandleCreated, "#2");
1934                         Assert.AreEqual (0, dgv.RowCount, "#3");
1935
1936                         dgv.DataSource = null;
1937
1938                         Form form = new Form ();
1939                         form.Controls.Add (dgv);
1940                         dgv.DataSource = dataList;
1941
1942                         Assert.IsNotNull (dgv.BindingContext, "#4");
1943                         Assert.IsFalse (dgv.IsHandleCreated, "#5");
1944                         Assert.AreEqual (2, dgv.RowCount, "#6");
1945
1946                         dgv.Dispose ();
1947                         dgv = new DataGridView ();
1948                         dgv.DataSource = dataList;
1949
1950                         Assert.IsNull (dgv.BindingContext, "#7");
1951                         Assert.IsFalse (dgv.IsHandleCreated, "#8");
1952                         Assert.AreEqual (0, dgv.RowCount, "#9");
1953
1954                         dgv.CreateControl ();
1955
1956                         Assert.IsNull (dgv.BindingContext, "#10");
1957                         Assert.IsTrue (dgv.IsHandleCreated, "#11");
1958                         Assert.AreEqual (0, dgv.RowCount, "#12");
1959                 }
1960
1961                 [Test]
1962                 public void RowTemplateDataGridView ()
1963                 {
1964                         DataGridView gdv = new DataGridView ();
1965                         Assert.IsNull (gdv.RowTemplate.DataGridView, "#1");
1966                 }
1967         }
1968         
1969         [TestFixture]
1970         public class DataGridViewControlCollectionTest
1971         {
1972                 [Test]
1973                 public void TestClear ()
1974                 {
1975                         using (DataGridView dgv = new DataGridView ()) {
1976                                 DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection) dgv.Controls;
1977                                 Control c1 = new Control ();
1978                                 Control c2 = new Control ();
1979                                 Control c3 = new Control ();
1980                                 Assert.AreEqual (2, controls.Count, "#02");
1981                                 controls.Add (c1);
1982                                 controls.Add (c2);
1983                                 controls.Add (c3);
1984                                 Assert.AreEqual (5, controls.Count, "#02");
1985                                 controls.Clear ();
1986                                 Assert.AreEqual (3, controls.Count, "#03");
1987                                 Assert.AreSame (c2, controls [2], "#04");
1988                         }
1989
1990                         // Maybe MS should start writing unit-tests?
1991
1992                         using (DataGridView dgv = new DataGridView ()) {
1993                                 DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
1994                                 Control [] c = new Control [20];
1995                                 for (int i = 0; i < c.Length; i++) {
1996                                         c [i] = new Control ();
1997                                         c [i].Text = "#" + i.ToString ();
1998                                 }
1999                                 
2000                                 Assert.AreEqual (2, controls.Count, "#02");
2001                                 controls.AddRange (c);
2002                                 Assert.AreEqual (22, controls.Count, "#02");
2003                                 controls.Clear ();
2004                                 Assert.AreEqual (12, controls.Count, "#03");
2005                                 
2006                                 for (int i = 0; i < c.Length; i += 2) {
2007                                         Assert.AreSame (c [i+1], controls [ (i / 2) + 2], "#A" + i.ToString ());
2008                                 }
2009                         }
2010                 }
2011
2012                 [Test]
2013                 public void TestCopyTo ()
2014                 {
2015                         using (DataGridView dgv = new DataGridView ()) {
2016                                 DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
2017                                 Control c1 = new Control ();
2018                                 Control c2 = new Control ();
2019                                 Control c3 = new Control ();
2020                                 Control [] copy = new Control [10];
2021                                 Assert.AreEqual (2, controls.Count, "#01");
2022                                 controls.AddRange (new Control [] { c1, c2, c3 });
2023                                 Assert.AreEqual (5, controls.Count, "#01-b");
2024                                 controls.CopyTo (copy, 0);
2025                                 Assert.AreEqual (5, controls.Count, "#02");
2026                                 Assert.AreEqual (10, copy.Length, "#03");
2027                                 for (int i = 0; i < copy.Length; i++) {
2028                                         if (i >= 5)
2029                                                 Assert.IsNull (copy [i], "#A" + i.ToString ());
2030                                         else
2031                                                 Assert.IsNotNull (copy [i], "#B" + i.ToString ());
2032                                 }
2033                         }
2034                 }
2035
2036                 [Test]
2037                 [ExpectedException (typeof (NotSupportedException))]
2038                 public void TestInsert ()
2039                 {
2040                         using (DataGridView dgv = new DataGridView ()) {
2041                                 DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
2042                                 controls.Insert (1, new Control ());
2043                         }
2044                 }
2045
2046                 [Test]
2047                 public void TestRemove ()
2048                 {
2049                         using (DataGridView dgv = new DataGridView ()) {
2050                                 DataGridView.DataGridViewControlCollection controls = (DataGridView.DataGridViewControlCollection)dgv.Controls;
2051                                 Control c1 = new Control ();
2052                                 Control c2 = new Control ();
2053                                 Control c3 = new Control ();
2054                                 Control [] copy = new Control [10];
2055                                 
2056                                 controls.AddRange (new Control [] {c1, c2, c3});
2057                                 
2058                                 controls.Remove (c2);
2059                                 Assert.AreEqual (4, controls.Count, "#01");
2060                                 controls.Remove (c2);
2061                                 Assert.AreEqual (4, controls.Count, "#02");
2062                                 controls.Remove (c1);
2063                                 Assert.AreEqual (3, controls.Count, "#03");
2064                                 controls.Remove (c3);
2065                                 Assert.AreEqual (2, controls.Count, "#04");
2066                                 
2067                                 controls.Remove (controls [0]);
2068                                 controls.Remove (controls [1]);
2069                                 Assert.AreEqual (2, controls.Count, "#05");
2070                         }
2071                 }
2072         }
2073                 
2074 }
2075
2076 #endif