Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Add drag 'n drop item to ListView group

Status
Not open for further replies.

adrianjohnson

Programmer
May 16, 2002
145
GB
Hi all,

I've implemented a drag and drop facility on a ListView (WinForms) application. The drag and drop itself works fine, as I write to the console the name of the file to be dropped.

However, my list view shows it's items in detail view (just 2 columns) and they're all bunched together in groups. I can get the name of the group, but cannot add the new drag-drop item into the group. Any ideas?

Here is some of the code:

Code:
        private void listMain_DragEnter(object sender, DragEventArgs e)
        {
            // Drag-drop operation for list view.

            // User is dragging a file into the tree view.
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        

        private void listMain_DragDrop(object sender, DragEventArgs e)
        {
            // Drag-drop operation for list view.

            // Get the name of the file being dropped in.
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            FileInfo newFile;
            string fileName = "";

            for (int i = 0; i < s.Length; i++)
            {
                newFile = new FileInfo(s[i]);
                fileName = newFile.ToString();
            }

            Console.WriteLine(fileName.ToString());


            if (currentGroup != "")
            {
                // Add the file to the list view.
                int startIndex = fileName.LastIndexOf(@"\");
                string myFile = fileName.Substring(startIndex + 1); ;

                ListViewItem listItem = new ListViewItem(myFile, currentGroup);
                listItem.Tag = fileName.ToString();
                listItem.SubItems.Add(File.GetLastWriteTime(fileName).ToString());
                listMain.Items.Add(listItem);
            }
        }

Many thanks,

Adrian Johnson

Adrian Johnson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top