Home Forums WinForms controls Xceed Grid for WinForms Drag and Drop contents into individual cell

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #15810 |

    How do drag and drop contents into a individual grid cell ?

    I am looking at the “CopyPasteDrag” sample application for guidance however this sample does not drag and drop into a single cell.

    Can anyone provide some sample code to provide this functionality or give me some pointer to get me in the right direction ?

    Imported from legacy forums. Posted by Graeme (had 3133 views)

    Xceed Support
    Member
    Post count: 5658

    Here is some sample code to do that.

    <code>
    private void Form1_Load( object sender, EventArgs e )
    {
    Column col1 = new Column( “Column1” );
    Column col2 = new Column( “Column2” );
    Column col3 = new Column( “Column3” );
    Column col4 = new Column( “Column4” );
    gridControl1.Columns.Add( col1 );
    gridControl1.Columns.Add( col2 );
    gridControl1.Columns.Add( col3 );
    gridControl1.Columns.Add( col4 );

    for( int i = 0; i < 40; i++ )
    {
    Xceed.Grid.DataRow row = gridControl1.DataRows.AddNew();
    foreach( Cell cell in row.Cells )
    {
    cell.Value = “Item ” + i.ToString();
    }
    row.EndEdit();
    }

    foreach( DataCell cell in gridControl1.DataRowTemplate.Cells )
    {
    cell.AllowDrop = true;

    cell.MouseDown += new MouseEventHandler( this.Mouse_Down );
    cell.MouseUp += new MouseEventHandler( this.Mouse_Up );
    cell.MouseMove += new MouseEventHandler( this.Mouse_Move );
    cell.DragOver += new DragEventHandler( this.Drag_Over );
    cell.DragDrop += new DragEventHandler( this.Drag_Drop );
    }

    gridControl1.AllowDrop = true;
    gridControl1.ReadOnly = true;
    }

    Xceed.Grid.Row tempRow;

    private Point m_mouseLocation = Point.Empty;

    private void Mouse_Down( object sender, MouseEventArgs e )
    {
    // Get the location of the mouse when the mouse button is pressed.
    m_mouseLocation = new Point( e.X, e.Y );
    }

    private void Mouse_Up( object sender, MouseEventArgs e )
    {
    // Reset the location of the mouse when the mouse button is released.
    m_mouseLocation = Point.Empty;
    }

    private void Mouse_Move( object sender, MouseEventArgs e )
    {
    // The mouse button is pressed
    if( m_mouseLocation != Point.Empty )
    {
    // The mouse has moved!
    if( Math.Abs( m_mouseLocation.X – e.X ) > 3 || Math.Abs( m_mouseLocation.Y – e.Y ) > 3 )
    {
    // Create our DataObject that will contain the data (rows/cells) to drag.
    DataObject data = new DataObject( this.CreateDataObject( ( ( Xceed.Grid.Cell )sender ).GridControl ) );

    // Initialize the drag and drop operation.
    gridControl1.DoDragDrop( data, DragDropEffects.Copy );

    // Reset the location of the mouse.
    m_mouseLocation = Point.Empty;
    }
    }
    }

    private void Drag_Over( object sender, DragEventArgs e )
    {
    // Get the format of the DataObject being drag onto the grid.
    // If it is plain text, we “accept” it. If not, we do nothing.

    if( tempRow != null )
    {
    tempRow.BackColor = gridControl1.BackColor;
    }

    ( ( Xceed.Grid.Cell )sender ).ParentRow.BackColor = Color.HotPink;

    tempRow = ( ( Xceed.Grid.Cell )sender ).ParentRow;

    if( e.Data.GetDataPresent( DataFormats.Text ) )
    e.Effect = DragDropEffects.Copy;
    }

    private void Drag_Drop( object sender, DragEventArgs e )
    {

    // Retrieve the data that is being dropped onto the grid.
    string data = ( string )e.Data.GetData( DataFormats.Text );

    // If there is data, we will parse it and insert the new DataRows.
    if( data != string.Empty )
    this.InsertNewRows( data, ( ( Xceed.Grid.Cell )sender ) ); //

    if( tempRow != null )
    {
    tempRow.BackColor = gridControl1.BackColor;
    }
    }

    private DataObject CreateDataObject( Xceed.Grid.GridControl sourceGrid )
    {
    DataObject data = new DataObject();

    string text = string.Empty;

    text = sourceGrid.CurrentCell.Value.ToString();
    // Set our plain text DataFormat.
    data.SetData( DataFormats.Text, text );

    // Return the DataObject.
    return data;
    }

    private void InsertNewRows( string data, Cell targetCell )
    {
    if( data != string.Empty && data is string)
    targetCell.Value = data;
    }
    </code>

    Imported from legacy forums. Posted by CharlesB (had 3633 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello,  I tried out this sample for the applicaton i am creating. Basically i am trying to drag and drop a cell’s content and at the same time do some background executions. I would like to see the item + some string being dragged. The bubble during the drag operation needs to show a string value which contains information about the dragged item. In addition to the sample above i had to add a grid.GiveFeedback event to show the bubble but some how it doesnt seem to work. Can you recommend how i can design this functionality

    In short i am looking for code that lets me show the tooltip that has a custom string as i move my dragged cell.

    Imported from legacy forums. Posted by kaio (had 1284 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.