Drag Drop Datagridview Cells

How would one go about dragging and dropping more than one cell into another part of the gridview?
Thanks
[109 byte] By [natetrost] at [2007-12-17]
# 1

Not sure I understand -- do you want to drag the content that is one cell into another cell?

-mark

DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Say I have 5 cells selected in a row, I then want to drag and drop them into the five cells to the right of the selected cells, something like excel can do. I already know how to do drag and drop, just not what data to store the cells as in the drag drop event. If I I itterate through the selected cells, the data is stored unpredictable because the first selected cell is based on the last cell that was selected, so putting it into a string makes it jumbled. Hope you follow what I mean. thanks
natetrost at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Assuming this is all in-process I would create an array of the cell's data. If you can assume that the data is all in one row then just enumerate the row's Cells collection and check for the Selected property. If the cell is selected add it to the array. Once all the selected cells data is in the array pass the array as your drag and drop data. You might want to create a class (maybe using a generic collection) vs. an array so you can query the data type in your drop event handler.


BTW, the DataGridView FAQ has a sample of drag and drop to reorder rows. Some of the code in there might be helpful to others who are trying to do some drag and drop stuff.

-mark

DataGridView Program Manager
Microsoft
This post is provided "as-is"

MarkRideout at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Mark, coul you please tell me how I can drag and drop only a cell from a Datagridview to another?. I have two datagridviews in a Windows Form.

Thanks
Angel.

AngelPalomino at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5

Angel, Let's try this:


Assume you have DataGridViewA (DGVA) and DataGridViewB (DGVB), to drag a cell or cells from DataGridViewA to DataGridViewB, I would do the following....

1. Create a mouse down event handler for DGVA
DataGridViewA.MouseDown += new MouseEventHandler(DataGridViewA_MouseDown);

2. Implement to the corresponding event handler
private void DataGridViewA_MouseDown(object sender, MouseEventArgs e)
{
/* Make sure it's an action of the right button because by default, a click of the left button will change the cell selection */
if (e.Button.Equals(MouseButtons.Right))
{
/* If there are selected cells, assume they are the cells to be copied and proceed with he DoDragDrop method */
if (DataGridViewA.SelectedRows.Count > 0)
{
DoDragDrop(DataGridViewA.SelectedCells, DragDropEffects.Copy | DragDropEffects.Move);
}
}
}

*** At this point, we've completed the basic implementation needed to carry out the tasks for dragging cells from DataGridViewA. You should be able to handle the dropping cells to DataGridViewB as you normally do.

Li-Jen


Li-Jen at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6
Li-Jen,

Regarding this comment:

/* Make sure it's an action of the right button because by default, a click of the left button will change the cell selection */

any way to override or "work around" that behavior? I would prefer my users use the primary mouse button for dragging.

theblueeyz at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7

Hi Li-Jen,

I realise that this is a very late response to your question, but I think I've managed to come up with a solution to this problem and I hope it's useful to you.

Within the MouseDown event of the DataGridView in question, you can perform a HitTest to determine if the user has clicked on a new cell and, if they have, you can then select the new cell and call the DoDragDrop event from there.

Example:

Code Snippet

private void dgvGrid1_MouseDown(object sender, MouseEventArgs e)

{

DataGridView.HitTestInfo htiInfo = this.dgvGrid1.HitTest(e.X, e.Y);

if (htiInfo.RowIndex == -1 || htiInfo.ColumnIndex == -1)

{

return;

}

this.dgvGrid1.CurrentCell = this.dgvGrid1.Rows[htiInfo.RowIndex].Cells[htiInfo.ColumnIndex];

this.dgvGrid1.DoDragDrop(/* your data here */, DragDropEffects.All);

}

Paul

PaulCurtis at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 8

Please suggest me with the code

How to handle the dropping cells to DataGridViewB

Tapan at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...