1つの列を持つバインドされたDataGridViewを作成しました。ファイルをDataGridViewにドラッグアンドドロップして、ファイル名(パスと名前)のみを列のセルに保存できるようにしたい。私は調査を行っており、その方法は、DragDropイベントとDragEnterイベントを使用して、いくつかの異なることを試しましたが、どれもうまくいきませんでした。誰かがこれを行う方法を知っていますか?
私のコード:
private void PlaylistDataGridDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
private void PlaylistDataGridDragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (var filePath in files)
{
var a = (DataRowView) PlaylistBindingSource.AddNew();
a.BeginEdit();
a[0] = filePath;
a.EndEdit();
}
}
}