DGV DragDrop - row disappearing - c#

I'm attempting to write some code to allow the users of my application to drag and drop rows in a DataGridView to reorder them. The problem is, the row that is being dragged disappears when it's dropped - so dragging and dropping has the effect of just removing that row. Here is my code:
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private void grdCons_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
DragDropEffects dropEffect = grdCons.DoDragDrop(grdCons.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
}
}
}
private void grdCons_MouseDown(object sender, MouseEventArgs e)
{
rowIndexFromMouseDown = grdCons.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 grdCons_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void grdCons_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = grdCons.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
grdCons.Rows.RemoveAt(rowIndexFromMouseDown);
grdCons.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rowToMove);
}
}
At a guess, the Insert on the DGV on the DragDrop event isn't working.

Here is a cleaned up version of your code that works:
public Form1()
{
InitializeComponent();
grdCons.Rows.Add(7);
for (int i = 0; i < grdCons.Rows.Count; i++)
{
grdCons.Rows[i].Cells[0].Value = i;
grdCons.Rows[i].Cells[1].Value = "Cell " + i;
}
grdCons.AllowDrop = true;
grdCons.AllowUserToAddRows = false;
grdCons.AllowUserToDeleteRows = false;
grdCons.MouseMove += new MouseEventHandler(grdCons_MouseMove);
grdCons.MouseDown += new MouseEventHandler(grdCons_MouseDown);
grdCons.DragOver += new DragEventHandler(grdCons_DragOver);
grdCons.DragDrop += new DragEventHandler(grdCons_DragDrop);
}
private int rowIndexFromMouseDown;
private void grdCons_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
grdCons.DoDragDrop(grdCons.Rows[rowIndexFromMouseDown], DragDropEffects.Move);
}
}
private void grdCons_MouseDown(object sender, MouseEventArgs e)
{
rowIndexFromMouseDown = grdCons.HitTest(e.X, e.Y).RowIndex;
}
private void grdCons_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void grdCons_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = grdCons.PointToClient(new Point(e.X, e.Y));
int targetIndex = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
DataGridViewRow rowToMove = e.Data.GetData(typeof(DataGridViewRow)) as DataGridViewRow;
grdCons.Rows.RemoveAt(rowIndexFromMouseDown);
grdCons.Rows.Insert(targetIndex, rowToMove);
}
}

The problem lies in grdCons_DragDrop(). Because you mentioned the DGV is bound to a DataTable calling grdCons.Rows.Insert(targetIndex, rowToMove) will trigger an InvalidOperationException. When a DGV is data-bound you need to manipulate the DataSource rather than the DGV. Here's the correct way to call grdCons_DragDrop().
private void grdCons_DragDrop(object sender, DragEventArgs e)
{
DataTable tbl = (DataTable)grdCons.DataSource;
Point clientPoint = grdCons.PointToClient(new Point(e.X, e.Y));
int targetIndex = grdCons.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
DataRow row = tbl.NewRow();
row.ItemArray = tbl.Rows[rowIndexFromMouseDown].ItemArray; //copy the elements
tbl.Rows.RemoveAt(rowIndexFromMouseDown);
tbl.Rows.Insert(targetIndex, rowToMove);
}
}

Related

C# Drag and Drop in Listview doesn't work with Groups

I have a Listview with Drag and Drop codes working well.
However, after i add groups to it, the Drag and Drop codes are run, but the outcome is not what the codes used to behave.
Dropped items were only added to the tail of its group.
Why? and how to get it work properly with groups?
The Drag and Drop procedures are given herebelow:
private void lvQ_DragDrop(object sender, DragEventArgs e)
{
int targetIndex = lvQ.InsertionMark.Index;
if (targetIndex == -1)
return;
if (lvQ.InsertionMark.AppearsAfterItem)
targetIndex++;
ListViewItem draggedItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem));
if (targetIndex == 0 && lvQ.Items.OfType<ListViewItem>().FirstOrDefault()?.SubItems[1].Text == "FF")
{
lvQ.InsertionMark.Index = -1;
}
else
{
lvQ.BeginUpdate();
lvQ.Items.Insert(targetIndex, (ListViewItem)draggedItem.Clone());
lvQ.Items.Remove(draggedItem);
lvQ.EndUpdate();
ShowTXFrameStr();
}
}
private void lvQ_DragEnter(object sender, DragEventArgs e)
{
e.Effect = e.AllowedEffect;
}
private void lvQ_DragLeave(object sender, EventArgs e)
{
lvQ.InsertionMark.Index = -1;
}
private void lvQ_DragOver(object sender, DragEventArgs e)
{
Point ptScreen = new(e.X, e.Y);
Point pt = lvQ.PointToClient(ptScreen);
int targetIndex = lvQ.InsertionMark.NearestIndex(pt);
if (targetIndex > -1)
{
Rectangle itemBounds = lvQ.GetItemRect(targetIndex);
lvQ.InsertionMark.AppearsAfterItem = pt.Y > itemBounds.Top + (itemBounds.Height / 2);
}
lvQ.InsertionMark.Index = targetIndex;
}
private void lvQ_ItemDrag(object sender, ItemDragEventArgs e)
{
lvQ.DoDragDrop(e.Item, DragDropEffects.Move);
}

Winforms datagridview : drag and drop causes an error

An exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll but was not handled in user code
Additional Information : Rows cannot be programmatically removed unless the DataGridView is data-bound to an IBindingList that supports change notification and allows delete
This is how I bind my data to datagridview :
IEnumerable<myTable> query = from p in db.myTables select p;
testList = query.ToList();
dataGridView1.DataSource = testList;
And this is what I use to drag & drop rows :
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (dataGridView1.SelectedRows.Count == 1)
{
if (e.Button == MouseButtons.Left)
{
rw = dataGridView1.SelectedRows[0];
rowIndexFromMouseDown = dataGridView1.SelectedRows[0].Index;
dataGridView1.DoDragDrop(rw, DragDropEffects.Move);
}
}
}
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
e.Effect = DragDropEffects.Move;
}
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
int rowIndexOfItemUnderMouseToDrop;
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
rowIndexOfItemUnderMouseToDrop = dataGridView1.HitTest(clientPoint.X, clientPoint.Y).RowIndex;
if (e.Effect == DragDropEffects.Move)
{
dataGridView1.Rows.RemoveAt(rowIndexFromMouseDown);
dataGridView1.Rows.Insert(rowIndexOfItemUnderMouseToDrop, rw);
}
}
Whenever I tried to drag & drop, dragging is ok but drop causes the error that I mentioned above, and if possible I dont want to use bindinglist because if I use it I will have to make lots of changes.
If you can help I would be greateful.
I fixed the problem. I've made changes on mouse events
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
DragDropEffects dropEffect = dataGridView1.DoDragDrop(dataGridView1.Rows[rowIndexFromMouseDown], DragDropEffects.Copy);
}
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
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_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
try
{
if (e.Data.GetDataPresent(typeof(DataGridViewRow)))
{
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
if (e.Effect == DragDropEffects.Copy)
{
DataGridViewRow Row = (DataGridViewRow)e.Data.GetData(typeof(DataGridViewRow));
dataGridView1.Rows.Add(Row.Cells[0].Value, Row.Cells[1].Value, Row.Cells[2].Value);
}
}
else
{
//Reflect the exception to screen
MessageBox.Show("Geen data! #01", "Error");
}
}
catch (Exception msg)
{
MessageBox.Show(msg.Message, "Error");
}
}

Unable to drag and drop text from DataGridView to TextBox C#

I have been using code that others online have supplied but for some reason it won't let me drag items from the datagridview to the textbox. I highlight a row in the dataGridView and try to drag it to the textbox but nothing happens. I have also enabled the drop property for the textBox but still no difference. Here's the code that I am using:
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
DataGridView.HitTestInfo info = dataGridView1.HitTest(e.X, e.Y);
if (info.RowIndex >= 0)
{
if (info.RowIndex >= 0 && info.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[info.RowIndex].Cells[info.ColumnIndex].Value;
if (text != null)
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
}
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
Here is a small sample that i have done to give you an idea on how to do this... works perfectly for me. I used WinForms here. If WPF, there may be some more events you will need to register to in order for the drag+drop to register...
Note that you will want to add more code here and there to perform what you really want to do when you drag an item from one control to the other.
public partial class Form1 : Form
{
private Rectangle dragBoxFromMouseDown;
private int rowIndexFromMouseDown;
private int rowIndexOfItemUnderMouseToDrop;
private DataGridViewRow draggedrow;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<StringValue> Items = new List<StringValue>() { new StringValue("1"), new StringValue("2"), new StringValue("3"), new StringValue("4"), new StringValue("5"), new StringValue("6") };
this.dataGridView1.DataSource = Items;
}
private void dataGridView1_MouseMove(object sender, MouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
// If the mouse moves outside the rectangle, start the drag.
if (dragBoxFromMouseDown != Rectangle.Empty &&
!dragBoxFromMouseDown.Contains(e.X, e.Y))
{
// Proceed with the drag and drop, passing in the list item.
DragDropEffects dropEffect = dataGridView1.DoDragDrop(
dataGridView1.Rows[rowIndexFromMouseDown],
DragDropEffects.Move);
}
}
}
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
// Get the index of the item the mouse is below.
rowIndexFromMouseDown = dataGridView1.HitTest(e.X, e.Y).RowIndex;
if (rowIndexFromMouseDown != -1)
{
// Remember the point where the mouse down occurred.
// The DragSize indicates the size that the mouse can move
// before a drag event should be started.
Size dragSize = SystemInformation.DragSize;
// Create a rectangle using the DragSize, with the mouse position being
// at the center of the rectangle.
dragBoxFromMouseDown = new Rectangle(new Point(e.X - (dragSize.Width / 2),
e.Y - (dragSize.Height / 2)),
dragSize);
this.draggedrow = this.dataGridView1.CurrentRow;
}
else
// Reset the rectangle if the mouse is not over an item in the ListBox.
dragBoxFromMouseDown = Rectangle.Empty;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Move;
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
this.textBox1.Text = (string)this.draggedrow.Cells["Value"].Value;
}
}
public class StringValue
{
public StringValue(string s)
{
_value = s;
}
public string Value { get { return _value; } set { _value = value; } }
string _value;
}
can't you use DataGridViewCellMouseEventArgs e instead of hittest for getting row index in dataGridView1_CellMouseDown. below is your code modified hope this helps
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.RowIndex >= 0)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
string text = (String)
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
if (text != null)
dataGridView1.DoDragDrop(text, DragDropEffects.Copy);
}
}
}
}
private void textBox1_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
{
textBox1.Text = (System.String)e.Data.GetData(typeof(System.String));
}
}
private void textBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}

