Skip to main content Skip to footer

Spread Windows Forms and Drag and Drop

You can drag and drop data from a Spread control to another Spread control using drag events such as DragEnter, DragOver, and DragDrop. Drag and drop requires fewer steps than copy and paste. The following example demonstrates using drag and drop with two Spread controls and a list box control. 1. Set the AllowDrop property to true to specify that you can drop data on a control. Set the AllowDragDrop property for Spread as well as the AllowDrop property to allow data to be dropped on the Spread control. For example:

listBox1.AllowDrop = true;  
fpSpread2.AllowDrop = true;  
fpSpread2.AllowDragDrop = true;  

This Spread example uses data from a database as the source data. For example:

cr = new FarPoint.Win.Spread.Model.CellRange(0, 0, 1, 1);  
s = fpSpread1.ActiveSheet.GetClip(0, 0, 1, 1);  

string pathname;  
pathname = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\\\GrapeCity\\\Spread Studio 8\\\Common\\\";  
string conStr = "Provider=Microsoft.JET.OLEDB.4.0;data source= " + pathname + "Patients2000.mdb";  
string sqlStr = "SELECT * FROM Patients";  
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(conStr);  
DataSet ds = new DataSet();  
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sqlStr, conn);  
fpSpread1.ActiveSheet.DataAutoSizeColumns = true;  
da.Fill(ds);  
fpSpread1.DataSource = ds;  

2. The DragOver event occurs when the control is being dragged over another object. You can use this event to specify what happens after dragging starts and before the control is dropped. The Spread example uses this event to get the column and row indexes, to clear the previous selection, and to add a new selection based on the selected data.

private void fpSpread2_DragOver(object sender, DragEventArgs e)  
        {  
            int r, c, rc, cc;  
            r = cr.Row;  
            c = cr.Column;  
            rc = cr.RowCount;  
            cc = cr.ColumnCount;  
            Point p = fpSpread2.PointToClient(new Point(e.X, e.Y));  
            FarPoint.Win.Spread.Model.CellRange range = fpSpread2.GetCellFromPixel(0, 0, p.X, p.Y);  
            if (range.ColumnCount == -1 && range.RowCount == -1)  
                return;  
            FarPoint.Win.Spread.Model.CellRange sel = fpSpread2.ActiveSheet.GetSelection(0);  
            if (sel != null)  
            fpSpread2.ActiveSheet.RemoveSelection(sel.Row, sel.Column, sel.RowCount, sel.ColumnCount);  

            if (cc == -1)  
            {  
                fpSpread2.ActiveSheet.AddSelection(range.Row, -1, rc, -1);  
            }  
            else if (rc == -1)  
            {  
                fpSpread2.ActiveSheet.AddSelection(-1, range.Column, -1, cc);  
            }  
            else  
            {  
                fpSpread2.ActiveSheet.AddSelection(range.Row, range.Column, rc, cc);  
            }                         
        }  

3. The DragEnter event occurs when the control is dragged into the target drop area. You can use this event to get data and set drag effects. Drag effects in this event will be overwritten if they are also set in the DragOver event. If you want to add effect code to DragOver instead, you could add code similar to the following. This code changes the effect if you also use the Ctrl key. For example: (VB) e.Effect = IIf((ModifierKeys And Keys.Control) = Keys.Control, DragDropEffects.Copy, DragDropEffects.Move) or (C#) e.Effect = ((ModifierKeys & Keys.Control) == Keys.Control ? DragDropEffects.Copy : DragDropEffects.Move);

private void fpSpread2_DragEnter(object sender, DragEventArgs e)  
        {  
            if (e.Data.GetDataPresent(DataFormats.Text))  
            {  
                e.Effect = DragDropEffects.Move;  
            }              
        }  

4. The DragDrop event occurs when the mouse is released during the drag and drop operation. Use this event to handle the data you wish to put on the target control. The Spread example uses this event to get the current selection and place the data in the Spread control with the SetClip method.

private void fpSpread2_DragDrop(object sender, DragEventArgs e)  
        {  
            int r, c, rc, cc;  
            r = cr.Row;  
            c = cr.Column;  
            rc = cr.RowCount;  
            cc = cr.ColumnCount;  
            FarPoint.Win.Spread.Model.CellRange sel = fpSpread2.ActiveSheet.GetSelection(0);  
            if (sel != null)  
                fpSpread2.ActiveSheet.SetClip(sel.Row, sel.Column, sel.RowCount, sel.ColumnCount, s);              
        }  

5. This example uses the MouseDown event to specify drag drop effects for the right mouse button when getting the data from fpSpread1.

private void fpSpread1_MouseDown(object sender, MouseEventArgs e)  
        {  
            if (e.Button == MouseButtons.Right)  
            {  
                fpSpread1.DoDragDrop(s, DragDropEffects.Copy | DragDropEffects.Move);  
            }              
        }  

6. This example uses the MouseUp event to get data from fpSpread1.

private void fpSpread1_MouseUp(object sender, MouseEventArgs e)  
        {  
            try  
            {  
                cr = fpSpread1.ActiveSheet.GetSelection(0);  
                s = fpSpread1.ActiveSheet.GetClip(cr.Row, cr.Column, cr.RowCount, cr.ColumnCount);  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show(ex.ToString() + "You did not make a selection!");  
                return;  
            }             
        }  

You can get the complete sample from: DragAndDropCSharp or DragAndDropVB. You will need to add the Spread references to the project. They are not included in the zip file. Run the example and select the data from fpSpread1 that you wish to drag, right-click on the selection, then drag it to fpSpread2 or the list box. Release the mouse to drop the data. The mouse pointer must be over a cell in the Spread control (not a header). The list box only displays the data from the active cell in this example. Drag-drop usually uses the left mouse button. This example uses the right mouse button for drag because the left mouse button is used for the selection in Spread.

MESCIUS inc.

comments powered by Disqus