New tests.
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / DataGridView.cs
index 853ce852b647a1adf72ba86e47b22518497ec5d7..6e811bfe84bbc930753ad9b6a8f889cfbb3f4a01 100644 (file)
@@ -736,11 +736,23 @@ namespace System.Windows.Forms {
                                if (!(value == null || value is IList || value is IListSource || value is IBindingList || value is IBindingListView))
                                        throw new NotSupportedException ("Type cannot be bound.");
                                        
-                               ClearBinding ();
-                               dataSource = value;
-                               if (BindingContext != null)
-                                       ReBind ();
-                               OnDataSourceChanged (EventArgs.Empty);
+                               if (value != DataSource) {
+                                       if (IsHandleCreated && value != null && BindingContext != null && BindingContext[value] != null)
+                                               DataMember = String.Empty;
+                                       ClearBinding ();
+       
+       
+                                       // Do not set dataSource prior to te BindingContext check because there is some lazy initialization 
+                                       // code which might result in double call to ReBind here and in OnBindingContextChanged
+                                       if (BindingContext != null) {
+                                               dataSource = value;
+                                               ReBind ();
+                                       } else {
+                                               dataSource = value;
+                                       }
+
+                                       OnDataSourceChanged (EventArgs.Empty);
+                               }
                        }
                }
 
@@ -908,11 +920,12 @@ namespace System.Windows.Forms {
                [Browsable (false)]
                public bool IsCurrentRowDirty {
                        get {
-                               if (!virtualMode) {
+                               if (!virtualMode)
                                        return IsCurrentCellDirty;
-                               }
-                               // Calcular
-                               throw new NotImplementedException();
+
+                               QuestionEventArgs args = new QuestionEventArgs ();
+                               OnRowDirtyStateNeeded (args);
+                               return args.Response;
                        }
                }
 
@@ -1028,10 +1041,10 @@ namespace System.Windows.Forms {
                                        if (ColumnCount == 0)
                                                ColumnCount = 1;
 
-                                       for (int i = rows.Count; i < value; i++) {
-                                               DataGridViewRow row = (DataGridViewRow) RowTemplateFull;
-                                               rows.AddInternal (row, false);
-                                       }
+                                       List<DataGridViewRow> newRows = new List<DataGridViewRow> (value - rows.Count);
+                                       for (int i = rows.Count; i < value; i++)
+                                               newRows.Add ((DataGridViewRow) RowTemplateFull);
+                                       rows.AddRange (newRows.ToArray());
                                }
                        }
                }
@@ -1134,7 +1147,6 @@ namespace System.Windows.Forms {
                        }
                        set {
                                rowTemplate = value;
-                               rowTemplate.SetDataGridView(this);
                        }
                }
 
