Category / Section
How to drag and drop the selected rows from grid into MultiColumnTreeView control in WinForms GridGroupingControl?
4 mins read
Drag and drop
To drag and drop the selected rows from WinForms GridGroupingControl into MultiColumnTreeView, the TableControlMouseDown event of GridGroupingControl and DragEnter and DragDrop events of MultiColumnTreeView control can be used.
To
copy the selected records
C#
//Event Subscription
this.gridGroupingControl1.TableControlMouseDown += gridGroupingControl1_TableControlMouseDown;
//Event Customization
void gridGroupingControl1_TableControlMouseDown(object sender,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.Copy);
//Adding the Selected records to the temp collection.
Record[] recordArray = new Record[this.gridGroupingControl1.Table.SelectedRecords.Count];
this.gridGroupingControl1.Table.SelectedRecords.CopyTo(recordArray, 0);
clonedRecords = recordArray.ToList();
if (ef == DragDropEffects.Move)
{
//need to delete selection if you want to support this.
}
}
}
}
}
VB
'Event Subscription
AddHandler Me.gridGroupingControl1.TableControlMouseDown, AddressOf gridGroupingControl1_TableControlMouseDown
'Event Customization
Private Sub gridGroupingControl1_TableControlMouseDown(ByVal sender As Object, ByVal e As GridTableControlMouseEventArgs)
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.Copy)
'Adding the Selected records to the temp collection.
Dim recordArray(Me.gridGroupingControl1.Table.SelectedRecords.Count - 1) As Record
Me.gridGroupingControl1.Table.SelectedRecords.CopyTo(recordArray, 0)
clonedRecords = recordArray.ToList()
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
To
perform the drag operation
C#
//Event Subscription
this.multiColumnTreeView1.DragDrop += multiColumnTreeView1_DragDrop;
//Event customization
void multiColumnTreeView1_DragDrop(object sender, DragEventArgs e)
{
SelectedRecordsCollection recs = e.Data.GetData(typeof(SelectedRecordsCollection)) as SelectedRecordsCollection;
if (recs != null)
{
TreeColumnAdvCollection collection = this.multiColumnTreeView1.Columns;
Syncfusion.Windows.Forms.Tools.MultiColumnTreeView.TreeNodeAdv[] item = new Syncfusion.Windows.Forms.Tools.MultiColumnTreeView.TreeNodeAdv[collection.Count];
TreeNodeAdvSubItem[] subItems =new TreeNodeAdvSubItem[collection.Count];
foreach (SelectedRecord record in recs)
{
Record rec = record.Record;
for (int i = 0; i < collection.Count; i++)
{
string name = collection[i].Text;
if (i == 0)
{
item[i] = new Syncfusion.Windows.Forms.Tools.MultiColumnTreeView.TreeNodeAdv();
item[i].Text = rec[name].ToString();
item[i].HelpText = rec[name].ToString();
}
else if (i > 0)
{
subItems[i] = new TreeNodeAdvSubItem();
subItems[i].Text = rec[name].ToString();
subItems[i].HelpText = rec[name].ToString();
item[0].SubItems.Add(subItems[i]);
}
}
this.multiColumnTreeView1.Nodes.Add(item[0]);
}
}
}
VB
'Event Subscription
AddHandler Me.multiColumnTreeView1.DragDrop, AddressOf multiColumnTreeView1_DragDrop
'Event customization
Private Sub multiColumnTreeView1_DragDrop(ByVal sender As Object, ByVal e As DragEventArgs)
Dim recs As SelectedRecordsCollection = TryCast(e.Data.GetData(GetType(SelectedRecordsCollection)),
If recs IsNot Nothing Then
Dim collection As TreeColumnAdvCollection = Me.multiColumnTreeView1.Columns
Dim item(collection.Count - 1) As Syncfusion.Windows.Forms.Tools.MultiColumnTreeView.TreeNodeAdv
Dim subItems(collection.Count - 1) As TreeNodeAdvSubItem
For Each record As SelectedRecord In recs
Dim rec As Record = record.Record
For i As Integer = 0 To collection.Count - 1
Dim name As String = collection(i).Text
If i = 0 Then
item(i) = New Syncfusion.Windows.Forms.Tools.MultiColumnTreeView.TreeNodeAdv()
item(i).Text = rec(name).ToString()
item(i).HelpText = rec(name).ToString()
ElseIf i > 0 Then
subItems(i) = New TreeNodeAdvSubItem()
subItems(i).Text = rec(name).ToString()
subItems(i).HelpText = rec(name).ToString()
item(0).SubItems.Add(subItems(i))
End If
Next i
Me.multiColumnTreeView1.Nodes.Add(item(0))
Next record
End If
End Sub
To copy the selected records
C#
//Event Subscription
this.multiColumnTreeView1.DragEnter += multiColumnTreeView1_DragEnter;
//Event Customization
void multiColumnTreeView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(SelectedRecordsCollection)))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
VB
'Event Subscription
AddHandler Me.multiColumnTreeView1.DragEnter, AddressOf multiColumnTreeView1_DragEnter
'Event Customization
Private Sub multiColumnTreeView1_DragEnter(ByVal sender As Object, ByVal e As DragEventArgs)
If e.Data.GetDataPresent(GetType(SelectedRecordsCollection)) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
The
screenshot below illustrates the drag and drop of the selected rows from the GridGroupingControl to the MulticolumnTreeview
Samples:
C#: Drag_Drop_CS
VB: Drag_Drop_VB