Drag and drop in winforms

I need to drag 6 images, which are in PictureBoxes, to 6 answer panels, one at a time and if the correct image is dragged to the correct spot it adds 1 to the score which is in a class called 'score'. There is code below to show one of the pcitures working but the form lags alot and if the image is dragged anywhere above the panel, it will snap into place. is there a way to stop this?
namespace DragAndDropQuiz
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void pcBxLiverpool_MouseEnter(object sender, EventArgs e)
{
this.Cursor = Cursors.Hand;
}
private void pcBxLiverpool_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Default;
}
private int xPos;
private int yPos;
private void pcBxGeneral_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
xPos = e.X;
yPos = e.Y;
}
}
private void pcBxGeneral_MouseMove(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
((PictureBox)sender).Top += (e.Y - yPos);
((PictureBox)sender).Left += (e.X - xPos);
this.Refresh();
}
}
}
private void pcBxGeneral_MouseUp(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox answer = (PictureBox)sender;
if (answer.Location.X < pnlAnswer.Location.X)
{
if (answer.Location.X + answer.Width > pnlAnswer.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlAnswer.Location.X + pnlAnswer.Width)
{
answer.Location = pnlAnswer.Location;
}
}
}
else if (answer.Location.X > pnlAnswer.Location.X)
{
if (answer.Location.X < (pnlAnswer.Location.X + pnlAnswer.Width))
{
answer.Location = pnlAnswer.Location;
}
}
}
}
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
}
private void groupBox2_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawRectangle(new Pen(Color.Red), pnlAnswer.Location.X, pnlAnswer.Location.Y, pnlAnswer.Width, pnlAnswer.Height);
}
}
}

Drag Drop Between two Grid Views on multiple forms

So i have a main form then multiple windows with data grid views in.
I want to be able to swap data to and from the main window to the child windows and vice versa.
I have on my main form:
private Rectangle dragBoxFromMouseDown;
private object valueFromMouseDown;
private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
{
var hittestInfo = dataGridView1.HitTest(e.X, e.Y);
if (hittestInfo.RowIndex != -1 && hittestInfo.ColumnIndex != -1)
{
valueFromMouseDown = dataGridView1.Rows[hittestInfo.RowIndex];
if (valueFromMouseDown != null)
{
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_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
{
if (dragBoxFromMouseDown != Rectangle.Empty && !dragBoxFromMouseDown.Contains(e.X, e.Y))
{
DragDropEffects dropEffect = dataGridView1.DoDragDrop(valueFromMouseDown, DragDropEffects.Copy);
}
}
}
Then on my child forms:
private void dataGridView1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(typeof(System.String)))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void dataGridView1_DragOver(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void dataGridView1_DragDrop(object sender, DragEventArgs e)
{
Point clientPoint = dataGridView1.PointToClient(new Point(e.X, e.Y));
DataGridView.HitTestInfo hit = dataGridView1.HitTest(clientPoint.X, clientPoint.Y);
if (hit.RowIndex != -1)
{
dataGridView1.Rows.Insert(hit.RowIndex, e.Data.GetData(typeof(Objects.Amazon.PoDetail)));
}
else
{
dataGridView1.Rows.Add(e.Data.GetData(typeof(Objects.Amazon.PoDetail)));
}
}
Obviously, it fails at the e.Data.GetData as its getting the data from the current datagridview in the child form.
I can't think of a way of passing the data between the forms.
How that can be possible. dragdrop data is of same type you have passed in DoDragDrop method. thus it is of DataGridViewRow type. So that should be-
object dropData = e.Data.GetData(typeof(DataGridViewRow));

Categories

Resources