Drag and Drop Grid Control Row DevExpress WPF


To simply drag a grid row and drop it in another grindcontrol follow these steps below

forexample we have 2 gridcontrols with both a tableview, lets call them
grid1 and gridview1
grid2 and gridview2

first add these 2 events to yr gridview1:
MouseDown=”gridview1_MouseDown”
MouseMove=”gridview1_MouseMove”

Then add these 2 events and 1 property to yr gridview2:
DragEnter=”gridview2_DragEnter”
Drop=”gridview2_Drop”
AllowDrop=”True” 

Then in code behind add this:

using DevExpress.Data;

bool _dragStarted = false;

// this is code for the gridview1

private void gridview1_MouseDown(object sender, MouseButtonEventArgs e)
{
    int rowHandle = gridViewMedia.GetRowHandleByMouseEventArgs(e);
    if (rowHandle != GridDataController.InvalidRow)
        _dragStarted = true;
}


private void gridview1_MouseMove(object sender, MouseEventArgs e)
{
    int rowHandle = gridview1.GetRowHandleByMouseEventArgs(e);
    if (_dragStarted)
    {
        DataObject data = CreateDataObject(rowHandle);
        FrameworkElement element = gridview1.GetRowElementByMouseEventArgs(e);
        if(element != null)
            DragDrop.DoDragDrop(element, data, DragDropEffects.Move | DragDropEffects.Copy);
        _dragStarted = false;
    }
}

private DataObject CreateDataObject(int rowHandle)
{
    DataObject data = new DataObject();
    data.SetData(typeof(int), rowHandle);
    return data;
}

// ------------------------



// this is code for the gridview2

private void gridview2_DragEnter(object sender, DragEventArgs e)
{
    e.Effects = DragDropEffects.Copy;
    e.Handled = true;
}

private void gridview2_Drop(object sender, DragEventArgs e)
{
    int rowHandle = (int)e.Data.GetData(typeof(int));
    MyObject myObject = (MyObject)grid1.GetRow(rowHandle);
    if (myObject != null)
    {
	// this is just a list/ List<MyObjects>
	_droppedItems.Add(myObject);

	// clear the datasource, and reload it the datasource with the new object added to the collection
	grid2.ItemsSource = null;
	grid2.ItemsSource = _droppedItems;
	
	// optional remove the focus from the grid
	gridview2.FocusedRowHandle = -1;

	// remove the row from the grid1
	grid1.DeleteRow(rowHandle);
    }
}


// ------------------------
About these ads

About johnnblade aka johnnyblade

C# Developer
This entry was posted in WPF & DevExpress and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s