@@ -2374,8 +2386,22 @@ namespace System.Windows.Forms {
                                return true;
 
                        try {
-                               currentCell.Value = currentCell.ParseFormattedValue (currentCell.EditedFormattedValue, 
-                                                                                    currentCell.InheritedStyle, null, null);
+                               // convert
+                               object newValue = currentCell.ParseFormattedValue (currentCell.EditedFormattedValue, 
+                                                                                  currentCell.InheritedStyle, null, null);
+
+                               DataGridViewCellValidatingEventArgs validateArgs = new DataGridViewCellValidatingEventArgs (currentCell.ColumnIndex, 
+                                                                                                                           currentCell.RowIndex, 
+                                                                                                                           newValue);
+                               // validate
+                               OnCellValidating (validateArgs);
+                               if (validateArgs.Cancel)
+                                       return false;
+                               OnCellValidated (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
+
+                               // commit
+                               currentCell.Value = newValue;
+
                        } catch (Exception e) {
                                DataGridViewDataErrorEventArgs args = new DataGridViewDataErrorEventArgs (e, currentCell.ColumnIndex, currentCell.RowIndex, 
                                                                                                          DataGridViewDataErrorContexts.Commit);
@@ -2387,17 +2413,32 @@ namespace System.Windows.Forms {
                        return true;
                }
 
-               [MonoTODO ("Always includes partial columns")]
                public int DisplayedColumnCount (bool includePartialColumns)
                {
                        int result = 0;
-                       
-                       for (int i = first_col_index; i < Columns.Count; i++)
-                               if (Columns.ColumnDisplayIndexSortedArrayList[i].Displayed)
+                       int columnLeft = 0;
+
+                       if (RowHeadersVisible)
+                               columnLeft += RowHeadersWidth;
+
+                       Size visibleClientArea = ClientSize;
+                       if (verticalScrollBar.Visible)
+                               visibleClientArea.Width -= verticalScrollBar.Width;
+                       if (horizontalScrollBar.Visible)
+                               visibleClientArea.Height -= horizontalScrollBar.Height;
+
+                       for (int index = first_col_index; index < Columns.Count; index++) {
+                               DataGridViewColumn column = Columns[ColumnDisplayIndexToIndex (index)];
+                               if (columnLeft + column.Width <= visibleClientArea.Width) {
                                        result++;
-                               else
+                                       columnLeft += column.Width;
+                               } else {
+                                       if (includePartialColumns)
+                                               result++;
                                        break;
-
+                               }
+                       }
+                                       
                        return result;
                }
 
@@ -2863,7 +2904,6 @@ namespace System.Windows.Forms {
                        return new HitTestInfo (-1, x, -1, y, DataGridViewHitTestType.None);
                }
 
-               [MonoTODO ("Invalidates whole grid")]
                public void InvalidateCell (DataGridViewCell dataGridViewCell)
                {
                        if (dataGridViewCell == null)
@@ -2875,7 +2915,6 @@ namespace System.Windows.Forms {
                        InvalidateCell (dataGridViewCell.ColumnIndex, dataGridViewCell.RowIndex);
                }
 
-               [MonoTODO ("Invalidates whole grid")]
                public void InvalidateCell (int columnIndex, int rowIndex)
                {
                        if (columnIndex < 0 || columnIndex >= columns.Count)
@@ -2884,25 +2923,26 @@ namespace System.Windows.Forms {
                        if (rowIndex < 0 || rowIndex >= rows.Count)
                                throw new ArgumentOutOfRangeException ("Row index is out of range.");
 
-                       Invalidate (GetCellDisplayRectangle (columnIndex, rowIndex, true));
+                       if (!is_binding)
+                               Invalidate (GetCellDisplayRectangle (columnIndex, rowIndex, true));
                }
 
-               [MonoTODO ("Invalidates whole grid")]
                public void InvalidateColumn (int columnIndex)
                {
                        if (columnIndex < 0 || columnIndex >= columns.Count)
                                throw new ArgumentOutOfRangeException ("Column index is out of range.");
 
-                       Invalidate (GetColumnDisplayRectangle (columnIndex, true));
+                       if (!is_binding)
+                               Invalidate (GetColumnDisplayRectangle (columnIndex, true));
                }
 
-               [MonoTODO ("Invalidates whole grid")]
                public void InvalidateRow (int rowIndex)
                {
                        if (rowIndex < 0 || rowIndex >= rows.Count)
                                throw new ArgumentOutOfRangeException ("Row index is out of range.");
 
-                       Invalidate (GetRowDisplayRectangle (rowIndex, true));
+                       if (!is_binding)
+                               Invalidate (GetRowDisplayRectangle (rowIndex, true));
                }
 
                public virtual void NotifyCurrentCellDirty (bool dirty) {
@@ -3063,8 +3103,53 @@ namespace System.Windows.Forms {
                                InvalidateRow (i);
                }
 
-               public void UpdateRowHeightInfo (int rowIndex, bool updateToEnd) {
-                       throw new NotImplementedException();
+               public void UpdateRowHeightInfo (int rowIndex, bool updateToEnd)
+               {
+                       if (rowIndex < 0 && updateToEnd)
+                               throw new ArgumentOutOfRangeException ("rowIndex");
+                       if (rowIndex < -1 && !updateToEnd)
+                               throw new ArgumentOutOfRangeException ("rowIndex");
+                       if (rowIndex >= Rows.Count)
+                               throw new ArgumentOutOfRangeException ("rowIndex");
+                       
+                       if (!VirtualMode && DataManager == null)
+                               return;
+
+                       if (rowIndex == -1) {
+                               updateToEnd = true;
+                               rowIndex = 0;
+                       }
+
+                       if (updateToEnd) {
+                               for (int i = rowIndex; i < Rows.Count; i++) {
+                                       DataGridViewRow row = Rows[i];
+                                       if (!row.Visible)
+                                               continue;
+
+                                       DataGridViewRowHeightInfoNeededEventArgs rowInfo = 
+                                               new DataGridViewRowHeightInfoNeededEventArgs (row.Index, row.Height, row.MinimumHeight);
+                                       OnRowHeightInfoNeeded (rowInfo);
+
+                                       if (row.Height != rowInfo.Height || row.MinimumHeight != rowInfo.MinimumHeight) {
+                                               row.Height = rowInfo.Height;
+                                               row.MinimumHeight = rowInfo.MinimumHeight;
+                                               OnRowHeightInfoPushed (new DataGridViewRowHeightInfoPushedEventArgs (row.Index, rowInfo.Height, 
+                                                                                                                    rowInfo.MinimumHeight));
+                                       }
+                               }
+                       } else {
+                               DataGridViewRow row = Rows[rowIndex];
+                               DataGridViewRowHeightInfoNeededEventArgs rowInfo = 
+                                       new DataGridViewRowHeightInfoNeededEventArgs (row.Index, row.Height, row.MinimumHeight);
+                               OnRowHeightInfoNeeded (rowInfo);
+
+                               if (row.Height != rowInfo.Height || row.MinimumHeight != rowInfo.MinimumHeight) {
+                                       row.Height = rowInfo.Height;
+                                       row.MinimumHeight = rowInfo.MinimumHeight;
+                                       OnRowHeightInfoPushed (new DataGridViewRowHeightInfoPushedEventArgs (row.Index, rowInfo.Height, 
+                                                                                                            rowInfo.MinimumHeight));
+                               }
+                       }
                }
 
                protected override bool CanEnableIme {
@@ -3709,15 +3794,37 @@ namespace System.Windows.Forms {
                                //
                                if (!is_autogenerating_columns && columns.Count == 1)
                                        ReBind ();
-                               foreach (DataGridViewRow row in Rows)
-                                       row.Cells.Add ((DataGridViewCell)e.Column.CellTemplate.Clone ());
+
+                               foreach (DataGridViewRow row in Rows) {
+                                       DataGridViewCell newCell = (DataGridViewCell)e.Column.CellTemplate.Clone ();
+                                       if (row.Cells.Count == columns.Count)
+                                               row.Cells.Replace (e.Column.Index, newCell);
+                                       else if (e.Column.Index >= row.Cells.Count)
+                                               row.Cells.Add (newCell);
+                                       else
+                                               row.Cells.Insert (e.Column.Index, newCell);
+                               }
                        }
                        
+                       e.Column.DataColumnIndex = FindDataColumnIndex (e.Column);
                        AutoResizeColumnsInternal ();
                        OnColumnAdded (e);
                        PrepareEditingRow (false, true);
                }
 
+               private int FindDataColumnIndex (DataGridViewColumn column)
+               {
+                       if (column != null && DataManager != null) {
+                               PropertyDescriptorCollection properties = DataManager.GetItemProperties();
+                               for (int i = 0; i < properties.Count; i++) {
+                                       if (String.Compare (column.DataPropertyName, properties[i].Name, true) == 0)
+                                               return i;
+                               }
+                       }
+
+                       return -1;
+               }
+
                protected virtual void OnColumnAdded (DataGridViewColumnEventArgs e)
                {
                        DataGridViewColumnEventHandler eh = (DataGridViewColumnEventHandler)(Events [ColumnAddedEvent]);
@@ -3971,6 +4078,11 @@ namespace System.Windows.Forms {
 
                protected override void OnDoubleClick (EventArgs e) {
                        base.OnDoubleClick(e);
+
+                       Point mouseLocation = this.PointToClient (Control.MousePosition);
+                       HitTestInfo hitInfo = HitTest (mouseLocation.X, mouseLocation.Y);
+                       if (hitInfo.Type == DataGridViewHitTestType.Cell)
+                               OnCellDoubleClick (new DataGridViewCellEventArgs (hitInfo.ColumnIndex, hitInfo.RowIndex));
                }
 
                protected virtual void OnEditingControlShowing (DataGridViewEditingControlShowingEventArgs e) {
@@ -4027,7 +4139,7 @@ namespace System.Windows.Forms {
                        base.OnHandleCreated(e);
                        ReBind ();
                        
-                       if (CurrentCell == null && Rows.Count > 0 && Columns.Count > 0)
+                       if (DataManager == null && CurrentCell == null && Rows.Count > 0 && Columns.Count > 0)
                                MoveCurrentCell (ColumnDisplayIndexToIndex (0), 0, true, false, false, false);
                }
 
@@ -4280,7 +4392,9 @@ namespace System.Windows.Forms {
                        DataGridViewRow row = null;
                        Rectangle cellBounds;
 
-                       if (hitTest.Type == DataGridViewHitTestType.ColumnHeader && MouseOverColumnResize (hitTest.ColumnIndex, e.X)) {
+                       if ((hitTest.Type == DataGridViewHitTestType.ColumnHeader ||
+                            (hitTest.Type == DataGridViewHitTestType.Cell && !ColumnHeadersVisible)) 
+                           && MouseOverColumnResize (hitTest.ColumnIndex, e.X)) {
                                if (e.Clicks == 2) {
                                        AutoResizeColumn (hitTest.ColumnIndex);
                                        return;
@@ -4384,7 +4498,12 @@ namespace System.Windows.Forms {
                        Cursor new_cursor = Cursors.Default;
                        HitTestInfo hit = this.HitTest (e.X, e.Y);
                        
-                       if (hit.Type == DataGridViewHitTestType.Cell) {
+                       if (hit.Type == DataGridViewHitTestType.ColumnHeader || 
+                           (!ColumnHeadersVisible && hit.Type == DataGridViewHitTestType.Cell && MouseOverColumnResize (hit.ColumnIndex, e.X))) {
+                               EnteredHeaderCell = Columns [hit.ColumnIndex].HeaderCell;
+                               if (MouseOverColumnResize (hit.ColumnIndex, e.X))
+                                       new_cursor = Cursors.VSplit;
+                       } else if (hit.Type == DataGridViewHitTestType.Cell) {
                                EnteredHeaderCell = null;
 
                                DataGridViewCell new_cell = GetCellInternal (hit.ColumnIndex, hit.RowIndex);
@@ -4405,6 +4524,8 @@ namespace System.Windows.Forms {
                                                MouseLeftErrorIcon (new_cell);
                                }
                                
+                               Cursor = new_cursor;
+
                                // We have never been in a cell before
                                if (hover_cell == null) {
                                        hover_cell = new_cell;
@@ -4480,13 +4601,7 @@ namespace System.Windows.Forms {
                                }
                        
                        } else {
-                               if (hit.Type == DataGridViewHitTestType.ColumnHeader) {
-                                       EnteredHeaderCell = Columns [hit.ColumnIndex].HeaderCell;
-                                       
-                                       if (MouseOverColumnResize (hit.ColumnIndex, e.X))
-                                               new_cursor = Cursors.VSplit;
-                               } else
-                                       EnteredHeaderCell = null;
+                               EnteredHeaderCell = null;
 
                                // We have left the cell area
                                if (hover_cell != null) {
@@ -4568,8 +4683,6 @@ namespace System.Windows.Forms {
 
                protected override void OnPaint (PaintEventArgs e)
                {
-                       base.OnPaint(e);
-
                        Graphics g = e.Graphics;
                        Rectangle bounds = ClientRectangle;
                        
@@ -4676,6 +4789,9 @@ namespace System.Windows.Forms {
                                        ControlPaint.DrawBorder3D (g, bounds, Border3DStyle.Sunken);
                                        break;
                        }
+
+                       // Call the base impl at the end.
+                       base.OnPaint(e);
                }
 
                private void RefreshScrollBars ()
@@ -4719,6 +4835,10 @@ namespace System.Windows.Forms {
                                if (scrollBars != ScrollBars.Horizontal && scrollBars != ScrollBars.Both)
                                        horizontalVisible = false;
 
+                               // MSNET compatibility here
+                               if (RowCount <= 1)
+                                       verticalVisible = false;
+
                                if (horizontalVisible) {
                                        horizontalScrollBar.Minimum = 0;
                                        horizontalScrollBar.Maximum = gridWidth;
@@ -4862,6 +4982,7 @@ namespace System.Windows.Forms {
 
                protected internal virtual void OnRowHeightChanged (DataGridViewRowEventArgs e)
                {
+                       UpdateRowHeightInfo (e.Row.Index, false);
                        DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [RowHeightChangedEvent]);
                        if (eh != null) eh (this, e);
                }
@@ -4907,6 +5028,11 @@ namespace System.Windows.Forms {
                        if (hover_cell != null && hover_cell.RowIndex >= e.RowIndex)
                                hover_cell = null;
 
+                       // Select the first row if we are not databound. 
+                       // If we are databound selection is managed by the data manager.
+                       if (IsHandleCreated && DataManager == null && CurrentCell == null && Rows.Count > 0 && Columns.Count > 0)
+                               MoveCurrentCell (ColumnDisplayIndexToIndex (0), 0, true, false, false, true);
+
                        AutoResizeColumnsInternal ();
                        Invalidate ();
                        OnRowsAdded (e);
@@ -5250,7 +5376,7 @@ namespace System.Windows.Forms {
                        return false;
                }
 
-               [MonoTODO ("What does insert do?")]
+               [MonoInternalNote ("What does insert do?")]
                protected bool ProcessInsertKey (Keys keyData)
                {
                        return false;
@@ -5508,6 +5634,10 @@ namespace System.Windows.Forms {
                                                        return false;
                                                else if (currentCell.RowIndex == NewRowIndex && new_row_editing)
                                                        CancelEdit ();
+                                       } else {
+                                               // CancelEdit will replace the uncommited real editing row with a place holder row
+                                               if (new_row_editing && currentCell.RowIndex == NewRowIndex)
+                                                       CancelEdit ();
                                        }
                                        OnCellLeave (new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
                                        OnRowLeave (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
@@ -5526,7 +5656,7 @@ namespace System.Windows.Forms {
                                OnCurrentCellChanged (EventArgs.Empty);
 
                                if (cell != null) {
-                                       // If the user begins an edit in the NewRow, add a new row
+                                       // If the user begins an edit in the NewRow, add a new real row
                                        if (AllowUserToAddRows && cell.RowIndex == NewRowIndex && !is_binding && !new_row_editing) {
                                                // OnUserAddedRow will add a real row and reset the current cell
                                                OnUserAddedRow (new DataGridViewRowEventArgs (Rows[NewRowIndex]));
@@ -5633,13 +5763,18 @@ namespace System.Windows.Forms {
 
                internal void OnHScrollBarScroll (object sender, ScrollEventArgs e)
                {
+                       int lastRightVisibleColumntIndex = Columns.Count - DisplayedColumnCount (false);
                        horizontalScrollingOffset = e.NewValue;
                        int left = 0;
 
                        for (int index = 0; index < Columns.Count; index++) {
                                DataGridViewColumn col = Columns[index];
 
-                               if (e.NewValue < left + col.Width) {
+                               if (col.Index >= lastRightVisibleColumntIndex) {
+                                       first_col_index = lastRightVisibleColumntIndex;
+                                       Invalidate ();
+                                       OnScroll (e);
+                               } else if (e.NewValue < left + col.Width) {
                                        if (first_col_index != index) {
                                                first_col_index = index;
                                                Invalidate ();
@@ -5669,6 +5804,8 @@ namespace System.Windows.Forms {
 
                                if (row.Index >= lastTopVisibleRowIndex) {
                                        first_row_index = lastTopVisibleRowIndex;
+                                       Invalidate ();
+                                       OnScroll (e);
                                } else if (e.NewValue < top + row.Height) {
                                        if (first_row_index != index) {
                                                first_row_index = index;
@@ -5700,6 +5837,8 @@ namespace System.Windows.Forms {
                                        OnColumnPostRemovedInternal(new DataGridViewColumnEventArgs(e.Element as DataGridViewColumn));
                                        break;
                                case CollectionChangeAction.Refresh:
+                                       hover_cell = null;
+                                       MoveCurrentCell (-1, -1, true, false, false, true);
                                        break;
                        }
                }
@@ -5732,6 +5871,9 @@ namespace System.Windows.Forms {
                        for (int i = 0; i < Columns.Count; i++) {
                                DataGridViewColumn col = Columns [i];
 
+                               if (!col.Visible)
+                                       continue;
+
                                switch (col.InheritedAutoSizeMode) {
                                case DataGridViewAutoSizeColumnMode.Fill:
                                        FillCount++;
@@ -5758,6 +5900,9 @@ namespace System.Windows.Forms {
                                        
                                        if (col.InheritedAutoSizeMode != DataGridViewAutoSizeColumnMode.Fill)
                                                continue;
+                               
+                                       if (!col.Visible)
+                                               continue;
                                                
                                        if (fixed_widths [i] != 0)
                                                continue;
@@ -5782,7 +5927,10 @@ namespace System.Windows.Forms {
                        for (int i = 0; i < columns.Count; i++) {
                                if (Columns [i].InheritedAutoSizeMode != DataGridViewAutoSizeColumnMode.Fill)
                                        continue;
-                                       
+
+                               if (!Columns[i].Visible)
+                                       continue;
+
                                Columns [i].Width = new_widths [i];
                        }
                }
@@ -5823,26 +5971,20 @@ namespace System.Windows.Forms {
                internal int CalculateColumnCellWidth (int index, DataGridViewAutoSizeColumnMode mode)
                {
                        int first_row = 0;
+                       int last_row = Rows.Count;
                        int result = 0;
-                       bool only_visible = false;
-                       
-                       if (mode == DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader || 
-                               mode == DataGridViewAutoSizeColumnMode.AllCellsExceptHeader)
-                               first_row++;
-                       
-                       only_visible = (mode == DataGridViewAutoSizeColumnMode.DisplayedCells || mode == DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader);
+
+                       if (mode == DataGridViewAutoSizeColumnMode.DisplayedCells || 
+                           mode == DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader) {
+                               first_row = first_row_index;
+                               last_row = DisplayedRowCount (true);;
+                       }
                        
-                       for (int i = first_row; i < Rows.Count; i++) {
+                       for (int i = first_row; i < last_row; i++) {
                                if (!Rows[i].Visible)
                                        continue;
-                               if (only_visible) {
-                                       Rectangle row_rect = this.GetRowDisplayRectangle (i, false);
-                                       if (!ClientRectangle.IntersectsWith (row_rect))
-                                               continue;
-                               }
                                
                                int cell_width = Rows[i].Cells[index].PreferredSize.Width;
-
                                result = Math.Max (result, cell_width);
                        }
                        
@@ -5920,30 +6062,13 @@ namespace System.Windows.Forms {
                                return;
                                
                        DataGridViewRow row = (DataGridViewRow)RowTemplateFull;
-                       rows.InternalAdd (row);
-
-                       PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (element);
-                       
-                       foreach (PropertyDescriptor property in properties) {
-                               if (property.PropertyType == typeof (IBindingList))
-                                       continue;
-                               
-                               // We do it this way because there may not be a column
-                               // for every cell, ignore cells with no column  
-                               DataGridViewCell cell = row.Cells.GetBoundCell (property.Name);
-                               
-                               if (cell == null)
-                                       continue;
-                                       
-                               cell.Value = property.GetValue (element);
-                               cell.ValueType = property.PropertyType;
-                       }
+                       rows.AddInternal (row, false);
                }
                
                private bool IsColumnAlreadyBound (string name)
                {
                        foreach (DataGridViewColumn col in Columns)
-                               if (col.DataPropertyName == name)
+                               if (String.Compare (col.DataPropertyName, name, true) == 0)
                                        return true;
 
                        return false;
@@ -6006,12 +6131,12 @@ namespace System.Windows.Forms {
                                                if (!property.IsBrowsable)
                                                        continue;
 
-                                               if (IsColumnAlreadyBound (property.DisplayName))
+                                               if (IsColumnAlreadyBound (property.Name))
                                                        continue;
 
                                                DataGridViewColumn col = CreateColumnByType (property.PropertyType);
                                                col.Name = property.DisplayName;
-                                               col.DataPropertyName = property.DisplayName;
+                                               col.DataPropertyName = property.Name;
                                                col.ReadOnly = !DataManager.AllowEdit || property.IsReadOnly;
                                                col.SetIsDataBound (true);
                                                col.ValueType = property.PropertyType;
@@ -6023,11 +6148,10 @@ namespace System.Windows.Forms {
                                }
 
                                // DataBind both autogenerated and not columns if there is a matching property
-                               foreach (PropertyDescriptor property in DataManager.GetItemProperties()) {
-                                       foreach (DataGridViewColumn col in Columns) {
-                                               if (col.DataPropertyName == property.Name)
-                                                       col.SetIsDataBound (true);
-                                       }
+                               foreach (DataGridViewColumn column in columns) {
+                                       column.DataColumnIndex = FindDataColumnIndex (column);
+                                       if (column.DataColumnIndex != -1)
+                                               column.SetIsDataBound (true);
                                }
 
                                foreach (object element in DataManager.List)
@@ -6043,8 +6167,6 @@ namespace System.Windows.Forms {
                        }
 
                        PrepareEditingRow (false, true);
-                       PerformLayout();
-                       Invalidate ();
                }
                
                private void MoveCurrentCell (int x, int y, bool select, bool isControl, bool isShift, bool scroll)
@@ -6111,14 +6233,14 @@ namespace System.Windows.Forms {
                        if (scroll) {
                                int disp_x = ColumnIndexToDisplayIndex (x);
                                bool scrollbarsRefreshed = false;
+                               int displayedColumnsCount = DisplayedColumnCount (false);
+                               int delta_x = 0;
 
+                               // The trick here is that in order to avoid unnecessary calculations each time a row/column 
+                               // is added/removed we recalculate the whole grid size just before the scroll to selection.
                                if (disp_x < first_col_index) {
-                                       // The trick here is that in order to avoid unnecessary calculations each time a row/column 
-                                       // is added/removed we recalculate the whole grid size just before just before the scroll 
-                                       // to selection.
                                        RefreshScrollBars ();
                                        scrollbarsRefreshed = true;
-                                       int delta_x = 0;
 
                                        if (disp_x == 0)
                                                delta_x = horizontalScrollBar.Value;
@@ -6132,6 +6254,18 @@ namespace System.Windows.Forms {
                                
                                        horizontalScrollBar.SafeValueSet (horizontalScrollBar.Value - delta_x);
                                        OnHScrollBarScroll (this, new ScrollEventArgs (ScrollEventType.ThumbPosition, horizontalScrollBar.Value));
+                               } else if (disp_x > first_col_index + displayedColumnsCount - 1) {
+                                       RefreshScrollBars ();
+                                       scrollbarsRefreshed = true;
+                                       
+                                       if (disp_x == Columns.Count - 1)
+                                               delta_x = horizontalScrollBar.Maximum - horizontalScrollBar.Value;
+                                       else
+                                               for (int i = first_col_index + displayedColumnsCount - 1; i < disp_x; i++)
+                                                       delta_x += Columns[ColumnDisplayIndexToIndex (i)].Width;
+
+                                       horizontalScrollBar.SafeValueSet (horizontalScrollBar.Value + delta_x);
+                                       OnHScrollBarScroll (this, new ScrollEventArgs (ScrollEventType.ThumbPosition, horizontalScrollBar.Value));
                                }
 
                                int disp_y = y;
@@ -6234,10 +6368,15 @@ namespace System.Windows.Forms {
                private void ReBind ()
                {
                        if (!is_binding) {
+                               SuspendLayout ();
+
                                is_binding = true;
                                ClearBinding ();
                                DoBinding ();
                                is_binding = false;
+
+                               ResumeLayout (true);
+                               Invalidate ();
                        }
                }