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

Restrict selection to single cell

How can I prevent the selection of multiple cells when the mouse is dragged through a group of cells? What I would really like to do is have the drag move the cell without having to hover the cursor on the bottom edge of it to get the move cursor. The application is a production schedule, the contents of the cells are not going to be editted, just moved around within the grid. TIA, Dave Erwin

1 Reply

AD Administrator Syncfusion Team August 12, 2003 05:48 PM UTC

You can handle the grid.Model.SelectionsChanging event and set e.Cancel = true if e.Range is not empty and has more than 1 cell in it. But you may not need this. Below is some code that you can add to Syncfusion\Essential Suite\Grid\Samples\DataBound\GridDataBoundImageCell sample to enable dragging a picture in sample just by mousing down and dragging it (without moving the mouse to the edge of the selection). Maybe you can use something similar to start a drag of one of your cells without relying on the built-in D&D support that requires selecting cells and then starting drag from an edge.
//in formload, subscribe to the events
this.gridDataBoundGrid1.MouseDown += new MouseEventHandler(grid_MouseDown);
this.gridDataBoundGrid1.MouseMove += new MouseEventHandler(grid_MouseMove);
this.gridDataBoundGrid1.MouseUp += new MouseEventHandler(grid_MouseUp);

//the handlers
private Point mouseDownPoint = Point.Empty;
private int mouseDownRow = -1;
private int mouseDownCol = -1;

private void grid_MouseDown(object sender, MouseEventArgs e)
{
	Point pt = new Point(e.X, e.Y);
	int rowIndex, colIndex;
	if(this.gridDataBoundGrid1.PointToRowCol(pt, out rowIndex, out colIndex, -1))
	{
		//check for photo column mousedown
		if(this.gridDataBoundGrid1[0, colIndex].Text == "Photo"
			&& rowIndex > 0)
		{
			mouseDownPoint = new Point(e.X, e.Y);
			mouseDownRow = rowIndex;
			mouseDownCol = colIndex;
		}
		else
		{
			mouseDownPoint = Point.Empty;
			mouseDownRow = -1;
			mouseDownCol = -1;
		}
	}
}

private void grid_MouseMove(object sender, MouseEventArgs e)
{
	//check if dragging...
	if(mouseDownPoint != Point.Empty)
	{
		if(Math.Abs(mouseDownPoint.X - e.X)
			+ Math.Abs(mouseDownPoint.Y - e.Y) > 8)
		{
			//get the bitmap however???
			Byte[] pict = this.gridDataBoundGrid1[mouseDownRow, mouseDownCol].CellValue as Byte[];
			if(pict != null)
			{
				int PictOffSet = 78;
				MemoryStream buffer = new MemoryStream(pict, PictOffSet, pict.Length - PictOffSet);
				Bitmap image =   (Bitmap)Bitmap.FromStream(buffer, true);

				this.DoDragDrop(image, DragDropEffects.All);
			}
			mouseDownPoint = Point.Empty;
			mouseDownRow = -1;
			mouseDownCol = -1;
		}
	}
}

private void grid_MouseUp(object sender, MouseEventArgs e)
{
	mouseDownPoint = Point.Empty;
	mouseDownRow = -1;
	mouseDownCol = -1;
}

Loader.
Live Chat Icon For mobile
Up arrow icon