1 Replies - 4643 Views - Last Post: 05 November 2013 - 11:26 AM Rate Topic: -----

#1 optix212   User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

DataGridView Drag and Drop files

Posted 05 November 2013 - 10:46 AM

I want to be able to drag and drop a file onto my datagridview, determine the row that the user dragged the file onto, and then copy that file to a file path constructed from cells within that row.

For some reason it seems like my "var files" variable is not filling up with strings.

I know this because of the MessageBox.Show(files[0].ToString()); statement right under where my files variable is supposed to be filled.

Here is my code:
private void jobsDataGridView_DragDrop(object sender, DragEventArgs e)
        {
            MessageBox.Show("Dropped");
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                MessageBox.Show("Data present");
                DataGridViewRow row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
                string savePath = Properties.Settings.Default["RemoteServer"] + @"\Users\brandon miller\Jobs" + @"\" + row.Cells[1].Value.ToString() + " - " + row.Cells[2].Value.ToString() + " - " + row.Cells[0].Value.ToString() + @"\" + row.Cells[3].Value.ToString();
                var files = (string[])e.Data.GetData(DataFormats.FileDrop);
                MessageBox.Show(files[0].ToString());
                foreach (var filePath in files)
                {
                    string fileName = Path.GetFileName(filePath);
                    System.IO.File.Copy(filePath, savePath + @"\" + fileName, true);
                }
            }
        }



Is This A Good Question/Topic? 0
  • +

Replies To: DataGridView Drag and Drop files

#2 optix212   User is offline

  • D.I.C Addict
  • member icon

Reputation: 30
  • View blog
  • Posts: 540
  • Joined: 10-October 09

Re: DataGridView Drag and Drop files

Posted 05 November 2013 - 11:26 AM

I ended up figuring it out. I had to use coordinates of the mouse cursor position, which I did not originally want to do, but it works fine..

Here is the final code in case anyone needs it in the future:
private void jobsDataGridView_DragDrop(object sender, DragEventArgs e)
        {
            //Get point of mouse cursor
            Point cursorPosition = this.jobsDataGridView.PointToClient(Cursor.Position);
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                //Hit test at x and y position of mouse cursor.
                DataGridView.HitTestInfo info = jobsDataGridView.HitTest(cursorPosition.X, cursorPosition.Y);
                //Generate a row out of the hit test's RowIndex property.
                DataGridViewRow row = jobsDataGridView.Rows[info.RowIndex];
                //Generate a save path out of that row.
                string savePath = @"\\" + Properties.Settings.Default["RemoteServer"] + @"\Users\brandon miller\Jobs" + @"\" + row.Cells[1].Value.ToString() + " - " + row.Cells[2].Value.ToString() + " - " + row.Cells[0].Value.ToString() + @"\" + row.Cells[3].Value.ToString();
                //Create a string array of file paths for each file dropped.
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                //For each file path in our string array, copy to our server directory.
                foreach (string filePath in files)
                {
                    string fileName = Path.GetFileName(filePath);
                    System.IO.File.Copy(filePath, savePath + @"\" + fileName, true);
                }
                if (MessageBox.Show("File(s) successfully copied to: \n" + savePath.ToString() + "\n\nWould you like to open that directory now?", "Copy Complete", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    Process.Start("explorer.exe", savePath);
                }
            }
        }

        private void jobsDataGridView_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1