/// to the data cache, or the new cell is in a hidden
/// row.
/////////////////////////////////////////////////////
- if (value.DataGridView != this)
+ if (value == null)
+ MoveCurrentCell (-1, -1, true, false, false, true);
+ else if (value.DataGridView != this)
throw new ArgumentException("The cell is not in this DataGridView.");
-
- if (value != null)
- MoveCurrentCell (value.OwningColumn.Index, value.OwningRow.Index, true, false, false, true);
else
- MoveCurrentCell (-1, -1, true, false, false, true);
+ MoveCurrentCell (value.OwningColumn.Index, value.OwningRow.Index, true, false, false, true);
}
}
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;
ReBind ();
OnDataSourceChanged (EventArgs.Empty);
cell.PositionEditingControl (true, true, this.GetCellDisplayRectangle (cell.ColumnIndex, cell.RowIndex, false), bounds, style, false, false, (columns [cell.ColumnIndex].DisplayIndex == 0), (cell.RowIndex == 0));
// Show the editing control
- EditingControlInternal.Visible = true;
+ if (EditingControlInternal != null)
+ EditingControlInternal.Visible = true;
IDataGridViewEditingControl dgvEditingControl = (IDataGridViewEditingControl) EditingControlInternal;
if (dgvEditingControl != null) {
DataManager.CancelCurrentEdit ();
new_row_editing = false;
- PrepareEditingRow (true, false);
+ PrepareEditingRow (false, false);
+ MoveCurrentCell (currentCell.ColumnIndex, NewRowIndex, true, false, false, true);
OnUserDeletedRow (new DataGridViewRowEventArgs (EditingRow));
}
}
Focus ();
if (currentCell.RowIndex == NewRowIndex) {
new_row_editing = false;
- PrepareEditingRow (false, false);
+ editing_row = null; // editing row becomes a real row
+ PrepareEditingRow (true, false); // add a new editing row
+ MoveCurrentCell (currentCell.ColumnIndex, NewRowIndex, true, false, false, true);
}
return true;
}
//
if (!is_autogenerating_columns && columns.Count == 1)
ReBind ();
-
foreach (DataGridViewRow row in Rows)
row.Cells.Add ((DataGridViewCell)e.Column.CellTemplate.Clone ());
}
internal void OnRowsAddedInternal (DataGridViewRowsAddedEventArgs e)
{
+ if (hover_cell != null && hover_cell.RowIndex >= e.RowIndex)
+ hover_cell = null;
+
AutoResizeColumnsInternal ();
Invalidate ();
OnRowsAdded (e);
if (Rows.Count == 0) {
MoveCurrentCell (-1, -1, true, false, false, true);
+ hover_cell = null;
} else if (Columns.Count == 0) {
MoveCurrentCell (-1, -1, true, false, false, true);
+ hover_cell = null;
} else {
int nextRowIndex = e.RowIndex;
if (nextRowIndex >= Rows.Count)
nextRowIndex = Rows.Count - 1;
MoveCurrentCell (currentCell != null ? currentCell.ColumnIndex : 0, nextRowIndex,
true, false, false, true);
+ if (hover_cell != null && hover_cell.RowIndex >= e.RowIndex)
+ hover_cell = null;
}
Invalidate ();
protected virtual void OnUserAddedRow (DataGridViewRowEventArgs e)
{
- // Switch the current editing row with a real
- int newRowIndex = NewRowIndex;
- int currentColumnIndex = currentCell != null ? currentCell.ColumnIndex : 0;
- new_row_editing = true;
PrepareEditingRow (false, false);
- if (DataManager != null)
+
+ new_row_editing = true;
+ if (DataManager != null) {
+ // Switch the current editing row with a real one
+ if (editing_row != null) {
+ Rows.RemoveInternal (editing_row);
+ editing_row = null;
+ }
DataManager.AddNew (); // will raise OnListPositionChanged
- else
- MoveCurrentCell (currentColumnIndex, NewRowIndex, true, false, false, true);
+ }
e = new DataGridViewRowEventArgs (Rows[NewRowIndex]);
DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserAddedRowEvent]);
if (cell != null) {
if (cell.KeyEntersEditMode (new KeyEventArgs ((Keys)m.WParam.ToInt32 ())))
BeginEdit (true);
- if (EditingControl != null && (Msg)m.Msg == Msg.WM_KEYDOWN || (Msg)m.Msg == Msg.WM_CHAR)
+ if (EditingControl != null && ((Msg)m.Msg == Msg.WM_KEYDOWN || (Msg)m.Msg == Msg.WM_CHAR))
XplatUI.SendMessage (EditingControl.Handle, (Msg)m.Msg, m.WParam, m.LParam);
}
if (cell != currentCell) {
if (currentCell != null) {
- if (currentCell.IsInEditMode && !EndEdit ())
- return false;
- else if (currentCell.RowIndex == NewRowIndex && new_row_editing)
- CancelEdit ();
+ if (currentCell.IsInEditMode) {
+ if (!EndEdit ())
+ return false;
+ else if (currentCell.RowIndex == NewRowIndex && new_row_editing)
+ CancelEdit ();
+ }
OnCellLeave (new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
OnRowLeave (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
}
return bounds;
}
- internal void PrepareEditingRow (bool cell_changed, bool column_changed)
+ private void PrepareEditingRow (bool cell_changed, bool column_changed)
{
- if (new_row_editing) {
- if (editing_row != null) {
- Rows.RemoveInternal (editing_row);
- editing_row = null;
- }
+ if (new_row_editing)
return;
- }
bool show = false;
show = ColumnCount > 0 && AllowUserToAddRows;
- if (!show && editing_row != null) {
- Rows.RemoveInternal (editing_row);
- editing_row = null;
+ if (!show) {
+ RemoveEditingRow ();
} else if (show) {
if (editing_row != null && (cell_changed || column_changed)) {
// The row changed, it's no longer an editing row.
// or
// The number of columns has changed, we need a new editing row.
- Rows.RemoveInternal (editing_row);
- editing_row = null;
+ RemoveEditingRow ();
}
if (editing_row == null) {
editing_row = RowTemplateFull;
}
}
+ internal void RemoveEditingRow ()
+ {
+ if (editing_row != null) {
+ if (Rows.Contains (editing_row))
+ Rows.RemoveInternal (editing_row);
+ editing_row = null;
+ }
+ }
+
internal DataGridViewRow EditingRow {
get { return editing_row; }
}
{
if (type == typeof (bool))
return new DataGridViewCheckBoxColumn ();
+ else if (typeof(Bitmap).IsAssignableFrom (type))
+ return new DataGridViewImageColumn ();
return new DataGridViewTextBoxColumn ();
}
private void ClearBinding ()
{
- columns.ClearAutoGeneratedColumns ();
- rows.Clear ();
- PrepareEditingRow (false, true);
if (DataManager != null) {
DataManager.ListChanged -= OnListChanged;
DataManager.PositionChanged -= OnListPositionChanged;
+ columns.ClearAutoGeneratedColumns ();
+ rows.Clear ();
+ RemoveEditingRow ();
}
}
MoveCurrentCell (0, 0, true, false, false, false);
}
+ PrepareEditingRow (false, true);
PerformLayout();
Invalidate ();
}