private HScrollBar horizontalScrollBar;
private VScrollBar verticalScrollBar;
private Control editingControl;
-
+ private bool new_row_commited = true;
+
// These are used to implement selection behaviour with SHIFT pressed.
private int selected_row = -1;
private int selected_column = -1;
DataGridViewHeaderCell pressed_header_cell;
DataGridViewHeaderCell entered_header_cell;
+ // For column/row resizing via mouse
+ private bool column_resize_active = false;
+ private bool row_resize_active = false;
+ private int resize_band = -1;
+ private int resize_band_start = 0;
+ private int resize_band_delta = 0;
+
public DataGridView ()
{
SetStyle (ControlStyles.Opaque, true);
/// to the data cache, or the new cell is in a hidden
/// row.
/////////////////////////////////////////////////////
- if (value.DataGridView != this) {
+ if (value.DataGridView != this)
throw new ArgumentException("The cell is not in this DataGridView.");
- }
- currentCell = value;
- currentRow = currentCell.OwningRow;
+
+ if (value != null)
+ MoveCurrentCell (value.OwningColumn.Index, value.OwningRow.Index, true, false, false, true);
+ else
+ MoveCurrentCell (-1, -1, true, false, false, true);
}
}
[Browsable (false)]
public DataGridViewRow CurrentRow {
- get { return currentRow; }
+ get {
+ if (currentCell != null)
+ return currentCell.OwningRow;
+ return null;
+ }
}
[DefaultValue ("")]
public object DataSource {
get { return dataSource; }
set {
- if (dataSource != value) {
- /* The System.Windows.Forms.DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements:
- - the System.Collections.IList interface, including one-dimensional arrays.
- - the System.ComponentModel.IListSource interface, such as the System.Data.DataTable and System.Data.DataSet classes.
- - the System.ComponentModel.IBindingList interface, such as the System.ComponentModel.Collections.BindingList<> class.
- - the System.ComponentModel.IBindingListView interface, such as the System.Windows.Forms.BindingSource class.
- */
- if (!(value == null || value is IList || value is IListSource || value is IBindingList || value is IBindingListView)) {
- throw new NotSupportedException("Type cant be binded.");
- }
-
- ClearBinding ();
+ /* The System.Windows.Forms.DataGridView class supports the standard Windows Forms data-binding model. This means the data source can be of any type that implements:
+ - the System.Collections.IList interface, including one-dimensional arrays.
+ - the System.ComponentModel.IListSource interface, such as the System.Data.DataTable and System.Data.DataSet classes.
+ - the System.ComponentModel.IBindingList interface, such as the System.ComponentModel.Collections.BindingList<> class.
+ - the System.ComponentModel.IBindingListView interface, such as the System.Windows.Forms.BindingSource class.
+ */
+ if (!(value == null || value is IList || value is IListSource || value is IBindingList || value is IBindingListView))
+ throw new NotSupportedException ("Type cannot be bound.");
- dataSource = value;
- OnDataSourceChanged (EventArgs.Empty);
-
- DoBinding ();
- }
+ ClearBinding ();
+
+ dataSource = value;
+ OnDataSourceChanged (EventArgs.Empty);
+
+ DoBinding ();
}
}
[DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
public int NewRowIndex {
get {
- if (!allowUserToAddRows) {
+ if (!allowUserToAddRows || ColumnCount == 0) {
return -1;
}
return rows.Count - 1;
ColumnCount = 1;
for (int i = rows.Count; i < value; i++) {
- DataGridViewRow row = (DataGridViewRow) RowTemplate.Clone ();
+ DataGridViewRow row = (DataGridViewRow) RowTemplateFull;
rows.AddInternal (row, false);
foreach (DataGridViewColumn col in columns)
}
}
+ // RowTemplate is just the row, it does not contain Cells
[Browsable (true)]
[DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
public DataGridViewRow RowTemplate {
}
}
+ // Take the RowTemplate, clone it, and add Cells
+ // Note this is not stored, so you don't need to Clone it
internal DataGridViewRow RowTemplateFull {
get {
DataGridViewRow row = (DataGridViewRow) RowTemplate.Clone ();
public virtual bool BeginEdit (bool selectAll) {
if (currentCell == null || currentCell.IsInEditMode)
return false;
-
+
if (currentCell.RowIndex >= 0) {
if ((currentCell.InheritedState & DataGridViewElementStates.ReadOnly) == DataGridViewElementStates.ReadOnly) {
return false;
if (e.Cancel)
return false;
-
+
+ // If the user begins an edit in the NewRow, add a new row
+ if (CurrentCell.RowIndex == NewRowIndex) {
+ new_row_commited = false;
+ OnUserAddedRow (new DataGridViewRowEventArgs (Rows[NewRowIndex]));
+ }
+
cell.SetIsInEditMode (true);
// The cell has an editing control we need to setup
// Show the editing control
EditingControlInternal.Visible = true;
- // Allow editing control to set focus as needed
- (EditingControlInternal as IDataGridViewEditingControl).PrepareEditingControlForEdit (selectAll);
-
+ IDataGridViewEditingControl dgvEditingControl = (IDataGridViewEditingControl) EditingControlInternal;
+ if (dgvEditingControl != null) {
+ dgvEditingControl.EditingControlDataGridView = this;
+ dgvEditingControl.EditingControlRowIndex = currentCell.OwningRow.Index;
+ dgvEditingControl.ApplyCellStyleToEditingControl (style);
+ dgvEditingControl.PrepareEditingControlForEdit (selectAll);
+ dgvEditingControl.EditingControlFormattedValue = currentCell.EditedFormattedValue;
+ }
return true;
}
public bool CancelEdit ()
{
if (currentCell != null && currentCell.IsInEditMode) {
+ // The user's typing caused a new row to be created, but
+ // now they are canceling that typing, we have to remove
+ // the new row we added.
+ if (!new_row_commited) {
+ DataGridViewRow delete_row = EditingRow;
+ Rows.RemoveInternal (delete_row);
+ editing_row = Rows[currentCell.RowIndex];
+ OnUserDeletedRow (new DataGridViewRowEventArgs (delete_row));
+ new_row_commited = true;
+ }
+
currentCell.SetIsInEditMode (false);
currentCell.DetachEditingControl ();
OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
public bool CommitEdit (DataGridViewDataErrorContexts context)
{
- if (currentCell != null && currentCell.IsInEditMode) {
- IDataGridViewEditingControl ctrl = EditingControl as IDataGridViewEditingControl;
- ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
- currentCell.Value = ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
- return true;
+ if (currentCell != null && currentCell.OwningRow.DataBoundItem != null) {
+ Object ob = currentCell.OwningRow.DataBoundItem;
+ PropertyDescriptor property = TypeDescriptor.GetProperties (ob)[currentCell.OwningColumn.DataPropertyName];
+ if (property != null && !property.IsReadOnly) {
+ try {
+ object value = currentCell.Value;
+ if (property.Converter != null &&
+ property.Converter.CanConvertFrom (value.GetType()))
+ value = property.Converter.ConvertFrom (value);
+ property.SetValue (ob, value);
+ return true;
+ } catch (Exception exc) {
+ DataGridViewDataErrorEventArgs args = new DataGridViewDataErrorEventArgs (exc, currentCell.ColumnIndex,
+ currentCell.RowIndex, context);
+ InternalOnDataError (args);
+ if (args.ThrowException)
+ throw exc;
+ return false;
+ }
+ }
}
-
- return false;
+
+ return true;
}
[MonoTODO ("Always includes partial columns")]
return result;
}
- [MonoTODO ("Always includes partial rows")]
public int DisplayedRowCount (bool includePartialRow)
{
int result = 0;
-
- for (int i = first_row_index; i < Rows.Count; i++)
- if (Rows[i].Displayed)
+ int rowTop = 0;
+
+ if (ColumnHeadersVisible)
+ rowTop += ColumnHeadersHeight;
+
+ for (int index = first_row_index; index < Rows.Count; index++) {
+ DataGridViewRow row = GetRowInternal (index);
+ if (rowTop + row.Height < ClientSize.Height) {
result++;
- else
+ rowTop += row.Height;
+ } else {
+ if (includePartialRow)
+ result++;
break;
+ }
+ }
return result;
}
public bool EndEdit ()
{
- if (currentCell != null && currentCell.IsInEditMode) {
- if (EditingControl != null) {
- IDataGridViewEditingControl ctrl = EditingControl as IDataGridViewEditingControl;
- ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
- currentCell.Value = ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
-
- currentCell.SetIsInEditMode (false);
- currentCell.DetachEditingControl ();
- } else if (currentCell is IDataGridViewEditingCell) {
- currentCell.Value = (currentCell as IDataGridViewEditingCell).EditingCellFormattedValue;
- currentCell.SetIsInEditMode (false);
- }
-
- OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
- }
-
- return true;
+ return EndEdit (DataGridViewDataErrorContexts.Commit);
}
[MonoTODO ("Does not use context parameter")]
public bool EndEdit (DataGridViewDataErrorContexts context)
{
- return EndEdit ();
+ if (currentCell == null || !currentCell.IsInEditMode)
+ return true;
+
+ if (EditingControl != null) {
+ IDataGridViewEditingControl ctrl = EditingControl as IDataGridViewEditingControl;
+ currentCell.Value = ctrl.GetEditingControlFormattedValue (DataGridViewDataErrorContexts.Commit);
+ if (!CommitEdit (context)) {
+ EditingControl.Focus ();
+ return false;
+ }
+ currentCell.DetachEditingControl ();
+ } else if (currentCell is IDataGridViewEditingCell) {
+ currentCell.Value = (currentCell as IDataGridViewEditingCell).EditingCellFormattedValue;
+ if (!CommitEdit (context))
+ return false;
+ }
+
+ currentCell.SetIsInEditMode (false);
+ new_row_commited = true;
+ OnCellEndEdit (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
+ Focus ();
+ return true;
}
public int GetCellCount (DataGridViewElementStates includeFilter) {
}
public virtual void NotifyCurrentCellDirty (bool dirty) {
- throw new NotImplementedException();
+ if (currentCell != null)
+ InvalidateCell (currentCell);
}
public bool RefreshEdit ()
throw new ArgumentNullException ("dataGridViewColumn");
if (dataGridViewColumn.DataGridView != this)
throw new ArgumentException ("dataGridViewColumn");
- // XXX: This is thrown too much, disable for now..
- //if (DataSource != null && !dataGridViewColumn.IsDataBound)
- // throw new ArgumentException ("dataGridViewColumn");
- if (VirtualMode && !dataGridViewColumn.IsDataBound)
- throw new InvalidOperationException ();
+
+ if (!EndEdit ())
+ return;
if (SortedColumn != null)
SortedColumn.HeaderCell.SortGlyphDirection = SortOrder.None;
- EndEdit ();
-
- // Figure out if this is a numeric sort or text sort
- bool is_numeric = true;
- double n;
+ sortedColumn = dataGridViewColumn;
+ sortOrder = direction == ListSortDirection.Ascending ? SortOrder.Ascending : SortOrder.Descending;
- foreach (DataGridViewRow row in Rows) {
- object val = row.Cells[dataGridViewColumn.Index].Value;
+ if (Rows.Count == 0)
+ return;
+
+ IBindingList bindingList = DataSource as IBindingList;
+ if (dataGridViewColumn.IsDataBound) {
+ if (bindingList != null && bindingList.SupportsSorting) {
+ CurrencyManager currencyManager = (CurrencyManager) this.BindingContext[DataSource];
+ bindingList.ApplySort (currencyManager.GetItemProperties()[dataGridViewColumn.DataPropertyName], direction);
+ dataGridViewColumn.HeaderCell.SortGlyphDirection = sortOrder;
+ }
+ } else {
+ // Figure out if this is a numeric sort or text sort
+ bool is_numeric = true;
+ double n;
- if (val != null && !double.TryParse (val.ToString (), out n)) {
- is_numeric = false;
- break;
+ foreach (DataGridViewRow row in Rows) {
+ object val = row.Cells[dataGridViewColumn.Index].Value;
+
+ if (val != null && !double.TryParse (val.ToString (), out n)) {
+ is_numeric = false;
+ break;
+ }
}
+
+ ColumnSorter sorter = new ColumnSorter (dataGridViewColumn, direction, is_numeric);
+ Rows.Sort (sorter);
+ dataGridViewColumn.HeaderCell.SortGlyphDirection = sortOrder;
}
-
- ColumnSorter sorter = new ColumnSorter (dataGridViewColumn, direction, is_numeric);
- Rows.Sort (sorter);
-
- sortedColumn = dataGridViewColumn;
- sortOrder = (SortOrder)direction + 1;
-
- dataGridViewColumn.HeaderCell.SortGlyphDirection = (SortOrder)direction + 1;
Invalidate ();
-
OnSorted (EventArgs.Empty);
}
eh (this, e);
}
- protected virtual void OnAutoSizeColumnModeChanged (DataGridViewAutoSizeColumnModeEventArgs e)
+ protected internal virtual void OnAutoSizeColumnModeChanged (DataGridViewAutoSizeColumnModeEventArgs e)
{
DataGridViewAutoSizeColumnModeEventHandler eh = (DataGridViewAutoSizeColumnModeEventHandler)(Events [AutoSizeColumnModeChangedEvent]);
if (eh != null)
eh (this, e);
}
+ internal void OnCellStateChangedInternal (DataGridViewCellStateChangedEventArgs e) {
+ this.OnCellStateChanged (e);
+ }
+
protected virtual void OnCellStateChanged (DataGridViewCellStateChangedEventArgs e)
{
DataGridViewCellStateChangedEventHandler eh = (DataGridViewCellStateChangedEventHandler)(Events [CellStateChangedEvent]);
internal void OnColumnAddedInternal (DataGridViewColumnEventArgs e)
{
if (e.Column.CellTemplate != null) {
- RowTemplate.Cells.Add ((DataGridViewCell)e.Column.CellTemplate.Clone ());
-
foreach (DataGridViewRow row in Rows)
- row.Cells.Add ((DataGridViewCell)RowTemplate.Cells[RowTemplate.Cells.Count - 1].Clone ());
+ row.Cells.Add ((DataGridViewCell)e.Column.CellTemplate.Clone ());
}
AutoResizeColumnsInternal ();
eh (this, e);
}
+ internal void OnColumnRemovedInternal (DataGridViewColumnEventArgs e)
+ {
+ if (e.Column.CellTemplate != null) {
+ int index = e.Column.Index;
+
+ foreach (DataGridViewRow row in Rows)
+ row.Cells.RemoveAt (index);
+ }
+
+ AutoResizeColumnsInternal ();
+ OnColumnRemoved (e);
+ PrepareEditingRow (false, true);
+ }
+
protected virtual void OnColumnRemoved (DataGridViewColumnEventArgs e)
{
DataGridViewColumnEventHandler eh = (DataGridViewColumnEventHandler)(Events [ColumnRemovedEvent]);
{
base.OnHandleCreated(e);
- if (Rows.Count > 0 && Columns.Count > 0)
+ if (CurrentCell == null && Rows.Count > 0 && Columns.Count > 0)
MoveCurrentCell (ColumnDisplayIndexToIndex (0), 0, true, false, false, false);
}
horizontalScrollBar.Bounds = new Rectangle (BorderWidth, Height - BorderWidth - horizontalScrollBar.Height, Width - (2 * BorderWidth), horizontalScrollBar.Height);
else if (verticalScrollBar.Visible)
verticalScrollBar.Bounds = new Rectangle (Width - BorderWidth - verticalScrollBar.Width, BorderWidth, verticalScrollBar.Width, Height - (2 * BorderWidth));
-
+
+ AutoResizeColumnsInternal ();
Invalidate ();
}
protected override void OnMouseClick (MouseEventArgs e)
{
base.OnMouseClick(e);
+
+ if (column_resize_active || row_resize_active)
+ return;
+
//Console.WriteLine("Mouse: Clicks: {0}; Delta: {1}; X: {2}; Y: {3};", e.Clicks, e.Delta, e.X, e.Y);
HitTestInfo hit = HitTest (e.X, e.Y);
{
base.OnMouseDown(e);
+ if (!EndEdit ())
+ return;
+
HitTestInfo hitTest = HitTest(e.X, e.Y);
DataGridViewCell cell = null;
DataGridViewRow row = null;
Rectangle cellBounds;
+ if (hitTest.Type == DataGridViewHitTestType.ColumnHeader && MouseOverColumnResize (hitTest.ColumnIndex, e.X)) {
+ if (e.Clicks == 2) {
+ AutoResizeColumn (hitTest.ColumnIndex);
+ return;
+ }
+
+ resize_band = hitTest.ColumnIndex;
+ column_resize_active = true;
+ resize_band_start = e.X;
+ resize_band_delta = 0;
+ DrawVerticalResizeLine (resize_band_start);
+ return;
+ }
+
+ if (hitTest.Type == DataGridViewHitTestType.RowHeader && MouseOverRowResize (hitTest.RowIndex, e.Y)) {
+ if (e.Clicks == 2) {
+ AutoResizeRow (hitTest.RowIndex);
+ return;
+ }
+
+ resize_band = hitTest.RowIndex;
+ row_resize_active = true;
+ resize_band_start = e.Y;
+ resize_band_delta = 0;
+ DrawHorizontalResizeLine (resize_band_start);
+ return;
+ }
+
if (hitTest.Type == DataGridViewHitTestType.Cell) {
+ row = rows [hitTest.RowIndex];
+ cell = row.Cells [hitTest.ColumnIndex];
+ SetCurrentCellAddressCore (cell.ColumnIndex, cell.RowIndex, false, true, true);
cellBounds = GetCellDisplayRectangle (hitTest.ColumnIndex, hitTest.RowIndex, false);
OnCellMouseDown (new DataGridViewCellMouseEventArgs (hitTest.ColumnIndex, hitTest.RowIndex, e.X - cellBounds.X, e.Y - cellBounds.Y, e));
OnCellClick (new DataGridViewCellEventArgs (hitTest.ColumnIndex, hitTest.RowIndex));
- row = rows [hitTest.RowIndex];
- cell = row.Cells [hitTest.ColumnIndex];
}
DoSelectionOnMouseDown (hitTest);
return;
}
- if (cell == currentCell) {
- BeginEdit (true);
- return;
- } else if (currentCell != null) {
- EndEdit ();
- OnCellLeave(new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
- }
- currentCell = cell;
- currentCellAddress = new Point (currentCell.ColumnIndex, currentCell.RowIndex);
- currentRow = cell.OwningRow;
- OnCurrentCellChanged(EventArgs.Empty);
- OnCellEnter(new DataGridViewCellEventArgs(cell.ColumnIndex, cell.RowIndex));
- if (editMode == DataGridViewEditMode.EditOnEnter) {
- BeginEdit (true);
- }
Invalidate();
return;
}
+ private void UpdateBindingPosition (int position)
+ {
+ if (DataSource != null && BindingContext != null) {
+ CurrencyManager currencyManager = this.BindingContext[DataSource] as CurrencyManager;
+ if (currencyManager != null)
+ currencyManager.Position = position;
+ }
+ }
+
protected override void OnMouseEnter (EventArgs e)
{
base.OnMouseEnter(e);
protected override void OnMouseMove (MouseEventArgs e)
{
base.OnMouseMove (e);
-
+
+ if (column_resize_active) {
+ // Erase the old line
+ DrawVerticalResizeLine (resize_band_start + resize_band_delta);
+
+ resize_band_delta = e.X - resize_band_start;
+
+ // Draw the new line
+ DrawVerticalResizeLine (resize_band_start + resize_band_delta);
+ return;
+ }
+
+ if (row_resize_active) {
+ // Erase the old line
+ DrawHorizontalResizeLine (resize_band_start + resize_band_delta);
+
+ resize_band_delta = e.Y - resize_band_start;
+
+ // Draw the new line
+ DrawHorizontalResizeLine (resize_band_start + resize_band_delta);
+ return;
+ }
+
+ Cursor new_cursor = Cursors.Default;
HitTestInfo hit = this.HitTest (e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell) {
EnteredHeaderCell = new_cell;
+ if (MouseOverRowResize (hit.RowIndex, e.Y))
+ new_cursor = Cursors.HSplit;
+
// Check if we have moved into an error icon area
Rectangle icon = new_cell.InternalErrorIconsBounds;
}
} else {
- if (hit.Type == DataGridViewHitTestType.ColumnHeader)
+ if (hit.Type == DataGridViewHitTestType.ColumnHeader) {
EnteredHeaderCell = Columns [hit.ColumnIndex].HeaderCell;
- else
+
+ if (MouseOverColumnResize (hit.ColumnIndex, e.X))
+ new_cursor = Cursors.VSplit;
+ } else
EnteredHeaderCell = null;
// We have left the cell area
hover_cell = null;
}
}
+
+ Cursor = new_cursor;
}
protected override void OnMouseUp (MouseEventArgs e)
{
base.OnMouseUp(e);
+ if (column_resize_active) {
+ column_resize_active = false;
+
+ if (resize_band_delta + Columns[resize_band].Width < 0)
+ resize_band_delta = -Columns[resize_band].Width;
+
+ Columns[resize_band].Width = Math.Max (resize_band_delta + Columns[resize_band].Width, Columns[resize_band].MinimumWidth);
+ Invalidate ();
+ return;
+ }
+
+ if (row_resize_active) {
+ row_resize_active = false;
+
+ if (resize_band_delta + Rows[resize_band].Height < 0)
+ resize_band_delta = -Rows[resize_band].Height;
+
+ Rows[resize_band].Height = Math.Max (resize_band_delta + Rows[resize_band].Height, Rows[resize_band].MinimumHeight);
+ Invalidate ();
+ return;
+ }
+
HitTestInfo hit = this.HitTest (e.X, e.Y);
if (hit.Type == DataGridViewHitTestType.Cell) {
gridWidth = rowHeadersVisible ? rowHeadersWidth : 0;
gridHeight = 0;
- int rows_displayed = 0;
int first_row_height = Rows.Count > 0 ? Rows[Math.Min (Rows.Count - 1, first_row_index)].Height : 0;
// int room_left = this.Height;
bounds.Y += bounds.Height;
bounds.X = BorderWidth;
- if (bounds.Y < ClientSize.Height - (horizontalScrollBar.Visible ? horizontalScrollBar.Height : 0))
- rows_displayed++;
- else
+ if (bounds.Y >= ClientSize.Height - (horizontalScrollBar.Visible ? horizontalScrollBar.Height : 0))
break;
gridHeight += row.Height;
if (eh != null) eh (this, e);
}
- protected internal virtual void OnRowsRemoved (DataGridViewRowsRemovedEventArgs e)
+ internal void OnRowsRemovedInternal (DataGridViewRowsRemovedEventArgs e)
+ {
+ if (selected_rows != null)
+ selected_rows.InternalClear ();
+ if (selected_columns != null)
+ selected_columns.InternalClear ();
+
+ if (Rows.Count > 0 && Columns.Count > 0 && currentCell != null &&
+ currentCell.RowIndex >= e.RowIndex)
+ MoveCurrentCell (0, Math.Min (e.RowIndex, Rows.Count - 2), true, false, false, true);
+ Invalidate ();
+ OnRowsRemoved (e);
+ }
+
+ protected virtual void OnRowsRemoved (DataGridViewRowsRemovedEventArgs e)
{
DataGridViewRowsRemovedEventHandler eh = (DataGridViewRowsRemovedEventHandler)(Events [RowsRemovedEvent]);
if (eh != null) eh (this, e);
protected virtual void OnUserAddedRow (DataGridViewRowEventArgs e)
{
+ editing_row = null;
+ PrepareEditingRow (false, false);
+
+ e = new DataGridViewRowEventArgs (editing_row);
+
DataGridViewRowEventHandler eh = (DataGridViewRowEventHandler)(Events [UserAddedRowEvent]);
if (eh != null) eh (this, e);
}
Rows.RemoveAt (row.Index);
}
- if (selected_rows != null)
- selected_rows.InternalClear ();
- if (selected_columns != null)
- selected_columns.InternalClear ();
-
- SetSelectedCellCore (0, Math.Min (index, Rows.Count - 1), true);
-
return true;
}
if (ProcessDataGridViewKey (new KeyEventArgs (keyData)))
return true;
+ break;
+ case Keys.Enter:
+ case Keys.Escape:
+ if (ProcessDataGridViewKey (new KeyEventArgs (keyData)))
+ return true;
+
break;
}
int current_row = CurrentCellAddress.Y;
if (current_row < Rows.Count - 1) {
- EndEdit ();
-
// Move to the last cell in the column
if ((keyData & Keys.Control) == Keys.Control)
MoveCurrentCell (CurrentCellAddress.X, Rows.Count - 1, true, (keyData & Keys.Control) == Keys.Control, (keyData & Keys.Shift) == Keys.Shift, true);
protected bool ProcessEnterKey (Keys keyData)
{
- if (!IsCurrentCellInEditMode)
- return false;
-
- CommitEdit (DataGridViewDataErrorContexts.Commit);
-
- // Move one cell down
- if ((keyData & Keys.Control) == 0) {
- int current_row = CurrentCellAddress.Y;
-
- if (current_row < Rows.Count - 1)
- MoveCurrentCell (CurrentCellAddress.X, current_row + 1, true, (keyData & Keys.Control) == Keys.Control, (keyData & Keys.Shift) == Keys.Shift, true);
- }
+ if (ProcessDownKey (keyData))
+ return true;
+ // ProcessDown may fail if we are on the last row,
+ // but Enter should still EndEdit if this is the last row
+ EndEdit ();
return true;
}
int disp_index = ColumnIndexToDisplayIndex (currentCellAddress.X);
if (disp_index > 0) {
- EndEdit ();
-
// Move to the first cell in the row
if ((keyData & Keys.Control) == Keys.Control)
MoveCurrentCell (ColumnDisplayIndexToIndex (0), currentCellAddress.Y, true, (keyData & Keys.Control) == Keys.Control, (keyData & Keys.Shift) == Keys.Shift, true);
int current_row = CurrentCellAddress.Y;
if (current_row < Rows.Count - 1) {
- EndEdit ();
-
// Move one "page" of cells down
int new_row = Math.Min (Rows.Count - 1, current_row + DisplayedRowCount (false));
int current_row = CurrentCellAddress.Y;
if (current_row > 0) {
- EndEdit ();
-
// Move one "page" of cells up
int new_row = Math.Max (0, current_row - DisplayedRowCount (false));
int disp_index = ColumnIndexToDisplayIndex (currentCellAddress.X);
if (disp_index < Columns.Count - 1) {
- EndEdit ();
-
// Move to the last cell in the row
if ((keyData & Keys.Control) == Keys.Control)
MoveCurrentCell (ColumnDisplayIndexToIndex (Columns.Count - 1), currentCellAddress.Y, true, (keyData & Keys.Control) == Keys.Control, (keyData & Keys.Shift) == Keys.Shift, true);
protected bool ProcessTabKey (Keys keyData)
{
- EndEdit ();
-
Form f = FindForm ();
if (f != null)
int current_row = CurrentCellAddress.Y;
if (current_row > 0) {
- EndEdit ();
-
// Move to the first cell in the column
if ((keyData & Keys.Control) == Keys.Control)
MoveCurrentCell (CurrentCellAddress.X, 0, true, (keyData & Keys.Control) == Keys.Control, (keyData & Keys.Shift) == Keys.Shift, true);
base.SetBoundsCore(x, y, width, height, specified);
}
- [MonoTODO ("Does not use validateCurrentCell or throughMouseClick")]
+ [MonoTODO ("Does not use validateCurrentCell")]
protected virtual bool SetCurrentCellAddressCore (int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)
{
if ((columnIndex < 0 || columnIndex > Columns.Count - 1) && rowIndex != -1)
if (cell != null && !cell.Visible)
throw new InvalidOperationException ("cell is not visible");
- if (setAnchorCellAddress)
- anchor_cell = new Point (columnIndex, rowIndex);
+ if (cell != currentCell) {
+ if (currentCell != null) {
+ if (currentCell.IsInEditMode && !EndEdit ())
+ return false;
+ OnCellLeave (new DataGridViewCellEventArgs(currentCell.ColumnIndex, currentCell.RowIndex));
+ OnRowLeave (new DataGridViewCellEventArgs (currentCell.ColumnIndex, currentCell.RowIndex));
+ }
+
+ currentCell = cell;
+ if (setAnchorCellAddress)
+ anchor_cell = new Point (columnIndex, rowIndex);
+ currentCellAddress = new Point (columnIndex, rowIndex);
+
+ UpdateBindingPosition (currentCell.RowIndex);
+ OnRowEnter (new DataGridViewCellEventArgs (cell.ColumnIndex, cell.RowIndex));
+ OnCellEnter (new DataGridViewCellEventArgs(cell.ColumnIndex, cell.RowIndex));
+ OnCurrentCellChanged (EventArgs.Empty);
+ if (throughMouseClick || editMode == DataGridViewEditMode.EditOnEnter)
+ BeginEdit (true);
+ } else {
+ if (throughMouseClick)
+ BeginEdit (true);
+ }
- currentCellAddress = new Point (columnIndex, rowIndex);
- CurrentCell = cell;
-
- OnCurrentCellChanged (EventArgs.Empty);
-
return true;
}
} else if (selected && !selected_columns.Contains (col)) {
selected_columns.InternalAdd (col);
}
+
+ Invalidate();
}
internal void SetSelectedRowCoreInternal (int rowIndex, bool selected) {
} else if (selected && !selected_rows.Contains (row)) {
selected_rows.InternalAdd (row);
}
+
+ Invalidate();
}
protected override void WndProc (ref Message m)
internal void InternalOnCellValueChanged (DataGridViewCellEventArgs e)
{
OnCellValueChanged (e);
-
- if (editing_row != null && e.RowIndex == editing_row.Index)
- PrepareEditingRow (true, false);
}
internal void InternalOnDataError (DataGridViewDataErrorEventArgs e)
internal void OnVScrollBarScroll (object sender, ScrollEventArgs e)
{
verticalScrollingOffset = e.NewValue;
+ if (Rows.Count == 0)
+ return;
+
int top = 0;
for (int index = 0; index < Rows.Count; index++) {
DataGridViewRow row = Rows[index];
-
- if (e.NewValue <= top + row.Height) {
+ if (e.NewValue < top + row.Height) {
if (first_row_index != index) {
first_row_index = index;
Invalidate ();
top += row.Height;
}
- if (Rows.Count == 0)
- return;
-
- first_row_index = Rows.Count - DisplayedRowCount (false) + 1;
+ first_row_index = Rows.Count - DisplayedRowCount (false);
Invalidate ();
OnScroll (e);
}
internal void OnColumnCollectionChanged (object sender, CollectionChangeEventArgs e) {
switch (e.Action) {
case CollectionChangeAction.Add:
- OnColumnAdded(new DataGridViewColumnEventArgs(e.Element as DataGridViewColumn));
+ OnColumnAddedInternal(new DataGridViewColumnEventArgs(e.Element as DataGridViewColumn));
break;
case CollectionChangeAction.Remove:
- OnColumnRemoved(new DataGridViewColumnEventArgs(e.Element as DataGridViewColumn));
+ OnColumnRemovedInternal(new DataGridViewColumnEventArgs(e.Element as DataGridViewColumn));
break;
case CollectionChangeAction.Refresh:
break;
{
float totalFillWeight = 0;
int FillCount = 0; // The number of columns that has AutoSizeMode.Fill set
- int spaceLeft = ClientSize.Width;
+ int spaceLeft = ClientSize.Width - (verticalScrollBar.VisibleInternal ? verticalScrollBar.Width : 0);
if (RowHeadersVisible) {
spaceLeft -= RowHeadersWidth;
case DataGridViewAutoSizeColumnMode.AllCells:
case DataGridViewAutoSizeColumnMode.DisplayedCells:
case DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader:
- size = CalculateColumnCellWidth (columnIndex, col.InheritedAutoSizeMode);
+ size = Math.Max (CalculateColumnCellWidth (columnIndex, col.InheritedAutoSizeMode),
+ col.HeaderCell.ContentBounds.Width);
break;
case DataGridViewAutoSizeColumnMode.ColumnHeader:
size = col.HeaderCell.ContentBounds.Width;
continue;
}
- Rectangle cell_rect = GetCellDisplayRectangle (index, i, false);
-
- result = Math.Max (result, cell_rect.Width);
+ int cell_width = Rows[i].Cells[index].PreferredSize.Width;
+
+ result = Math.Max (result, cell_width);
}
return result;
return bounds;
}
- private void PrepareEditingRow (bool cell_changed, bool column_changed)
+ internal void PrepareEditingRow (bool cell_changed, bool column_changed)
{
bool show = false;
show = ColumnCount > 0 && AllowUserToAddRows;
if (!show && editing_row != null) {
- Rows.Remove (editing_row);
+ Rows.RemoveInternal (editing_row);
editing_row = null;
} else if (show) {
if (editing_row != null) {
editing_row = null;
} else if (column_changed) {
// The number of columns has changed, we need a new editing row.
- Rows.Remove (editing_row);
+ Rows.RemoveInternal (editing_row);
editing_row = null;
}
}
// Its a normal array/collection type thing
else if (list.GetType ().IsArray) {
GenerateColumnsFromType (list.GetType ().GetElementType ());
+ } else if (list is BindingSource && (list as BindingSource).item_type != null) {
+ foreach (PropertyDescriptor property in TypeDescriptor.GetProperties ((list as BindingSource).item_type)) {
+ DataGridViewColumn col = CreateColumnByType (property.PropertyType);
+ col.Name = property.DisplayName;
+ col.ReadOnly = property.IsReadOnly;
+ col.AutoGenerated = true;
+ columns.Add (col);
+ }
+
+ AllowUserToAddRows = (list as BindingSource).AllowNew;
}
}
if (list is DataView) {
(list as DataView).ListChanged += OnListChanged;
(list as DataView).Table.ColumnChanged += OnTableColumnChanged;
+ (list as DataView).Table.TableCleared += OnTableCleared;
}
-
+
// Add the rows
foreach (object element in list)
AddBoundRow (element);
}
+ private void OnBindingSourceDataSourceChanged (object sender, EventArgs args)
+ {
+ ClearBinding();
+ DoBinding();
+ }
+
private void AddBoundRow (object element)
{
// Don't add rows if there are no columns
if (ColumnCount == 0)
return;
- DataGridViewRow row = (DataGridViewRow)RowTemplate.Clone ();
+ DataGridViewRow row = (DataGridViewRow)RowTemplateFull;
rows.InternalAdd (row);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties (element);
(dataSource as DataView).Table.ColumnChanged -= OnTableColumnChanged;
} else if (dataSource is DataTable)
((dataSource as IListSource).GetList () as DataView).ListChanged -= OnListChanged;
+
+ if (dataSource is IBindingList)
+ (dataSource as IBindingList).ListChanged -= OnListChanged;
+ if (dataSource is BindingSource)
+ (dataSource as BindingSource).DataSourceChanged -= OnBindingSourceDataSourceChanged;
}
}
(value as DataSet).Tables.CollectionChanged += OnDataSetTableChanged;
value = (value as DataSet).Tables[dataMember];
}
+
if (value is BindingSource)
- value = (value as BindingSource).List;
-
- if (value is IList)
+ BindBindingSource (value as BindingSource);
+ else if (value is IBindingListView)
+ BindIBindingListView (value as IBindingListView);
+ else if (value is IBindingList)
+ BindIBindingList (value as IBindingList);
+ else if (value is IList)
BindIList (value as IList);
else if (value is IListSource)
BindIListSource (value as IListSource);
- else if (value is IBindingList)
- BindIBindingList (value as IBindingList);
- else if (value is IBindingListView)
- BindIBindingListView (value as IBindingListView);
OnDataBindingComplete (new DataGridViewBindingCompleteEventArgs (ListChangedType.Reset));
}
+ if (Rows.Count > 0 && Columns.Count > 0)
+ MoveCurrentCell (0, 0, true, false, false, false);
+ PerformLayout();
Invalidate ();
}
+ private void BindBindingSource (BindingSource bindingSource)
+ {
+ BindIList (bindingSource.List);
+ bindingSource.ListChanged += OnBindingSourceDataSourceChanged;
+ bindingSource.DataSourceChanged += OnBindingSourceDataSourceChanged;
+ }
+
private void BindIListSource (IListSource list) {
- BindIList(list.GetList());
+ BindIList (list.GetList());
}
private void BindIBindingList (IBindingList list) {
- BindIList(list);
+ BindIList (list);
+ list.ListChanged += OnListChanged;
}
private void BindIBindingListView (IBindingListView list) {
private void MoveCurrentCell (int x, int y, bool select, bool isControl, bool isShift, bool scroll)
{
+ if (!SetCurrentCellAddressCore (x, y, true, false, false))
+ return;
+
bool full_row_selected = Rows.SharedRow(CurrentCellAddress.Y).Selected;
bool full_col_selected = Columns[CurrentCellAddress.X].Selected;
else if (mode == DataGridViewSelectionMode.ColumnHeaderSelect)
mode = DataGridViewSelectionMode.CellSelect;
- // Move CurrentCell
- SetCurrentCellAddressCore (x, y, true, false, false);
-
// If the current cell isn't visible, scroll to it
if (scroll) {
int disp_x = ColumnIndexToDisplayIndex (x);
}
int disp_y = y;
+ int displayedRowsCount = DisplayedRowCount (false);
+ int delta_y = 0;
if (disp_y < first_row_index) {
- int delta_y = 0;
-
if (disp_y == 0)
delta_y = verticalScrollBar.Value;
else
verticalScrollBar.SafeValueSet (verticalScrollBar.Value - delta_y);
OnVScrollBarScroll (this, new ScrollEventArgs (ScrollEventType.ThumbPosition, verticalScrollBar.Value));
- } else if (disp_y > first_row_index + DisplayedRowCount (false) - 2) {
- int delta_y = 0;
-
+ } else if (disp_y > first_row_index + displayedRowsCount - 1) {
if (disp_y == Rows.Count - 1)
delta_y = verticalScrollBar.Maximum - verticalScrollBar.Value;
else
- for (int i = first_row_index + DisplayedRowCount (false) - 2; i < disp_y; i++)
+ for (int i = first_row_index + displayedRowsCount - 1; i < disp_y; i++)
delta_y += GetRowInternal (i).Height;
verticalScrollBar.SafeValueSet (verticalScrollBar.Value + delta_y);
{
switch (args.ListChangedType) {
case ListChangedType.ItemAdded:
- AddBoundRow ((sender as DataView)[args.NewIndex]);
+ AddBoundRow ((sender as IBindingList)[args.NewIndex]);
break;
case ListChangedType.ItemDeleted:
Rows.RemoveAt (args.NewIndex);
break;
+ default:
+ ClearBinding ();
+ DoBinding ();
+ break;
}
Invalidate ();
DoBinding ();
}
+ private void OnTableCleared (object sender, DataTableClearEventArgs e)
+ {
+ ClearBinding ();
+ DoBinding ();
+ }
+
+ private bool MouseOverColumnResize (int col, int mousex)
+ {
+ if (!allowUserToResizeColumns)
+ return false;
+
+ Rectangle col_bounds = GetCellDisplayRectangle (col, 0, false);
+
+ if (mousex >= col_bounds.Right - 4 && mousex <= col_bounds.Right)
+ return true;
+
+ return false;
+ }
+
+ private bool MouseOverRowResize (int row, int mousey)
+ {
+ if (!allowUserToResizeRows)
+ return false;
+
+ Rectangle row_bounds = GetCellDisplayRectangle (0, row, false);
+
+ if (mousey >= row_bounds.Bottom - 4 && mousey <= row_bounds.Bottom)
+ return true;
+
+ return false;
+ }
+
+ private void DrawVerticalResizeLine (int x)
+ {
+ Rectangle splitter = new Rectangle (x, Bounds.Y + 3 + (ColumnHeadersVisible ? ColumnHeadersHeight : 0), 1, Bounds.Height - 3 - (ColumnHeadersVisible ? ColumnHeadersHeight : 0));
+ XplatUI.DrawReversibleRectangle (Handle, splitter, 2);
+ }
+
+ private void DrawHorizontalResizeLine (int y)
+ {
+ Rectangle splitter = new Rectangle (Bounds.X + 3 + (RowHeadersVisible ? RowHeadersWidth : 0), y, Bounds.Width - 3 + (RowHeadersVisible ? RowHeadersWidth : 0), 1);
+ XplatUI.DrawReversibleRectangle (Handle, splitter, 2);
+ }
+
#region Stuff for ToolTips
private void MouseEnteredErrorIcon (DataGridViewCell item)
{
DataGridViewRow row1 = (DataGridViewRow)x;
DataGridViewRow row2 = (DataGridViewRow)y;
+ if (row1.Cells[column].ValueType == typeof (DateTime) && row2.Cells[column].ValueType == typeof (DateTime))
+ return DateTime.Compare ((DateTime)row1.Cells[column].valuex, (DateTime)row2.Cells[column].valuex) * direction;
+
object val1 = row1.Cells[column].FormattedValue;
object val2 = row2.Cells[column].FormattedValue;