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

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)