Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
4555
Drag and Drop a Single Cell
posted

 This article demonstrates how to allow cells to be dragged to different locations in an UltraGrid. It is based on the knowledge base article
 KB010086 for dragging and dropping rows in a WinGrid and it is written in C#.

Step-By-Step Example

1) The first step is to set the UltraGrid’s AllowDrop property to True., the SelectCellType property to SingleAutoDrag and the CellClickAction to CellSelect:

     ultraGrid1.AllowDrop = true;

      e.Layout.Override.SelectTypeCell = SelectType.SingleAutoDrag;

    e.Layout.Override.CellClickAction = CellClickAction.CellSelect;

 

 

2) Next, handle the UltraGrid’s SelectionDrag event and call the UltraGrid’s DoDragDrop() method. The SelectionDrag event fires when the end-user clicks and starts to drag a selected cell.

    private void ultraGrid1_SelectionDrag(object sender, CancelEventArgs e)
        {
            ultraGrid1.DoDragDrop(ultraGrid1.Selected.Cells, DragDropEffects.Copy);
        }

 

3)  Now handle the UltraGrid’s DragOver event to set the drag effects    

private void ultraGrid1_DragOver(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Copy;
        } 

 

 

4) Finally, handle the UltraGrid’s DragDrop event and get the position on the grid where the cell is to be dropped. Once we have that location, we can move the cell to it.  

 

private void ultraGrid1_DragDrop(object sender, DragEventArgs e)
        {
            int dropIndexRow, dropIndexCol;

            // Get the position on the grid where the dragged CELL is to be dropped.
            //get the grid coordinates of the row and columns(the drop zone)
            UIElement uieOver = ultraGrid1.DisplayLayout.UIElement.ElementFromPoint(ultraGrid1.PointToClient(new Point(e.X, e.Y)));

            //get the cell that is the drop zone/or where the dragged cell is to be dropped
            UltraGridRow ugrOver = uieOver.GetContext(typeof(UltraGridRow), true) as UltraGridRow;
            UltraGridColumn ugrOverCol = uieOver.GetContext(typeof(UltraGridColumn), true) as UltraGridColumn;

            if (ugrOver != null)
            {
                dropIndexRow = ugrOver.Index;
                dropIndexCol = ugrOverCol.Index;
                //index position of drop zone in grid
                SelectedCellsCollection SelCells;
                //get the dragged cell which are to be dragged to another position in the grid
                SelCells = (SelectedCellsCollection)e.Data.GetData(typeof(SelectedCellsCollection));

             
                ultraGrid1.Rows[dropIndexRow].Cells[dropIndexCol].Value = SelCells[0].Value;

            }
}  

 

Sample


 

 

 

 

wingrid_Drag_and_Drop_SingleCell.zip
Parents
No Data
Reply Children
No Data