Example E2621
Visible to All Users

WinForms Data Grid - Copy a cell's value to other cells by dragging its bottom-right edge

This example demonstrates how to copy the focused cell's value to other cells using drag and drop (similar to the Microsoft Excel feature):

Files to Review

Documentation

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Form1.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; namespace WindowsApplication1 { public partial class Form1 : DevExpress.XtraEditors.XtraForm { public Form1() { InitializeComponent(); WindowState = FormWindowState.Maximized; gridView1.OptionsView.ColumnAutoWidth = false; gridView1.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDownFocused; gridView1.OptionsSelection.MultiSelect = true; gridView1.OptionsSelection.MultiSelectMode = GridMultiSelectMode.CellSelect; new DataSourceHelper(gridView1, 50, 50); SelectedCellsBorderHelper selectedCellsHelper = new SelectedCellsBorderHelper(gridView1); new DragCellsValuesHelper(selectedCellsHelper); } } }
Classes/DragCellsValuesHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.Utils.Paint; using DevExpress.Utils; namespace WindowsApplication1 { public class DragCellsValuesHelper { private SelectedCellsBorderHelper _Helper; public GridView View { get { return _Helper.GridView; } } private GridCell _SourceGridCell; public GridCell SourceGridCell { get { return _SourceGridCell; } set { _SourceGridCell = value; } } public DragCellsValuesHelper(SelectedCellsBorderHelper selectedCellsHelper) { _Helper = selectedCellsHelper; InitViewEvents(); } private void InitViewEvents() { View.MouseDown += new MouseEventHandler(View_MouseDown); View.MouseUp += new MouseEventHandler(View_MouseUp); View.ShowingEditor += new CancelEventHandler(View_ShowingEditor); View.SelectionChanged += new DevExpress.Data.SelectionChangedEventHandler(View_SelectionChanged); } void View_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e) { if (_Helper.IsCopyMode) UpdateHint(); } private void UpdateHint() { ToolTipController.DefaultController.HideHint(); string text = View.GetFocusedDisplayText(); if (string.IsNullOrEmpty(text)) return; ToolTipController.DefaultController.ShowHint(text); } void View_ShowingEditor(object sender, CancelEventArgs e) { e.Cancel = _Helper.IsCopyMode; } void View_MouseUp(object sender, MouseEventArgs e) { if (_Helper.IsCopyMode) OnCopyFinished(); _Helper.IsCopyMode = false; } private void OnCopyFinished() { ToolTipController.DefaultController.HideHint(); CopyCellsValues(); } private void CopyCellsValues() { object value = View.GetRowCellValue(SourceGridCell.RowHandle, SourceGridCell.Column); GridCell[] selectedCells = View.GetSelectedCells(); foreach (GridCell cell in selectedCells) { View.SetRowCellValue(cell.RowHandle, cell.Column, value); } } void View_MouseDown(object sender, MouseEventArgs e) { _Helper.IsCopyMode = _Helper.GetDragRect().Contains(e.Location); if (_Helper.IsCopyMode) OnStartCopy(); } private void OnStartCopy() { SourceGridCell = new GridCell(View.FocusedRowHandle, View.FocusedColumn); } } }
Classes/SelectedCellsBorderHelper.cs(vb)
C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using DevExpress.XtraGrid.Views.Grid; using DevExpress.XtraGrid.Views.Grid.ViewInfo; using DevExpress.XtraGrid.Columns; using DevExpress.XtraGrid; using DevExpress.XtraGrid.Views.Base; using DevExpress.Utils.Paint; namespace WindowsApplication1 { public class SelectedCellsBorderHelper { public SelectedCellsBorderHelper(GridView gridView) { _GridView = gridView; gridView.GridControl.PaintEx += GridControl_PaintEx; } private void GridControl_PaintEx(object sender, PaintExEventArgs e) { if(IsCopyMode) DrawCopyBorder(e); else DrawRegularBorder(e); } private bool _IsCopyMode; private GridView _GridView; public GridView GridView { get { return _GridView; } set { _GridView = value; } } private GridControl _GridControl; public GridControl GridControl { get { return _GridView.GridControl; } } public bool IsCopyMode { get { return _IsCopyMode; } set { _IsCopyMode = value; OnCopyModeChanged(); } } XPaint _paint = new XPaint(); private XPaint Paint { get { return _paint; } } private int _DragRectSize = 6; int lineWidth = 3; private Rectangle GetSelectionBounds() { int width = 0; int height = 0; Rectangle rTop = Rectangle.Empty; bool shouldReturn = false; GridView view = GridControl.FocusedView as GridView; GridViewInfo info = view.GetViewInfo() as GridViewInfo; GridCell[] gridCells = view.GetSelectedCells(); if(gridCells.Length == 0) { shouldReturn = true; return Rectangle.Empty; } Brush hb = Brushes.Black; List<GridCellInfo> visibleColl = new List<GridCellInfo>(); foreach(GridRowInfo row in info.RowsInfo) { if(row is GridGroupRowInfo) { continue; } GridCellInfoCollection coll = (row as GridDataRowInfo).Cells; foreach(GridCellInfo cell in coll) visibleColl.Add(cell); } List<GridCellInfo> collection = new List<GridCellInfo>(); foreach(GridCell cell in gridCells) foreach(GridCellInfo cellInfo in visibleColl) if(cellInfo.RowInfo != null && cellInfo.ColumnInfo != null) if(cell.RowHandle == cellInfo.RowHandle && cell.Column == cellInfo.Column) collection.Add(cellInfo); if(collection.Count == 0) { shouldReturn = true; return Rectangle.Empty; } rTop = GetCellRect(view, collection[0].RowHandle, collection[0].Column); Rectangle rBottom = GetCellRect(view, collection[collection.Count - 1].RowHandle, collection[collection.Count - 1].Column); if(rTop.Y > rBottom.Y) height = rTop.Y - rBottom.Bottom; else height = rBottom.Bottom - rTop.Y; if(rTop.X <= rBottom.X) width = rBottom.Right - rTop.X; else width = rTop.X - rBottom.Right; return new Rectangle(rTop.X, rTop.Y, width, height); } private void DrawCopyBorder(PaintExEventArgs e) { Rectangle rect = GetSelectionBounds(); Paint.DrawFocusRectangle(e.Cache.Graphics, rect, Color.Black, Color.White); } private void DrawRegularBorder(PaintExEventArgs e) { Rectangle rect = GetSelectionBounds(); e.Cache.DrawRectangle(e.Cache.GetPen(Color.Black, lineWidth), rect); if(GridView.GetSelectedCells().Length == 1) DrawDragImage(e, rect); } public Rectangle GetDragRect() { return GetDragRect(GetSelectionBounds()); } private Rectangle GetDragRect(Rectangle rect) { return new Rectangle(rect.Right - _DragRectSize, rect.Bottom - _DragRectSize, _DragRectSize, _DragRectSize); } private void DrawDragImage(PaintExEventArgs e, Rectangle rect) { e.Cache.FillRectangle(Brushes.Black, GetDragRect(rect)); } Rectangle GetCellRect(GridView view, int rowHandle, GridColumn column) { GridViewInfo info = (GridViewInfo)view.GetViewInfo(); GridCellInfo cell = info.GetGridCellInfo(rowHandle, column); if(cell != null) return cell.Bounds; return Rectangle.Empty; } private void OnCopyModeChanged() { GridView.InvalidateRow(GridView.FocusedRowHandle); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.