void FormulaSupportWithRangeSelectionsHelper_PreviewMouseMove(object sender, MouseEventArgs e) { if (inRangeSelection && usingMouse) { currentCell = grid.PointToCellRowColumnIndex(e); int top = 0, bottom = 0, left = 0, right = 0; grid.ScrollRows.GetVisibleSection(ScrollAxisRegion.Body, out top, out bottom); grid.ScrollColumns.GetVisibleSection(ScrollAxisRegion.Body, out left, out right); if (currentCell.RowIndex >= grid.ScrollRows.LastBodyVisibleLineIndex) { grid.ScrollRows.ScrollToNextLine(); } else if (currentCell.RowIndex <= top) { grid.ScrollRows.ScrollToPreviousLine(); } if (currentCell.ColumnIndex >= grid.ScrollColumns.LastBodyVisibleLineIndex) { grid.ScrollColumns.ScrollToNextLine(); } else if (currentCell.ColumnIndex <= left) { grid.ScrollColumns.ScrollToPreviousLine(); } if (currentCell.RowIndex > 0 && currentCell.ColumnIndex > 0 && (currentCell.ColumnIndex != lastCol || currentCell.RowIndex != lastRow)) { lastCol = currentCell.ColumnIndex; lastRow = currentCell.RowIndex; GridRangeInfo range = selectedRange; selectedRange = GridRangeInfo.Cell(startRow, startCol).UnionRange(GridRangeInfo.Cell(lastRow, lastCol)); PlaceTextInCell(false, selectedRange); grid.InvalidateCell(range.UnionRange(selectedRange)); e.Handled = true; } } }
private void ExpandSelection(Key key, bool isControlKey, bool isShiftKey) { GridRangeInfo range = selectedRange; int row = range.IsEmpty ? grid.CurrentCell.CellRowColumnIndex.RowIndex : range.Top; int col = range.IsEmpty ? grid.CurrentCell.CellRowColumnIndex.ColumnIndex : range.Left; if (range.Height <= 1 && range.Width <= 1) { startRow = row; startCol = col; lastRow = row; lastCol = col; } if (!isControlKey) { switch (key) { case Key.Down: lastRow++; break; case Key.Up: lastRow--; break; case Key.Left: lastCol--; break; case Key.Right: lastCol++; break; default: break; } } else { //// control key goes to end of row/col switch (key) { case Key.Down: lastRow = grid.Model.RowCount - 1; break; case Key.Up: lastRow = 1; break; case Key.Left: lastCol = 1; break; case Key.Right: lastCol = grid.Model.ColumnCount - 1; break; default: break; } } lastRow = Math.Min(Math.Max(1, lastRow), grid.Model.RowCount - 1); lastCol = Math.Min(Math.Max(1, lastCol), grid.Model.ColumnCount - 1); if (!isShiftKey) { startRow = lastRow; startCol = lastCol; } inRangeSelection = true; selectedRange = GridRangeInfo.Cell(startRow, startCol).UnionRange(GridRangeInfo.Cell(lastRow, lastCol)); PlaceTextInCell(false, selectedRange); // grid.InvalidateCells(); grid.InvalidateCell(selectedRange.UnionRange(range)); }
void FormulaSupportWithRangeSelectionsHelper_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (grid.IsMouseOver) { currentCell = grid.PointToCellRowColumnIndex(new MouseEventArgs(e.MouseDevice, e.Timestamp)); if (currentCell.RowIndex > 0 && currentCell.ColumnIndex > 0) { if (grid.CurrentCell.CellRowColumnIndex != currentCell) { if (grid.CurrentCell.IsEditing) { if (OkToPlaceText()) { startRow = currentCell.RowIndex; startCol = currentCell.ColumnIndex; inRangeSelection = true; usingMouse = true; GridRangeInfo range = selectedRange; selectedRange = GridRangeInfo.Cell(startRow, startCol); PlaceTextInCell(false, selectedRange); grid.InvalidateCell(selectedRange.UnionRange(range)); e.Handled = true; } else { try { if (grid.CurrentCell.Renderer.CellModel is GridCellFormulaModel) { GridFormulaEngine engine = ((GridCellFormulaModel)grid.CurrentCell.Renderer.CellModel).Engine; //check for missing right paren string s = grid.CurrentCell.Renderer.ControlText; int loc = s.LastIndexOf('('); if (loc > -1) { int loc1 = s.LastIndexOf(')'); if (loc1 < loc || loc1 == -1) { grid.CurrentCell.Renderer.ControlText = s + ")"; } } string message = "", val = "", parseString = ""; if (!engine.IsFormulaValid(grid.CurrentCell.Renderer.ControlText, out parseString, out message, out val)) { MessageBox.Show(message); e.Handled = true; } } } catch (Exception ex) { MessageBox.Show(ex.Message); e.Handled = true; } } } } else { currentCellModified = grid.CurrentCell.IsEditing; } } } }
public virtual void AddSelectedRanges(GridRangeInfo NewRange, RowColumnIndex Currentcell, MovingDirection Direction) { MoveDir = Direction; Gridmodel.Selections.Clear(); Gridmodel.Selections.Add(MouseDownRange.UnionRange(NewRange)); }
/// <summary> /// Set timer clok to notify the changes /// </summary> private void m_timer_Tick(object sender, EventArgs e) { timerCount++; GridRangeInfo gCells = GridRangeInfo.EmptyRange(); if (oldSize != m_syncGrid.ClientSize) { // Dispose graphics context if size was changed. if (g != null) { g.Dispose(); } g = null; oldSize = m_syncGrid.ClientSize; } if (drawDirectToDC && g == null) { g = m_syncGrid.CreateGridGraphics(); } try { for (int i = 0; i < m_numUpdatesPerTick; i++) { int recNum = rand.Next(m_set.Count - 1); int rowNum = recNum + 1; int col = rand.Next(18) + 1; int colNum = col + 1; object[] drow = (object[])m_set[recNum]; drow[col] = rand.Next(100); GridRangeInfo g1 = GridRangeInfo.Cell(rowNum, colNum); gCells = GridRangeInfo.UnionRange(gCells, g1); // Clear our volatile cache m_syncGrid.ResetVolatileData(); // Handle case when values is change for current cell. if (g1 == m_syncGrid.CurrentCell.RangeInfo) { m_syncGrid.CurrentCell.Model.ResetActiveText(rowNum, colNum); } else { // Draw direct to dc if (drawDirectToDC) { Rectangle bounds = m_syncGrid.RangeInfoToRectangle(g1); if (!bounds.IsEmpty) { // DrawClippedGrid method lets you simply draw the cells at the specified bounds directly to the graphics context. m_syncGrid.DrawClippedGrid(g, bounds); } } } } if (!drawDirectToDC) { m_syncGrid.ResetVolatileData(); m_syncGrid.ViewLayout.Lock(); // Prevent subsequent calls to ViewLayout.Reset method from clearing the layout information. Method was added in 3.1.0.x Rectangle bounds = m_syncGrid.RangeInfoToRectangle(gCells); if (!bounds.IsEmpty) { m_syncGrid.Invalidate(bounds); m_syncGrid.Update(); } m_syncGrid.ViewLayout.Unlock(); } // Insert or remove a row if (insertRemoveCount == 0) { return; } if (toggleInsertRemove > 0 && (timerCount % insertRemoveModulus) == 0) { icount = ++icount % (toggleInsertRemove * 2); shouldInsert = icount <= toggleInsertRemove; Console.WriteLine(m_syncGrid.RowCount); if (shouldInsert) { for (int ri = 0; ri < insertRemoveCount; ri++) { int recNum = rand.Next(m_set.Count - 1); int rowNum = recNum + 1; // Underlying data structure (this could be a datatable or whatever structure // you use behind a virtual grid). // // In this case it is a ArrayList int next = rand.Next(100); object[] row = new object[] { "Hello world", next + 1, next + 2, next + 3, next + 4, next + 5, next + 6, next + 7, next + 8, next + 9, next + 10, next + 11, next + 12, next + 13, next + 14, next + 15, next + 16, next + 17, next + 18, next + 19, next + 20 }; m_set.Insert(recNum, row); // Now that the change was done in the underlying datasource we need // to tell the grid to reflect those changes: m_syncGrid.Model.ResetVolatileData(); // Old way: if (!drawDirectToDC) { m_syncGrid.Rows.OnRangeInserting(new GridRangeInsertingEventArgs(rowNum, 1, null)); m_syncGrid.Rows.OnRangeInserted(new GridRangeInsertedEventArgs(rowNum, 1, null, true)); m_syncGrid.Update(); } else { // Optimized way: Directly calling ScrollWindows, don't let ScrollWindow call Invalidate. if (rowNum < m_syncGrid.TopRowIndex) { if (!allowChangeTopRowWhenInsertBeforeView || !m_syncGrid.ViewLayout.HasPartialVisibleRows) // checks whether the last row is shown and fully visible { // in this case we need to insert at top rowNum = m_syncGrid.TopRowIndex; } else { // If record is inserted before viewable area, simply increase // the top row so that the viewable area stays the same. No // drawing of the screen is needed. m_syncGrid.InternalSetTopRow(m_syncGrid.TopRowIndex + 1); m_syncGrid.UpdateScrollBars(); rowNum = -1; // mark it as handled. if (showNumberedRowHeaders) { // Repaint only row headers - they all change Rectangle rowHeaderBounds = m_syncGrid.RangeInfoToRectangle(GridRangeInfo.Col(0)); m_syncGrid.DrawClippedGrid(g, rowHeaderBounds); } } } if (rowNum != -1) { GridRangeInfo rg = GridRangeInfo.Row(rowNum); Rectangle bounds = m_syncGrid.RangeInfoToRectangle(rg); if (!bounds.IsEmpty) { if (showNumberedRowHeaders) { // don't scroll row header area - leave it as is (only works if all rows have same height ...) bounds.X += m_syncGrid.ColWidths[0]; if (!m_syncGrid.ViewLayout.HasPartialVisibleRows) { // Repaint row headers below last row Rectangle rowHeaderBounds = m_syncGrid.RangeInfoToRectangle(GridRangeInfo.Cells(m_syncGrid.RowCount, 0, m_syncGrid.RowCount, 0)); m_syncGrid.DrawClippedGrid(g, rowHeaderBounds); } } // If rows can have different heights: // m_syncGrid.ViewLayout.Reset(); Rectangle scrollBounds = new Rectangle(bounds.Left, bounds.Top, bounds.Right, m_syncGrid.ClientRectangle.Bottom); m_syncGrid.ScrollWindow(0, bounds.Height, scrollBounds, scrollBounds, false); // don't cause Invalidate being called. m_syncGrid.Model.ResetVolatileData(); // System.Threading.Thread.Sleep(1000); // DrawClippedGrid method lets you simply draw the cells at the specified bounds directly to the graphics context. m_syncGrid.DrawClippedGrid(g, bounds); } m_syncGrid.UpdateScrollBars(); } } } } else { for (int ri = 0; ri < insertRemoveCount; ri++) { int recNum = 5; //rand.Next(m_set.Count - 1); int rowNum = recNum + 1; // Underlying data structure (this could be a datatable or whatever structure // you use behind a virtual grid). // // In this case it is a ArrayList m_set.RemoveAt(recNum); // Now that the change was done in the underlying datasource we need // to tell the grid to reflect those changes: // Old way: if (!drawDirectToDC) { m_syncGrid.Rows.OnRangeRemoving(new GridRangeRemovingEventArgs(rowNum, rowNum + 1)); GridModelInsertRangeOptions iro = new GridModelInsertRangeOptions(); iro.RowColSizes = new int[m_syncGrid.Rows.DefaultSize]; m_syncGrid.Rows.OnRangeRemoved(new GridRangeRemovedEventArgs(rowNum, rowNum + 1, iro, true)); m_syncGrid.Update(); } else { // Optimized way: Directly calling ScrollWindows, don't let ScrollWindow call Invalidate. if (rowNum < m_syncGrid.TopRowIndex) { if (!allowChangeTopRowWhenInsertBeforeView || !m_syncGrid.ViewLayout.HasPartialVisibleRows) // checks whether the last row is shown and fully visible { // in this case we need to insert at top rowNum = m_syncGrid.TopRowIndex; } else { // If record is inserted before viewable area, simply increase // the top row so that the viewable area stays the same. No // drawing of the screen is needed. m_syncGrid.InternalSetTopRow(m_syncGrid.TopRowIndex - 1); m_syncGrid.UpdateScrollBars(); if (showNumberedRowHeaders) { // Repaint only row headers - they all change Rectangle rowHeaderBounds = m_syncGrid.RangeInfoToRectangle(GridRangeInfo.Col(0)); m_syncGrid.DrawClippedGrid(g, rowHeaderBounds); } rowNum = -1; // mark it as handled. } } if (rowNum != -1) { GridRangeInfo rg = GridRangeInfo.Row(rowNum); Rectangle bounds = m_syncGrid.RangeInfoToRectangle(rg); if (!bounds.IsEmpty) { if (showNumberedRowHeaders) { // don't scroll row header area - leave it as is (only works if all rows have same height ...) bounds.X += m_syncGrid.ColWidths[0]; } // If rows can have different heights: // m_syncGrid.ViewLayout.Reset(); Rectangle scrollBounds = new Rectangle(bounds.Left, bounds.Top, bounds.Right, m_syncGrid.ClientRectangle.Bottom); Rectangle scrolledInViewBounds = m_syncGrid.ScrollWindow(0, -bounds.Height, scrollBounds, scrollBounds, false); // don't cause Invalidate being called. m_syncGrid.Model.ResetVolatileData(); // DrawClippedGrid method lets you simply draw the cells at the specified bounds directly to the graphics context. if (!m_syncGrid.ViewLayout.HasPartialVisibleRows) { scrolledInViewBounds = new Rectangle(0, m_syncGrid.ViewLayout.Corner.Y, bounds.Right, bounds.Height); } m_syncGrid.DrawClippedGrid(g, scrolledInViewBounds); } m_syncGrid.UpdateScrollBars(); } } } } } } finally { } }
public void MouseMove(MouseControllerEventArgs e) { RowColumnIndex cell = Grid.PointToCellRowColumnIndex(e.Location); if (isMouseDown && cell.RowIndex != 0 && cell.ColumnIndex != 0) { if (cell.RowIndex < Grid.TopRowIndex && Grid.TopRowIndex > Grid.Model.FrozenRows) { Grid.TopRowIndex = Grid.TopRowIndex - 1; } else if (cell.ColumnIndex < Grid.LeftColumnIndex && Grid.LeftColumnIndex > Grid.Model.FrozenColumns) { Grid.LeftColumnIndex = Grid.LeftColumnIndex - 1; } Rect r = Grid.CellSpanToRect(ScrollAxisRegion.Body, ScrollAxisRegion.Body, new CellSpanInfo(cell.RowIndex, cell.ColumnIndex, cell.RowIndex, cell.ColumnIndex)); if (flag == 0) { top = mouseDownRange.Top; bottom = mouseDownRange.Bottom; left = mouseDownRange.Left; right = mouseDownRange.Right; // MessageBox.Show(mouseDownRange.Left.ToString()); flag = 1; } if (mouseDownRange.Top == top && mouseDownRange.Bottom == bottom && mouseDownRange.Right == right && mouseDownRange.Left == left && flag == 1) { moveDir = MovingDirection.None; //flag = 0; } if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Down) && (cell.RowIndex > mouseDownRange.Bottom || flag == 1) && cell.RowIndex > mouseDownRange.Bottom && e.Location.Y > r.Top + r.Height / 2) { moveDir = MovingDirection.Down; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, mouseDownRange.Right))); Grid.ScrollInView(new RowColumnIndex(Math.Min(Grid.Model.RowCount, cell.RowIndex), cell.ColumnIndex)); } else if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Up) && cell.RowIndex < mouseDownRange.Top && e.Location.Y < r.Top - r.Height / 2) { moveDir = MovingDirection.Up; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, mouseDownRange.Right))); Grid.ScrollInView(new RowColumnIndex(Math.Max(Grid.Model.FrozenRows + 1, cell.RowIndex - 1), cell.ColumnIndex)); } else if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Right) && cell.ColumnIndex > mouseDownRange.Right && e.Location.X > r.Left + r.Width / 2) { if (cell.RowIndex >= mouseDownRange.Top && cell.RowIndex <= mouseDownRange.Bottom) { moveDir = MovingDirection.Right; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, cell.ColumnIndex))); Grid.ScrollInView(new RowColumnIndex(cell.RowIndex, Math.Min(Grid.Model.ColumnCount, cell.ColumnIndex + 1))); } } else if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Left) && cell.ColumnIndex < mouseDownRange.Left && e.Location.X < r.Left - r.Width / 2) { moveDir = MovingDirection.Left; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(mouseDownRange.Top, cell.ColumnIndex))); Grid.ScrollInView(new RowColumnIndex(cell.RowIndex, Math.Max(Grid.Model.FrozenColumns + 1, cell.ColumnIndex - 1))); } if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Down) && cell.RowIndex > mouseDownRange.Bottom && e.Location.Y > r.Bottom + r.Height / 2) { moveDir = MovingDirection.Down; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, mouseDownRange.Right))); Grid.ScrollInView(new RowColumnIndex(Math.Min(Grid.Model.RowCount, cell.RowIndex + 1), cell.ColumnIndex)); } else if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Up) && cell.RowIndex < mouseDownRange.Top && e.Location.Y < r.Bottom - r.Height / 2) { moveDir = MovingDirection.Up; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, mouseDownRange.Right))); Grid.ScrollInView(new RowColumnIndex(Math.Max(Grid.Model.FrozenRows + 1, cell.RowIndex - 1), cell.ColumnIndex)); } else if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Right) && cell.ColumnIndex > mouseDownRange.Right && e.Location.X > r.Right + r.Width / 2) { moveDir = MovingDirection.Right; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, cell.ColumnIndex))); Grid.ScrollInView(new RowColumnIndex(cell.RowIndex, Math.Min(Grid.Model.ColumnCount, cell.ColumnIndex + 1))); } else if ((moveDir == MovingDirection.None || moveDir == MovingDirection.Left) && cell.ColumnIndex < mouseDownRange.Left && e.Location.X < r.Right - r.Width / 2) { if (cell.RowIndex >= mouseDownRange.Top && cell.RowIndex <= mouseDownRange.Bottom) { moveDir = MovingDirection.Left; Grid.Model.Selections.Clear(); Grid.Model.Selections.Add(mouseDownRange.UnionRange(GridRangeInfo.Cell(cell.RowIndex, cell.ColumnIndex))); Grid.ScrollInView(new RowColumnIndex(cell.RowIndex, Math.Max(Grid.Model.FrozenColumns + 1, cell.ColumnIndex - 1))); } } } }