2008-05-20 Jonathan Pobst <monkey@jpobst.com>
authorJonathan Pobst <monkey@jpobst.com>
Tue, 20 May 2008 20:32:03 +0000 (20:32 -0000)
committerJonathan Pobst <monkey@jpobst.com>
Tue, 20 May 2008 20:32:03 +0000 (20:32 -0000)
* DataGridViewCellCollection.cs: Add a method to find the cell
with the given DataPropertyName.
* DataGridViewColumn.cs: Track if the column was autogenerated or not.
* DataGridViewColumnCollection.cs: Add a method to clear all
autogenerated columns.
* DataGridView.cs: If AutoGenerateColumns is false, don't autogenerate
columns.
[Fixes bug #348082]

2008-05-20  Jonathan Pobst  <monkey@jpobst.com>

* DataGridViewDataBindingTest.cs: Add test for AutoGenerateColumns.

svn path=/trunk/mcs/; revision=103618

mcs/class/Managed.Windows.Forms/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridView.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewCellCollection.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumn.cs
mcs/class/Managed.Windows.Forms/System.Windows.Forms/DataGridViewColumnCollection.cs
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/ChangeLog
mcs/class/Managed.Windows.Forms/Test/System.Windows.Forms/DataGridViewDataBindingTest.cs

index e9d346b34f8660e3bcc97fcaf2d6383e8b8039d6..ca6ef638b316c4c200dda9d60ada61d18a060a4d 100644 (file)
@@ -1,3 +1,14 @@
+2008-05-20  Jonathan Pobst  <monkey@jpobst.com>
+
+       * DataGridViewCellCollection.cs: Add a method to find the cell
+       with the given DataPropertyName.
+       * DataGridViewColumn.cs: Track if the column was autogenerated or not.
+       * DataGridViewColumnCollection.cs: Add a method to clear all
+       autogenerated columns.
+       * DataGridView.cs: If AutoGenerateColumns is false, don't autogenerate
+       columns.
+       [Fixes bug #348082]
+
 2008-05-20  Jonathan Pobst  <monkey@jpobst.com>
 
        * DataGridView.cs: Don't try to update the RowTemplate with
index 80008d4db57bcaf9ccb6ee2d08bdd8cb71f62138..e44dcce47e0a25b141134720e910eb555331b410 100644 (file)
@@ -5393,34 +5393,44 @@ namespace System.Windows.Forms {
                }
 
                private void BindIList (IList list) {
-                       // Stuff from a DataSet
-                       if (list is DataView) {
-                               DataView dataView = (DataView) list;
-                               DataTable table = dataView.Table;
-
-                               foreach (DataColumn dataColumn in table.Columns) {
-                                       DataGridViewColumn col = CreateColumnByType (dataColumn.DataType);
-                                       
-                                       col.Name = dataColumn.ColumnName;
-                                       col.DataPropertyName = dataColumn.ColumnName;
-                                       col.SetIsDataBound (true);
-                                       col.ValueType = dataColumn.DataType;
+                       if (autoGenerateColumns) {
+                               // Stuff from a DataSet
+                               if (list is DataView) {
+                                       DataView dataView = (DataView) list;
+                                       DataTable table = dataView.Table;
+
+                                       foreach (DataColumn dataColumn in table.Columns) {
+                                               DataGridViewColumn col = CreateColumnByType (dataColumn.DataType);
+                                               
+                                               col.Name = dataColumn.ColumnName;
+                                               col.DataPropertyName = dataColumn.ColumnName;
+                                               col.SetIsDataBound (true);
+                                               col.ValueType = dataColumn.DataType;
+                                               col.AutoGenerated = true;
+                                               
+                                               columns.Add (col);
+                                       }
                                        
-                                       columns.Add (col);
+                                       dataView.ListChanged += OnListChanged;
                                }
                                
-                               dataView.ListChanged += OnListChanged;
-                       }
-                       else if (list.Count > 0) {
-                               DataGridViewCell template = new DataGridViewTextBoxCell();
-                               foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(list[0])) {
-                                       DataGridViewColumn col = new DataGridViewColumn(template);
-                                       col.Name = property.DisplayName;
-                                       col.ReadOnly = property.IsReadOnly;
-                                       columns.Add(col);
+                               else if (list.Count > 0) {
+                                       DataGridViewCell template = new DataGridViewTextBoxCell();
+                                       foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(list[0])) {
+                                               DataGridViewColumn col = new DataGridViewColumn(template);
+                                               col.Name = property.DisplayName;
+                                               col.ReadOnly = property.IsReadOnly;
+                                               col.AutoGenerated = true;
+                                               columns.Add (col);
+                                       }
                                }
                        }
+                       
                        foreach (object element in list) {
+                               // Don't add rows if there are no columns
+                               if (ColumnCount == 0)
+                                       break;
+                                       
                                DataGridViewRow row = (DataGridViewRow)RowTemplate.Clone ();
                                rows.InternalAdd (row);
 
@@ -5432,7 +5442,7 @@ namespace System.Windows.Forms {
                                        
                                        // We do it this way because there may not be a column
                                        // for every cell, ignore cells with no column  
-                                       DataGridViewCell cell = row.Cells[property.Name];
+                                       DataGridViewCell cell = row.Cells.GetBoundCell (property.Name);
                                        
                                        if (cell == null)
                                                continue;
@@ -5454,7 +5464,7 @@ namespace System.Windows.Forms {
                private void ClearBinding ()
                {
                        if (dataSource != null) {
-                               columns.Clear ();
+                               columns.ClearAutoGeneratedColumns ();
                                rows.Clear ();
                                
                                if (dataSource is DataView)
index 6c72cdd288cbfa67ce8093c5525681cef2a7dfc3..6192d93d817978857eafd196fdb376a9ff1d6092 100644 (file)
@@ -95,6 +95,16 @@ namespace System.Windows.Forms
                        }
                }
 
+               internal DataGridViewCell GetBoundCell (string dataPropertyName)
+               {
+                       foreach (DataGridViewCell cell in base.List) {
+                               if (string.Compare (cell.OwningColumn.DataPropertyName, dataPropertyName, true) == 0)
+                                       return cell;
+                       }
+                       
+                       return null;
+               }
+               
                public event CollectionChangeEventHandler CollectionChanged;
 
                int IList.Add (object value)
index 8348691d10b6cd6183e482147360522ffe75d89b..f425ff4649751e694fa6e0e6fca0f4cba9c8d559 100644 (file)
@@ -37,7 +37,7 @@ namespace System.Windows.Forms {
        [ToolboxItem ("")]
        [DesignTimeVisible (false)]
        public class DataGridViewColumn : DataGridViewBand, IComponent, IDisposable {
-
+               private bool auto_generated;
                private DataGridViewAutoSizeColumnMode autoSizeMode;
                private DataGridViewCell cellTemplate;
                private ContextMenuStrip contextMenuStrip;
@@ -241,6 +241,7 @@ Example */
                        }
                }
 
+               internal bool AutoGenerated { get { return auto_generated; } set { auto_generated = value; } }
                internal bool HeaderTextSet { get { return headerTextSet; } }
                
                [Browsable (false)]
index 5811808ca0f174cfe3b4e58544a9f0eb091e89e2..eec4361657381e7999b347aee78a623539438415 100644 (file)
@@ -241,9 +241,20 @@ namespace System.Windows.Forms
                        List<DataGridViewColumn> result = new List<DataGridViewColumn> (array);
 
                        result.Sort (new ColumnDisplayIndexComparator ());
+                       
+                       for (int i = 0; i < result.Count; i++)
+                               result[i].DisplayIndex = i;
+                       
                        display_index_sorted = result;
                }
                
+               internal void ClearAutoGeneratedColumns ()
+               {
+                       for (int i = list.Count - 1; i >= 0; i--)
+                               if ((list[i] as DataGridViewColumn).AutoGenerated)
+                                       list.RemoveAt (i);
+               }
+               
                private class ColumnDisplayIndexComparator : IComparer<DataGridViewColumn>
                {
                        public int Compare (DataGridViewColumn o1, DataGridViewColumn o2)
index 724ffaf38e82f8f944090847954955857b69a3bc..dc1d52ecd67e5f85dba27447c5e4d7c3c9238dd0 100644 (file)
@@ -1,3 +1,7 @@
+2008-05-20  Jonathan Pobst  <monkey@jpobst.com>
+
+       * DataGridViewDataBindingTest.cs: Add test for AutoGenerateColumns.
+
 2008-05-20  Jonathan Pobst  <monkey@jpobst.com>
 
        * DataGridViewColumnCollectionTest.cs: Change Add() test to not
index d9ecdc58d433ff74c94b31585f1556a598de9d1e..a92a15068c404efeadeaabb9411dcd3699096e82 100644 (file)
@@ -211,6 +211,63 @@ namespace MonoTests.System.Windows.Forms
                        f.Dispose ();
                }
 
+               [Test]
+               public void TestAutoGenerateColumns ()
+               {
+                       // Binding when AutoGenerateColumns is false
+                       Form f = new Form ();
+                       f.ShowInTaskbar = false;
+
+                       DataSet ds = new DataSet ();
+
+                       DataTable dt = ds.Tables.Add ("Muppets");
+
+                       dt.Columns.Add ("ID");
+                       dt.Columns.Add ("Name");
+
+                       dt.Rows.Add (1, "Kermit");
+                       dt.Rows.Add (2, "Miss Piggy");
+                       dt.Rows.Add (3, "Gonzo");
+
+                       DataGridView dgv = new DataGridView ();
+                       dgv.AutoGenerateColumns = false;
+                       dgv.DataSource = dt;
+
+                       f.Controls.Add (dgv);
+                       f.Show ();
+
+                       Assert.AreEqual (0, dgv.ColumnCount, "A1");
+                       Assert.AreEqual (0, dgv.RowCount, "A2");
+
+                       dgv.DataSource = null;
+                       
+                       DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn ();
+                       col1.DataPropertyName = "Name";
+                       dgv.Columns.Add (col1);
+
+                       dgv.DataSource = dt;
+
+                       Assert.AreEqual (1, dgv.ColumnCount, "A3");
+                       Assert.AreEqual (4, dgv.RowCount, "A4");
+
+                       Assert.AreEqual ("Kermit", dgv.Rows[0].Cells[0].Value, "A5");
+
+                       dgv.DataSource = null;
+
+                       DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn ();
+                       col2.DataPropertyName = "id";
+                       dgv.Columns.Add (col2);
+
+                       dgv.DataSource = dt;
+
+                       Assert.AreEqual (2, dgv.ColumnCount, "A6");
+                       Assert.AreEqual (4, dgv.RowCount, "A7");
+
+                       Assert.AreEqual ("Kermit", dgv.Rows[0].Cells[0].Value, "A8");
+                       Assert.AreEqual ("1", dgv.Rows[0].Cells[1].Value, "A9");
+
+                       f.Dispose ();
+               }
        }
 }
 #endif
\ No newline at end of file