Drag Drop Datagridview Cells
Thanks
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"
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"
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.
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
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.
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:
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