We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

GridControl drag-drop when multiple cells selected

I am using an unbound GridControl and a separate datatable containing data to populate and update the grid. The grid cell background and other style info are controlled by handling the GridPrepareViewStyleInfo event, and inspecting datatable contents corresponding to e.CollIndex and e.RowIndex. I want to allow users to drag and drop multiple cells at a time from one place to another within the grid. To keep the datatable in sync, this requires keeping track of which cells were originally selected, (when handling MouseDown) and then when the DragDrop event fires, move data from the original datatable cells to the new datatable cells corresponding to the drop point on the grid. This is straightforward by using Control.MousePosition and PointToRowCol() when only one cell is moved. But I cannot figure out how to detect a range of cells being moved. Any help is appreciated. --Van Baker

5 Replies

AD Administrator Syncfusion Team November 4, 2004 12:40 PM UTC

Are you actually selecting the cells first by mousing down and dragging the mouse over the range, and then releasing the mouse to select the cells. And then once the cells have been selected, you mousedown a second time to drag the drag the cells. Is this what you are doing? Normally, you can get the selected range using grid.Selections.Ranges.ActiveRange. But unless you are using the default Ole D&D support, when you mousedown the second time to start the drag, the selection is lost. One way around this would be to use the SelectionsChanging event to track a form member that always holds the last non-empty selected range.
private GridRangeInfo lastSelectedRange = null;
private void gridControl1_SelectionChanging(object sender, GridSelectionChangingEventArgs e)
{
	if(!e.Range.IsEmpty)
		this.lastSelectedRange = e.Range;
}


VB vbaker November 4, 2004 01:24 PM UTC

Yes, I am first selecting the cell(s); then, hover the mouse over the selection until the little dotted rectangle shows on the cursor, and then click and perform the drag operation. The AllowDrop property for the grid is set to true, so I assume this means ole drag-drop support is enabled. One thing I''ve noticed is that when multiple cells are dropped, calling e.Data.GetData() in the DragDrop event handler returns the cell text values of all the originally selected cells, separated by \r characters. At this point, would grid.Selections.Ranges.ActiveRange still return the originally selected range of cells? And is there a simple way to determine the target range of cells as well? Thanks, --Van Baker


AD Administrator Syncfusion Team November 4, 2004 02:58 PM UTC

In a non-virtual GridControl, you can get the information on what was dragged directly from the GridData object that is in the DataObject. Below is some code from a DragDrop event handler showing how to do this. As far as getting the drop cell, you can use grid.PointToRowCol. Here is code you could use in a DragDrop handler. Point clickPoint = gridControl1.PointToClient(new Point(e.X, e.Y)); int row, col; gridControl1.PointToRowCol(clickPoint, out row, out col); =============
private void gridControl1_DragDrop(object sender, DragEventArgs e)
{
	if(e.Data.GetDataPresent(typeof(GridData)))
	{
		GridData data = e.Data.GetData(typeof(GridData)) as GridData;
		for(int i = 0; i < data.RowCount; i++)
		{
			for(int j = 0; j < data.ColCount; ++j)
			{
				string s = "";
				if(data[i,j] != null)
				{
					GridStyleInfo style = new GridStyleInfo(data[i,j]);
					s = style.Text;
				}
				Console.Write(s + "\t");
			}
			Console.WriteLine();
		}
	}
}


VB vbaker November 4, 2004 05:33 PM UTC

Selections.Ranges.ActiveRange works fine for getting the source range of dragged cells. The problem is when the cells are dropped. When using e.X and e.Y to determine the row and column of the drop location, it tells you where the cell under the mouse cursor was dropped, but not where the range of cells was dropped. For instance, suppose I select three cells all in column 2 in rows 1, 2 and 3. Then, I click on the top cell of the three and drag the mouse to location (r5, c3). All three cells drop, but e.X and e.Y will tell me only the row and column where the _top_ cell was dropped (r5, c3). The grid range of the dropped cells is (r5, c3: r7, c3). Now, I repeat this, but using the bottom cell for dragging the three cells, and dropping when the mouse is at the same location (r5, c3) on the grid as before. This time, e.X and e.Y will indicate same drop point (r5, c3), which is where the _bottom_ cell was dropped. Even though the grid range where the three cells were dropped is different (r3, c3: r5, c3), the drop point is the same. So, how can I determine the actual target range where a set of cells is dropped? --Van Baker


AD Administrator Syncfusion Team November 4, 2004 05:56 PM UTC

Take a look at this object. It holds information used by the grid to handle the drop. Maybe it has exactly the value you need. this.grid.Model.DragDropData If not, here is how you can compute it. When the drag starts, get the mousedown cell, get the original selected range. From this information, you can get the row and col offsets of the top-left cell of the range from the mouse down cell. Then on the drop, get the cell of the drop, and then uses the above offset to compute the top-left cell of the drop range.

Loader.
Live Chat Icon For mobile
Up arrow icon