Hi all,
I am trying to drag and drop columns from grid1 to grid2 without any luck.
Found a example here on how to drag and drop rows :
http://www.telerik.com/help/winforms/gridview-rows-drag-and-drop.html
I searched for the same but then with columns instead of rows but i could not find any examples,so i am not sure if it even is possible to do it?
If it is possible does anyone know about a example or a tutorial on how to do this?
best regards,
H
9 Answers, 1 is accepted
Thank you for writing.
RadGridView handles the whole drag and operation by RadGridViewDragDropService. The PreviewDragOver event allows you to control on what targets the item being dragged can be dropped on. The PreviewDragDrop event allows you to get a handle on all the aspects of the drag and drop operation, the source (drag) grid, the destination (target) grid, as well as the item being dragged. This is where we will initiate the actual physical move of the dragged item(s) from one grid to the other. In order to implement drag and drop functionality of a column from one grid to another, you can use the approach below:
public
Form1()
{
InitializeComponent();
RadDragDropService svc =
this
.radGridView1.GridViewElement.GetService<RadDragDropService>();
svc.PreviewDragStart += svc_PreviewDragStart;
svc.PreviewDragDrop += svc_PreviewDragDrop;
svc.PreviewDragOver += svc_PreviewDragOver;
}
private
void
svc_PreviewDragStart(
object
sender, PreviewDragStartEventArgs e)
{
e.CanStart =
true
;
}
private
void
svc_PreviewDragOver(
object
sender, RadDragOverEventArgs e)
{
SnapshotDragItem draggedItem = e.DragInstance
as
SnapshotDragItem;
GridHeaderCellElement targetHeaderCell = e.HitTarget
as
GridHeaderCellElement;
if
(draggedItem !=
null
&& targetHeaderCell !=
null
)
{
GridHeaderCellElement sourceHeaderCell = draggedItem.Item
as
GridHeaderCellElement;
if
(sourceHeaderCell !=
null
&& targetHeaderCell.GridViewElement != sourceHeaderCell.GridViewElement)
{
e.CanDrop =
true
;
}
else
{
e.CanDrop =
false
;
}
}
}
private
void
svc_PreviewDragDrop(
object
sender, RadDropEventArgs e)
{
SnapshotDragItem draggedItem = e.DragInstance
as
SnapshotDragItem;
GridHeaderCellElement targetHeaderCell = e.HitTarget
as
GridHeaderCellElement;
if
(draggedItem ==
null
|| targetHeaderCell ==
null
)
{
return
;
}
GridHeaderCellElement sourceHeaderCell = draggedItem.Item
as
GridHeaderCellElement;
if
(sourceHeaderCell !=
null
)
{
e.Handled =
true
;
sourceHeaderCell.GridViewElement.Template.Columns.Remove(sourceHeaderCell.ColumnInfo.Name);
GridViewTemplate targetTemplate = targetHeaderCell.GridViewElement.Template;
targetTemplate.Columns.Insert(targetTemplate.Columns.IndexOf(targetHeaderCell.ColumnInfo.Name),
sourceHeaderCell.ColumnInfo
as
GridViewDataColumn );
}
}
Note that this is just an example and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best.
I hope this information helps. Should you have further questions I would be glad to help.
Dess
Telerik


I am trying to use this solution. The .Gif show exactly what i need to do.
The issue i got is that when i select a column i get a Red X mark on my cursor and won't let me move it.
is there a MasterGrid of some other radGridView Config im missing?

I partially found my problem but i don't have solution to it.
The issues i get is that my grid2 is totally empty. therefor i can't drop a column into it.
if i create an empty grid with grid2.ColumnCount = 10; grid2.RowCount = 10; it let me drag stuff properly into it.
is there a way to add column and rows size dynamically?
In the PreviewDragOver event handler, you can specify on which elements you can drop the row. In the example, you can drop on particular cells only. In your case you need to drop on an empty GridTableElement:
private
void
svc_PreviewDragOver(
object
sender, RadDragOverEventArgs e)
{
if
(e.HitTarget
is
GridTableElement)
{
e.CanDrop =
true
;
}
}
I hope this will be useful. Let me know if you have additional questions.
Regards,
Dimitar
Progress Telerik

Thank you!
I've got another question!
Grid2 got 3-10 column that was Generated programmaly and no Rows.
Let's say i'm trying to move The Data from a column "All rows" from Grid1 and drop the data into an pre-existing column in Grid2.
I'm experimenting with a for loop but don't have any luck with it yet.
Thank you for your support!
Thank you for writing back.
I would recommend you to have a look at the following help article: https://docs.telerik.com/devtools/winforms/gridview/rows/drag-and-drop
It demonstrates a sample approach how to drag a row from one grid to another. You have full control over the drag and drop process. Hence, it is up to you what row exactly will be added to the target grid. Have in mind that if the grid is in bound mode, it is necessary to insert the new record in the DataSource collection, not the Rows collection of the RadGridView.
If you are still experiencing any further difficulties, feel free to submit a support ticket where you can attach a sample project. Thus, our support staff will gladly assist you.
I hope this information helps.
Regards,
Dess
Progress Telerik

Thank you for the reply.
I've manage to perform what i tried to do with a simple for loop in the end.
private void svc_PreviewDragDrop(object sender, RadDropEventArgs e)
{
SnapshotDragItem draggedItem = e.DragInstance as SnapshotDragItem;
GridHeaderCellElement targetHeaderCell = e.HitTarget as GridHeaderCellElement;
if (draggedItem == null || targetHeaderCell == null)
{
return;
}
GridHeaderCellElement sourceHeaderCell = draggedItem.Item as GridHeaderCellElement;
if (sourceHeaderCell != null)
{
e.Handled = true;
for (int i = 0; i < gridSource.RowCount; i++)
{
gridTarget.Rows[i].Cells[targetHeaderCell.ColumnIndex].Value = gridSource.Rows[i].Cells[sourceHeaderCell.ColumnIndex].Value;
}
}
}
Thank you for writing back.
I am glad that the referred help article was useful for achieving your goal. Thank you for sharing the code snippet with the community.
Regards,
Dess
Progress Telerik