Posted 15 May 2022, 7:43 pm EST
Hi,
is it possible with the C1DragDropManager beetween two DataGrid that I can insert somewhere the new row?
And also highlight the selected row (target)
Regards
Forums Home / ComponentOne / WPF Edition
Posted by: patrick.muellner on 15 May 2022, 7:43 pm EST
Posted 15 May 2022, 7:43 pm EST
Hi,
is it possible with the C1DragDropManager beetween two DataGrid that I can insert somewhere the new row?
And also highlight the selected row (target)
Regards
Posted 15 May 2022, 9:48 pm EST
Hi Patrick,
Yes you can implement this requirement using C1DragDropManager. I have attached a sample which implements the same using C1DragDropManager’s RegisterDropTarget/RegisterDragSource/UnRegisterDragSource methods and DragDrop event. (see DataGridDragDrop.zip)
Note: The attached sample implements one way drag drop i.e. from one DataGrid to other. You can use the same implementation for vice versa.
Best Regards,
Kartik
Posted 23 May 2022, 6:30 pm EST
Hi,
thanks for you answer.
But is it possible that I insert the row where I drag over?
And also the row is selected where I want to drop it?
Regards
Posted 23 May 2022, 7:32 pm EST
Additional - with DragDropManager possible that I can use this with more rows?
Posted 23 May 2022, 9:10 pm EST
Hi Patrick.
Thanks for reaching out to us again.
The sample provided in previous post is the example to drag a row and insert into another Datagrid. Could you please clarify your requirement. What you want to achieve? Row insertion while drag/drop solution is already provided with you.
In order to select a row where you want to drop a dragged row. You need to handle DragOver event as:(see code snippet)
private void _dragDropManager_DragOver(object source, DragDropEventArgs e)
{
var targetDataGrid = e.DropTarget as C1DataGrid;
var cell = targetDataGrid.GetCellFromPoint(Mouse.GetPosition(this));
if(cell != null)
{
targetDataGrid.Selection.Clear();
targetDataGrid.Selection.Add(new DataGridCellsRange(cell.Row));
}
}
private void Source_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
var cell = sourceDataGrid.GetCellFromPoint(e.GetPosition(null));
if (cell != null && cell.IsSelected)
{
draggedData.Clear();
foreach (User item in sourceDataGrid.SelectedItems)
{
draggedData.Add(item);
}
_dragDropManager.DoDragDrop(cell.Row.Presenter, e, DragDropEffect.Move);
e.Handled = true;
}
}
// On Drop
private void OnDragDrop(object source, DragDropEventArgs e)
{
var targetDataGrid = e.DropTarget as C1DataGrid;
var cell = targetDataGrid.GetCellFromPoint(Mouse.GetPosition(this));
if(cell!=null)
{
int index = cell.Row.Index;
foreach (User item in draggedData)
{
(sourceDataGrid.DataSourceView.SourceCollection as ObservableCollection<User>).Remove(item);
(targetDataGrid.DataSourceView.SourceCollection as ObservableCollection<User>).Insert(index, item);
index++;
}
targetDataGrid.Selection.BeginUpdate();
targetDataGrid.Selection.Clear();
targetDataGrid.Selection.Add(targetDataGrid.Rows[cell.Row.Index - draggedData.Count], targetDataGrid.Rows[cell.Row.Index - 1]);
targetDataGrid.Selection.EndUpdate();
draggedData.Clear();
}
}
Please refer the attached modified sample for the same : DataGridDragDrop_Mod.zip
Best Regards,
Nitin
Posted 11 November 2022, 1:01 am EST
Hi,
I have one additional question.
What I have many rows und want do drop them for example to the bottom of my DataGrid the Sroller doesn’t works.
So with many rows it is not possible? How can I solve this problem?
Regards Patrick
Posted 13 November 2022, 6:03 pm EST
Hi Patrick,
Apologize for the delay in response.
To scroll target DataGrid while dragging, you need to handle MouseMove event of the current window as:(see code sippet)
private void TargetGrid_MouseMove(object sender, MouseEventArgs e)
{
if (e.MouseDevice.LeftButton == MouseButtonState.Pressed)
{
var ht = targetDataGrid.GetCellFromPoint(e.GetPosition(this));
if (ht != null)
{
if (targetDataGrid.Viewport.LastVisibleRow == ht.Row.Index && targetDataGrid.Rows.Count - 1 > ht.Row.Index)
targetDataGrid.ScrollIntoView(ht.Row.Index + 1, ht.Column.Index);
if (targetDataGrid.Viewport.FirstVisibleRow == ht.Row.Index && ht.Row.Index > 0)
targetDataGrid.ScrollIntoView(ht.Row.Index - 1, ht.Column.Index);
}
}
}
Please refer the attached modified sample for the same : DataGridDragDrop_Mod2.zip
Best Regards,
Nitin