I want to add drag and drop function to my app where Data Grid View is used to display file names. Datasource is a bindinglist. I made another bininglist to store dragged files, and I want to import this binding list to the datasource.
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
BindingList<Datei> droppedDateien = e.Data.GetData(DataFormats.FileDrop) as BindingList<Datei>;
if (droppedDateien.Count() > 0) //this block does not execute.
{
foreach (Datei datei in draggedDateienlist)
{
dateienList.Add(datei);
}
}
}
However, I checked nothing happens in DragDrop function. Why is that?
As I understand your code snippet, you are not doing internal drag & drop (
DoDragDrop), I think you want to drag external files from the File Explorer for example and drop them into the grid. Accordingly,You are doing wrong cast here:
e.Data.GetData(DataFormats.FileDrop)method returnsstring[]notBindingList<T>.So, cast to the right type, create new
Dateiobjects, and add them to thedateienList.Just in case, if you are doing internal drag & drop like moving rows from one grid to another: