This is a migrated thread and some comments may be shown as answers.

ListView DragDrop with Groups

3 Answers 324 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Doug
Top achievements
Rank 1
Doug asked on 08 May 2013, 11:27 PM
I'm trying to set up a ListView with two groups that allows the user to:

1. Drag and Drop items within the first group only.  The user can sequence (non-group) items in that part of the list.  Only one item at a time should be dragged/dropped.  (The drag/drop example I found on the forum doesn't work well with groups.  It removes items.  But it works well if I eliminate my groups.)

2. Double click an item to move it from its current group to the other group.  I got this much code to work.  What I need to work out still is to prevent double-clicking on a group item from expanding/collapsing that group.

private void lvMain_DoubleClick(object sender, EventArgs e)
{
    if (lvMain.SelectedItem.GetType() == typeof(ListViewDataItem))
    {
        if (lvMain.SelectedItem.Group == lvMain.Groups[0])
        {
            lvMain.SelectedItem.Group = lvMain.Groups[1];
        }
        else
        {
            lvMain.SelectedItem.Group = lvMain.Groups[0];
        }
    }
}

Thank you,
Gary

3 Answers, 1 is accepted

Sort by
0
Peter
Telerik team
answered on 13 May 2013, 02:33 PM
Hi Gary,

Thank you for writing.

RadListView does not support drag drop in Grouping mode. In case that you can eliminate your group you can use the built-in drag-drop behavior (you have set AllowDragDrop = true), and you should utilize the events provided by the ListViewDragDropService:
this.radListView1.AllowDragDrop=true;
this.radListView1.AllowDrop =true;
this.radListView1.ListViewElement.DragDropService.PreviewDragDrop += DragDropService_PreviewDragDrop;
this.radListView1.ListViewElement.DragDropService.PreviewDragOver += DragDropService_PreviewDragOver;
 
 
void DragDropService_PreviewDragOver(object sender, RadDragOverEventArgs e)
{
    SimpleListViewVisualItem item  = e.HitTarget as SimpleListViewVisualItem;
    if( item.Text.Contains("One"))
    {
        e.CanDrop = false;//stop draging here
    }
}
 
void DragDropService_PreviewDragDrop(object sender, RadDropEventArgs e)
{
  if( e.HitTarget !=null)
      e.Handled = true;
}

Your approach about the DoubleClick event is correct. You should add only logic for checking the item under the mouse:
private void radListView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            RadElement elementUnderMouse = this.radListView1.ElementTree.GetElementAtPoint(e.Location);
            if (elementUnderMouse is SimpleListViewVisualItem)
            {
                      //your logic for the group change
            }
        }

I hope this helps.

Greetings,
Peter
the Telerik team
RadChart for WinForms is obsolete. Now what?
0
LE THANH
Top achievements
Rank 1
answered on 13 May 2013, 04:04 PM
I have RadListView with Group like image attach below. I want to drag file outside into item of RadListView. How to get info, data of that file and item which i have drop into ??? And I want to scroll RadListView to next items when mouse drag outside listview?
I have try many example on forum but i can not. Please help me, thanks!!!!
0
Peter
Telerik team
answered on 16 May 2013, 01:13 PM
Hello Le,

Thank you for writing.

I am not sure what is your implementation of this functionality, however, you can use the standard
OLE drag to achieve it. Please, refer to the sample code below demonstrating how to drag files from explorer to RadListView:
this.radListView1.AllowDragDrop = true;
this.radListView1.AllowDrop = true;          
this.radListView1.DragEnter += radListView1_DragEnter;
this.radListView1.DragDrop += radListView1_DragDrop;
this.radListView1.DragOver += radListView1_DragOver;
 
void radListView1_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
       e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;           
}
 
void radListView1_DragDrop(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
    {
        string[] filePaths = (string[])(e.Data.GetData(DataFormats.FileDrop));
        foreach (string fileLoc in filePaths)
        {                   
            if (File.Exists(fileLoc))
            {
                FileInfo info = new FileInfo(fileLoc);//Get the info from file
                MessageBox.Show(info.CreationTime.ToString() + " " + radListView1.SelectedItem.Text);
            }
        }
    }
}
 
void radListView1_DragOver(object sender, DragEventArgs e)
{  
    //get the list item under mouse
    BaseListViewVisualItem item = radListView1.ListViewElement.ElementTree.GetElementAtPoint( radListView1.ListViewElement.PointFromScreen( new Point(e.X, e.Y))) as BaseListViewVisualItem;
    if (item != null )
    {
        if (item is SimpleListViewVisualItem)
        {
            radListView1.SelectedItem = item.Data;//select the item under mouse
            radListView1.ListViewElement.EnsureItemVisible(item.Data);//scroll to the item
        }
        //20 is yours item height
        //these lines scroll the ListView if you hover last or first item
        if (item.ControlBoundingRectangle.Y >= radListView1.ListViewElement.ControlBoundingRectangle.Y - 20)
        {
            if( radListView1.SelectedIndex < radListView1.Items.Count)
                radListView1.ListViewElement.EnsureItemVisible(radListView1.Items[radListView1.SelectedIndex + 1]);                 
        }
        if (item.ControlBoundingRectangle.Y < 20)
        {
            if( radListView1.SelectedIndex > 0 )
                radListView1.ListViewElement.EnsureItemVisible(radListView1.Items[radListView1.SelectedIndex - 1]);
        }
    }
}

I hope this helps. 

Off topic, I would like to kindly ask you in future to separate the questions that are not related to each other in separate threads, so we can handle them accordingly. Thank you for the understanding. Greetings,
Peter
the Telerik team
RadChart for WinForms is obsolete. Now what?
Tags
ListView
Asked by
Doug
Top achievements
Rank 1
Answers by
Peter
Telerik team
LE THANH
Top achievements
Rank 1
Share this question
or