Category / Section
How to drag and drop operation by just clicking on the selection in WinForms GridControl?
4 mins read
Drag and drop operation
By default, the drag and drop are performed at the edges of the selected range, that is, rectangle. You can customize the drag and drop of selected ranges at any position of the rectangle.
Solution
To implement the customization, you have to handle the MouseDown, MouseUp and Selection changing events. For example, the following code samples have two grids. You can select a range of cells in the bottom grid and drag them to the upper grid by clicking the selection.
C#
//In gridDataBoundGrid1. //Enables dragdrop. this.gridDataBoundGrid1.AllowDrop=true; #region [gridDataBoundGrid1 Events] void gridDataBoundGrid1_MouseUp(object sender, MouseEventArgs e) { IgnoreClick1 = false; } void gridDataBoundGrid1_MouseMove(object sender, MouseEventArgs e) { //Check whether the left button is pressed or not. if (e.Button == MouseButtons.Left) { if (IgnoreClick1) { //Specify the coordinates. Point pt = new Point(e.X, e.Y); if (Math.Abs(e.X - Mousedownpt1.X) > SystemInformation.DragSize.Width / 2 || Math.Abs(e.Y - Mousedownpt1.Y) > SystemInformation.DragSize.Height / 2) { Console.WriteLine("start DragDrop"); //Rejects the changes made to the current cell. this.gridDataBoundGrid1.CurrentCell.Deactivate(false); GridRangeInfoList rangeList; //Retrieves the selected range. When the range is not selected, it return false. if (this.gridDataBoundGrid1.Selections.GetSelectedRanges(out rangeList, false)) { GridRangeInfo range = rangeList.ActiveRange; //Column and rows into cellranges by using ExpandRange. range = range.ExpandRange(1, 1, this.gridDataBoundGrid1.Model.RowCount, this.gridDataBoundGrid1.Model.ColCount); rangeList = new GridRangeInfoList(); rangeList.Add(range); int rowCount, colCount; string s; //Copy the selected data. this.gridDataBoundGrid1.Model.TextDataExchange.CopyTextToBuffer(out s, rangeList, out rowCount, out colCount); //Converts the selected data as an object for transfer. DataObject data = new DataObject(DataFormats.Text, s); //Enables all the DragDrop operations. this.gridDataBoundGrid1.DoDragDrop(data, DragDropEffects.All); } IgnoreClick1 = false; } } } } void Model_SelectionChanging(object sender, GridSelectionChangingEventArgs e) { GridRangeInfoList rangeList; //Retrieves the selected range. When the range is not selected, it return false. if (this.gridDataBoundGrid1.Selections.GetSelectedRanges(out rangeList, false)) { Mousedownpt1 = this.gridDataBoundGrid1.PointToClient(Control.MousePosition); int hitRow, hitCol; //Returns the row and column index of the specific cell. if (this.gridDataBoundGrid1.PointToRowCol(Mousedownpt1, out hitRow, out hitCol, -1)) { GridRangeInfo range = GridRangeInfo.Cell(hitRow, hitCol); // Checks whether the range list intersects with the range. if (rangeList.AnyRangeIntersects(range)) { e.Cancel = true; IgnoreClick1 = true; } } } else IgnoreClick1 = false; } #endregion //In gridControl1. //Enables dragdrop. this.gridControl1.AllowDrop=true; #region [gridControl1 Events] void gridControl1_MouseUp(object sender, MouseEventArgs e) { IgnoreClick = false;} void gridControl1_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { if (IgnoreClick) { //Specifies the coordinates. Point pt = new Point(e.X, e.Y); if (Math.Abs(e.X - Mousedownpt.X) > SystemInformation.DragSize.Width / 2 || Math.Abs(e.Y - Mousedownpt.Y) > SystemInformation.DragSize.Height / 2) { Console.WriteLine("start DragDrop"); //Rejects the changes made to the current cell. this.gridControl1.CurrentCell.Deactivate(false); GridRangeInfoList rangeList; //Retrieves the selected range. When the range is not selected, it return false. if (this.gridControl1.Selections.GetSelectedRanges(out rangeList, false)) { string s; int rowCount, colCount; //Copies the selected data. this.gridControl1.TextDataExchange.CopyTextToBuffer(out s, rangeList, out rowCount, out colCount); //Converts the selected data as an object for transfer. DataObject data = new DataObject(DataFormats.Text, s); //Enables all the DragDrop operations. this.gridControl1.DoDragDrop(data, DragDropEffects.All); } IgnoreClick = false; } } } } void gridControl1_SelectionChanging(object sender, Syncfusion.Windows.Forms.Grid.GridSelectionChangingEventArgs e) { GridRangeInfoList rangelist; //Retrieves the selected range. When the range is not selected, it return false. if (this.gridControl1.Selections.GetSelectedRanges(out rangelist, false)) { Mousedownpt = this.gridControl1.PointToClient(Control.MousePosition); int hitrow, hitcol; //Returns the row and column index of the specific cell. if (this.gridControl1.PointToRowCol(Mousedownpt, out hitrow, out hitcol, -1)) { //Gets the range from the specific cell. GridRangeInfo range = GridRangeInfo.Cell(hitrow, hitcol); // Checks whether the range list intersects with the range. if (rangelist.AnyRangeIntersects(range)) { e.Cancel = true; IgnoreClick = true; } } } else IgnoreClick = false; } #endregion
VB
'In gridDataBoundGrid1. 'Enables dragdrop. Me. gridDataBoundGrid1.AllowDrop = True #Region "[gridDataBoundGrid1 Events]" Private Sub gridDataBoundGrid1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) IgnoreClick1 = False End Sub Private Sub gridDataBoundGrid1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) 'Checks whether the left button is pressed or not. If e.Button = MouseButtons.Left Then If IgnoreClick1 Then 'Specifies the coordinates. Dim pt As New Point(e.X, e.Y) If Math.Abs(e.X - Mousedownpt1.X) > SystemInformation.DragSize.Width \ 2 OrElse Math.Abs(e.Y - Mousedownpt1.Y) > SystemInformation.DragSize.Height \ 2 Then Console.WriteLine("start DragDrop") 'Rejects the changes made to the current cell. Me.gridDataBoundGrid1.CurrentCell.Deactivate(False) Dim rangeList As GridRangeInfoList 'Retrieves the selected range. When the range is not selected, it return false. If Me.gridDataBoundGrid1.Selections.GetSelectedRanges(rangeList, False) Then Dim range As GridRangeInfo = rangeList.ActiveRange 'Column and rows into cellranges by using ExpandRange. range = range.ExpandRange(1, 1, Me.gridDataBoundGrid1.Model.RowCount, Me.gridDataBoundGrid1.Model.ColCount) rangeList = New GridRangeInfoList() rangeList.Add(range) Dim rowCount, colCount As Integer Dim s As String 'Copies the selected data. Me.gridDataBoundGrid1.Model.TextDataExchange.CopyTextToBuffer(s, rangeList, rowCount, colCount) 'Converts the selected data as an object for transfer. Dim data As New DataObject(DataFormats.Text, s) 'Enables all the DragDrop operations. Me.gridDataBoundGrid1.DoDragDrop(data, DragDropEffects.All) End If IgnoreClick1 = False End If End If End If End Sub Private Sub Model_SelectionChanging(ByVal sender As Object, ByVal e As GridSelectionChangingEventArgs) Dim rangeList As GridRangeInfoList 'Retrieves the selected range. When the range is not selected, it return false. If Me.gridDataBoundGrid1.Selections.GetSelectedRanges(rangeList, False) Then Mousedownpt1 = Me.gridDataBoundGrid1.PointToClient(Control.MousePosition) Dim hitRow, hitCol As Integer 'Returns the row and column index of the specific cel.l If Me.gridDataBoundGrid1.PointToRowCol(Mousedownpt1, hitRow, hitCol, -1) Then Dim range As GridRangeInfo = GridRangeInfo.Cell(hitRow, hitCol) 'Checks whether the range list intersects with the range. If rangeList.AnyRangeIntersects(range) Then e.Cancel = True IgnoreClick1 = True End If End If Else IgnoreClick1 = False End If End Sub #End Region 'In gridControl1 'Enables dragdrop. Me.gridControl1.AllowDrop = True #Region "[gridControl1 Events]" Private Sub gridControl1_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) IgnoreClick = False End Sub Private Sub gridControl1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If e.Button = MouseButtons.Left Then If IgnoreClick Then 'Specifies the coordinates. Dim pt As New Point(e.X, e.Y) If Math.Abs(e.X - Mousedownpt.X) > SystemInformation.DragSize.Width \ 2 OrElse Math.Abs(e.Y - Mousedownpt.Y) > SystemInformation.DragSize.Height \ 2 Then Console.WriteLine("start DragDrop") 'Rejects the changes made to the current cell. Me.gridControl1.CurrentCell.Deactivate(False) Dim rangeList As GridRangeInfoList 'Retrieves the selected range. If there is no selected range, it returns false. If Me.gridControl1.Selections.GetSelectedRanges(rangeList, False) Then Dim s As String Dim rowCount, colCount As Integer 'Copies the selected data. Me.gridControl1.TextDataExchange.CopyTextToBuffer(s, rangeList, rowCount, colCount) 'Converts the selected data as an object for transfer. Dim data As New DataObject(DataFormats.Text, s) 'Enables all the DragDrop operations. Me.gridControl1.DoDragDrop(data, DragDropEffects.All) End If IgnoreClick = False End If End If End If End Sub Private Sub gridControl1_SelectionChanging(ByVal sender As Object, ByVal e As Syncfusion.Windows.Forms.Grid.GridSelectionChangingEventArgs) Dim rangelist As GridRangeInfoList 'Retrieves the selected range. When the range is not selected, it return false. If Me.gridControl1.Selections.GetSelectedRanges(rangelist, False) Then Mousedownpt = Me.gridControl1.PointToClient(Control.MousePosition) Dim hitrow, hitcol As Integer 'Returns the row and column index of the specific cell. If Me.gridControl1.PointToRowCol(Mousedownpt, hitrow, hitcol, -1) Then 'Gets the range from the specific cell. Dim range As GridRangeInfo = GridRangeInfo.Cell(hitrow, hitcol) 'Checks whether the range list intersects with the range. If rangelist.AnyRangeIntersects(range) Then e.Cancel = True IgnoreClick = True End If End If Else IgnoreClick = False End If End Sub #End Region
Samples:
C#: Drag_and_drop_by_clicking_selection
VB: Drag_and_drop_by_clicking_selection