Drag & Drop Rows Within C1FlexGrid

Many of our customers have asked for a functionality to drag and drop rows in C1FlexGrid for Winforms. This blog explains an approach to achieve this by using the Drag & Drop feature offered by C1FlexGrid.

Set the Drag & Drop Modes of the Grid

We'll set the DragMode and DropMode properties to Manual and handle the drag & drop later.


//both are OLE drag sources  
{  
    flex.DragMode = DragModeEnum.Manual;  
    flex.DropMode = DropModeEnum.Manual;  
}  

Handling Drag & Drop


private void flex_BeforeMouseDown(object sender, BeforeMouseDownEventArgs e)  
{  
    //start dragging when the user clicks the cell  
    C1FlexGrid flex = (C1FlexGrid)sender;  
    HitTestInfo hti = flex.HitTest(e.X, e.Y);  

    if (hti.Type == HitTestTypeEnum.Cell)  
    {  
        //select the row  
        int index = hti.Row;  
        flex.Select(index, 0, index, flex.Cols.Count - 1, false);  

        //save info for target  
        _src = flex;  

        //do drag drop  
        DragDropEffects dd = flex.DoDragDrop(flex.Clip, DragDropEffects.Move);  

        //if it worked, delete row from source (it's a move)  
        if (dd == DragDropEffects.Move)  
        {  
            flex.Rows.Remove(index);  
        }  

        //done, reset info  
        _src = null;  
    }  
}  

private void flex_DragOver(object sender, DragEventArgs e)  
{  
    //check that we have the type of data we want  
    if (e.Data.GetDataPresent(typeof(string)))  
    {  
        e.Effect = DragDropEffects.Move;  
    }  
}  

private void flex_DragDrop(object sender, DragEventArgs e)  
{  
    //find the drop position  
    C1FlexGrid flex = (C1FlexGrid)sender;  
    Point pt = flex.PointToClient(new Point(e.X, e.Y));  
    HitTestInfo hti = flex.HitTest(pt.X, pt.Y);  

    //newindex - the index where the row is to be moved to  
    //oldindex - the index from where the row was moved from  
    int newindex = hti.Row;  
    int oldindex = flex.RowSel;  

    if (newindex < 0)  
        newindex = flex.Rows.Count;  

        if (newindex < 1)         newindex = 1;     C1.Win.C1FlexGrid.Row row = default(C1.Win.C1FlexGrid.Row);     //going upwards     if (oldindex > 1 & newindex < oldindex)     {         row = flex.Rows[oldindex];     }     //coming downwards     if (newindex > oldindex & (oldindex + 1) < flex.Rows.Count)     {         row = flex.Rows[oldindex + 1];     }     // remove the row from the earlier position     flex.Rows.Remove(oldindex);     // if the row to be added is the last row     if (newindex > flex.Rows.Count)  
    {  
        //insert a new row at the drop position  
        flex.Rows.Insert(newindex - 1);  

        //copy data from source row  
        flex.Select(newindex - 1, 0, newindex - 1, flex.Cols.Count - 1, false);  
        flex.Clip = Convert.ToString(e.Data.GetData(typeof(string)));  
    }  
    else  
    {  
        //insert a new row at the drop position  
        flex.Rows.Insert(newindex);  

        //copy data from source row  
        flex.Select(newindex, 0, newindex, flex.Cols.Count - 1, false);  
        flex.Clip = Convert.ToString(e.Data.GetData(typeof(string)));  

    }  

    flex.Rows.Insert(oldindex);  
}  

Click the image below for a better understanding of the behavior. Flex_DragRows You may refer to the attached samples for complete implementation. Download Sample CS Download Sample VB

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus