How to drag and drop in the WinForms GridGroupingControl?
Drag and drop
To implement the Drag and Drop function in the WinForms Grid GroupingControl, you have to handle the Control.DragOver and Control.DragDrop events. In the following example, the TableControlMouseDown event is handled to start the dragdrop operation that occurs only when the mousedown is over a RecordRowHeaderCell. In the Control.DragDrop event, the selected records are added to the target Grid. With the cursor coordinates, the row and column of the target Grid can be found. By using the row and column, the celltype can be found. When the cell type is RecordFieldCell, then the records in the DataObject can be pasted.
Solution:
To drag and drop in the GridGroupingControl, set the AllowDrop property to true and handle DragDrop, DragOver, and TableControlMouseDown events. The following code example explains how the dragdrop operation works in the GridGroupingControl.
C#
//Sets the drop and selection properties. this.gridGroupingControl1.AllowDrop = true; this.gridGroupingControl1.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended; private void grid_TableControlMouseDown(object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlMouseEventArgs e) { GridGroupingControl grid = sender as GridGroupingControl; if(grid != null) { GridTableControl tableControl = grid.TableControl; Point pt = new Point(e.Inner.X, e.Inner.Y); int row, col; if(tableControl.PointToRowCol(pt, out row, out col)) { GridTableCellStyleInfo style = tableControl.Model[row, col]; //checks if the cell in which the MouseDown event is a RecordRowHeaderCell. //If yes, then begins the DragDrop operation. if((style.TableCellIdentity.TableCellType == GridTableCellType.RecordRowHeaderCell || style.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordRowHeaderCell) && style.TableCellIdentity.DisplayElement.ParentRecord != null && style.TableCellIdentity.DisplayElement.ParentRecord.IsSelected()) { DragDropEffects ef = tableControl.DoDragDrop(new DataObject(grid.Table.SelectedRecords), DragDropEffects.All); if(ef == DragDropEffects.Move) { //need to delete selection if you want to support this. } } } } } private void grid_DragDrop(object sender, DragEventArgs e) { //gets the current grid. GridGroupingControl grid = sender as GridGroupingControl; //checks if the data handled in this event is a SelectedRecordCollection. if(e.Data.GetDataPresent(typeof(SelectedRecordsCollection))) { SelectedRecordsCollection recs = e.Data.GetData(typeof(SelectedRecordsCollection)) as SelectedRecordsCollection; if(recs != null) { GridTableControl tableControl = grid.TableControl; // pt is used to get the coordinates where the data is dropped. Point pt = tableControl.PointToClient(new Point(e.X, e.Y)); int row, col; // using the point, the corresponding row and column can be got. if(tableControl.PointToRowCol(pt, out row, out col)) { GridTableCellStyleInfo style = tableControl.Model[row, col]; // using the row and column the celltype can be found and checked if it is a RecordFieldCell. if((style.TableCellIdentity.TableCellType == GridTableCellType.RecordFieldCell || style.TableCellIdentity.TableCellType == GridTableCellType.AlternateRecordFieldCell)) { //pasting the selected records in the target grid. int target = grid.Table.FilteredRecords.IndexOf(style.TableCellIdentity.DisplayElement.ParentRecord); foreach(SelectedRecord rec in recs) { DataRowView drv = rec.Record.GetData() as DataRowView; if(target > -1 && target < grid.Table.FilteredRecords.Count) { DataRowView targetDRV = grid.Table.FilteredRecords[target].GetData() as DataRowView;; targetDRV.BeginEdit(); for(col = 0; col < drv.Row.ItemArray.GetLength(0); col++) { if(col < targetDRV.Row.ItemArray.GetLength(0)) { targetDRV[col] = drv[col]; } } targetDRV.EndEdit(); targetDRV.Row.AcceptChanges(); target += 1; } } } //shows the effects during drag/drop. e.Effect = DragDropEffects.Copy;// tell source grid not to delete records } } } } private void grid_DragOver(object sender, DragEventArgs e) { // shows the dragdrop effects. if(e.Data.GetDataPresent(typeof(SelectedRecordsCollection))) { e.Effect = DragDropEffects.Copy; } }
VB
'Sets the drop and selection properties. Me.gridGroupingControl1.AllowDrop = True Me.gridGroupingControl1.TableOptions.ListBoxSelectionMode = SelectionMode.MultiExtended private void grid_TableControlMouseDown(Object sender, Syncfusion.Windows.Forms.Grid.Grouping.GridTableControlMouseEventArgs e) Dim grid As GridGroupingControl = TryCast(sender, GridGroupingControl) If grid IsNot Nothing Then Dim tableControl As GridTableControl = grid.TableControl Dim pt As New Point(e.Inner.X, e.Inner.Y) Dim row, col As Integer If tableControl.PointToRowCol(pt, row, col) Then Dim style As GridTableCellStyleInfo = tableControl.Model(row, col) 'checks if the cell in which the MouseDown event is a RecordRowHeaderCell. 'If yes, then begins the DragDrop operation. If(style.TableCellIdentity.TableCellType = GridTableCellType.RecordRowHeaderCell OrElse style.TableCellIdentity.TableCellType = GridTableCellType.AlternateRecordRowHeaderCell) AndAlso style.TableCellIdentity.DisplayElement.ParentRecord IsNot Nothing AndAlso style.TableCellIdentity.DisplayElement.ParentRecord.IsSelected() Then Dim ef As DragDropEffects = tableControl.DoDragDrop(New DataObject(grid.Table.SelectedRecords), DragDropEffects.All) If ef = DragDropEffects.Move Then 'need to delete selection if you want to support this. End If End If End If End If End Sub private void grid_DragDrop(Object sender, DragEventArgs e) 'gets the current grid. Dim grid As GridGroupingControl = TryCast(sender, GridGroupingControl) 'checks if the data handled in this event is a SelectedRecordCollection. If e.Data.GetDataPresent(GetType(SelectedRecordsCollection)) Then Dim recs As SelectedRecordsCollection = TryCast(e.Data.GetData(GetType(SelectedRecordsCollection)), SelectedRecordsCollection) If recs IsNot Nothing Then Dim tableControl As GridTableControl = grid.TableControl ' pt is used to get the coordinates where the data is dropped. Dim pt As Point = tableControl.PointToClient(New Point(e.X, e.Y)) Dim row, col As Integer ' using the point, the corresponding row and column can be got. If tableControl.PointToRowCol(pt, row, col) Then Dim style As GridTableCellStyleInfo = tableControl.Model(row, col) ' using the row and column the celltype can be found and checked if it is a RecordFieldCell. If(style.TableCellIdentity.TableCellType = GridTableCellType.RecordFieldCell OrElse style.TableCellIdentity.TableCellType = GridTableCellType.AlternateRecordFieldCell) Then 'pasts the selected records in the target grid. Dim target As Integer = grid.Table.FilteredRecords.IndexOf(style.TableCellIdentity.DisplayElement.ParentRecord) For Each rec As SelectedRecord In recs Dim drv As DataRowView = TryCast(rec.Record.GetData(), DataRowView) If target > -1 AndAlso target < grid.Table.FilteredRecords.Count Then Dim targetDRV As DataRowView = TryCast(grid.Table.FilteredRecords(target).GetData(), DataRowView) targetDRV.BeginEdit() For col = 0 To drv.Row.ItemArray.GetLength(0) - 1 If col < targetDRV.Row.ItemArray.GetLength(0) Then targetDRV(col) = drv(col) End If Next col targetDRV.EndEdit() targetDRV.Row.AcceptChanges() target += 1 End If Next rec End If 'shows the effects during drag/drop. e.Effect = DragDropEffects.Copy ' tell source grid not to delete records End If End If End If End Sub private void grid_DragOver(Object sender, DragEventArgs e) 'shows the dragdrop effects. If e.Data.GetDataPresent(GetType(SelectedRecordsCollection)) Then e.Effect = DragDropEffects.Copy End If End Sub
Samples: