K
Kirill_1984
Guest
Hello everyone! I have made a drag and drop event to exchange items inside dataGridView1. The point is that I remove and add items inside dataGridView1 when exchanging them and everything just works fine. But I decided that I need to drag and drop items of the same kind from some other folders (at desktop) into the same dataGridView1 (not inside it). The problem is I have trouble with that as existing items are deleted when I drag and drop them from outside the form. I guess it's because it thinks I do the same drag and drop inside it so it does that unwanted event. My question is how I can define whether drag and drop is made inside or outside the control? Should I create any special flag which specifies I am outside the form? Any help will be appreciated.
My code is:
//A code to exhange items inside dataGridView1
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
}
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
else
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
//Here is what happens inside dataGridView1
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
if (rowIndexFromMouseDown >= 0 && rowIndexFromMouseDown < dataGridView1.RowCount)
{
string tmp = listBox2.Items[rowIndexFromMouseDown].ToString();
dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
if (rowToMove != null)
{
dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
}
listBox2.Items.RemoveAt(rowIndexFromMouseDown);
listBox2.Items.Insert(rowIndexOfItemUnderMouseToDrop, tmp);
}
}
//Here is what I want to add from outside
string[] droppedItems = (string[])e.Data.GetData(DataFormats.FileDrop);
if (droppedItems != null)
{
foreach (string file in droppedItems)
{
string fileName = Path.GetFileName(file);
string filePath = Path.GetFullPath(file);
MessageBox.Show(fileName + " | " + filePath); //Here I get what I want to add from outside
}
}
//End
indexDragAndDropInsert = rowIndexOfItemUnderMouseToDrop;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
isDGVItemDragAndDropped = true;
e.Effect = DragDropEffects.Move;
int mousepos = PointToClient(Cursor.Position).Y;
if (mousepos > (dataGridView1.Location.Y + (dataGridView1.Height * 0.95)))
{
if (dataGridView1.FirstDisplayedScrollingRowIndex < dataGridView1.RowCount - 1)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex + 1;
}
}
if (mousepos < (dataGridView1.Location.Y + (dataGridView1.Height * 0.05)))
{
if (dataGridView1.FirstDisplayedScrollingRowIndex > 0)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex - 1;
}
}
}
Continue reading...
My code is:
//A code to exhange items inside dataGridView1
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.All;
else
e.Effect = DragDropEffects.None;
}
}
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
Size dragSize = SystemInformation.DragSize;
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2), e.Y - (dragSize.Height / 2)), dragSize);
}
else
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
//Here is what happens inside dataGridView1
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
if (rowIndexFromMouseDown >= 0 && rowIndexFromMouseDown < dataGridView1.RowCount)
{
string tmp = listBox2.Items[rowIndexFromMouseDown].ToString();
dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
if (rowToMove != null)
{
dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
}
listBox2.Items.RemoveAt(rowIndexFromMouseDown);
listBox2.Items.Insert(rowIndexOfItemUnderMouseToDrop, tmp);
}
}
//Here is what I want to add from outside
string[] droppedItems = (string[])e.Data.GetData(DataFormats.FileDrop);
if (droppedItems != null)
{
foreach (string file in droppedItems)
{
string fileName = Path.GetFileName(file);
string filePath = Path.GetFullPath(file);
MessageBox.Show(fileName + " | " + filePath); //Here I get what I want to add from outside
}
}
//End
indexDragAndDropInsert = rowIndexOfItemUnderMouseToDrop;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
isDGVItemDragAndDropped = true;
e.Effect = DragDropEffects.Move;
int mousepos = PointToClient(Cursor.Position).Y;
if (mousepos > (dataGridView1.Location.Y + (dataGridView1.Height * 0.95)))
{
if (dataGridView1.FirstDisplayedScrollingRowIndex < dataGridView1.RowCount - 1)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex + 1;
}
}
if (mousepos < (dataGridView1.Location.Y + (dataGridView1.Height * 0.05)))
{
if (dataGridView1.FirstDisplayedScrollingRowIndex > 0)
{
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.FirstDisplayedScrollingRowIndex - 1;
}
}
}
Continue reading...