Home > Software engineering >  drag and drop works but also drops from and to same grid Winforms C# .NET Framework
drag and drop works but also drops from and to same grid Winforms C# .NET Framework

Time:10-01

I implemented drag and drop and all works fine except that i can drag from the grid and drop in the same grid which duplicates files. I have tried to look at types of controls or data source but can't seem to figure it out. I tried File.Exists(file) in an if statement but didn't work

What I have tried

            _attachmentGrid.DragEnter  = (s, e) =>
            {

                if (e.Data.GetDataPresent(DataFormats.FileDrop))
                {
                    

                    e.Effect = DragDropEffects.Copy;
                }
                else
                {
                    e.Effect = DragDropEffects.None;
                }
            };

            _attachmentGrid.DragDrop  = (s, e) =>
            {
                        
                //var newPath = Directory.GetCurrentDirectory();
  
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                if (files != null)
                {
                    foreach (string file in files)
                    {
                        string fileName = Path.GetFileName(file);
                      
                        AttachFile(file);

                    }
                }
            };

            _attachmentGrid.MouseDown  = (s, e) =>
            {                           
                    byte[] data = GetData(_selection.SingleSelection);
                    string fileName = _selection.SingleSelection.Entity.FileAttachment.AttachmentName;
                    var list = new StringCollection();

                    list = _manager.DragAttachment(data, fileName);
                
                    DataObject dataObject = new DataObject();
                    dataObject.SetFileDropList(list);
                    _attachmentGrid.DoDragDrop(dataObject, DragDropEffects.Move | DragDropEffects.Copy);                  
              
            };```

Thank you

CodePudding user response:

Ended up putting a tag in the mousedown event and checking if the tag is true in the drag drop event then not attach the files which solved my issue.

  • Related