Click here to Skip to main content
15,946,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I'm developing an application in WPF, in it I use a DataGrids
.Here i am trying to implementing drag and drop functionality in between two grids.I am trying drag row from one datagrid and dropping to another datagrid.
Here i am using the below code.
XAML code

 <DataGrid HorizontalAlignment="Left" AutoGenerateColumns="True" Name="SourceGrid" Margin="12,29,0,12" Width="325" PreviewMouseLeftButtonDown="SourceGrid_PreviewMouseLeftButtonDown" />
<DataGrid Name="DestGrid" AutoGenerateColumns="True" HorizontalAlignment="Right" AllowDrop="True" Margin="0,29,16,12" Width="325" Drop="DestGrid_Drop"/>

XAML.CS code is
private void SourceGrid_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGrid parent = (DataGrid)sender;
            dragSource = parent;
            object data = GetDataFromSourceGrid(dragSource, e.GetPosition(parent));

            if (data != null)
            {
                DragDrop.DoDragDrop(parent, data, DragDropEffects.Move);
            }
        }
        private static object GetDataFromSourceGrid(DataGrid source, Point point)
        {
            UIElement element = source.InputHitTest(point) as UIElement;
            if (element != null)
            {
                object data = DependencyProperty.UnsetValue;
                while (data == DependencyProperty.UnsetValue)
                {
                    data = source.ItemContainerGenerator.ItemFromContainer(element);

                    if (data == DependencyProperty.UnsetValue)
                    {
                        element = VisualTreeHelper.GetParent(element) as UIElement;
                    }

                    if (element == source)
                    {
                        return null;
                    }
                }

                if (data != DependencyProperty.UnsetValue)
                {
                    return data;
                }
            }

            return null;
        }

        private void DestGrid_Drop(object sender, DragEventArgs e)
        {
            DataGrid parent = (DataGrid)sender;
            object data = e.Data.GetData(typeof(string));
            ((IList)dragSource.ItemsSource).Remove(data);
            parent.Items.Add(data);
        }


Here every thing is working fine but in
DestGrid_Drop
event
object data = e.Data.GetData(typeof(string));
this line i am getting null value.

Any one could you please help me.

What I have tried:

i tried so many articles.Same code if i can apply to Listboxes it's working fine.
Posted
Updated 5-Sep-19 20:42pm

Check out this:
WPF 4 DataGrid Row Drag and Drop | DotNetCurry[^]
Drag and Drop DataGrid Row in WPF[^]

You've missed the part which is responsible for row selecting.
 
Share this answer
 
Comments
Ram349 20-Aug-19 3:55am    
Hi Maciej Los,
Thanks for responding my post.I am doing dragging row from one data grid to another data grid.
I tried your one that one not worked.Could you please share any suggestion.
 
Share this answer
 
Comments
Maciej Los 22-Aug-19 8:45am    
:thumbsup:
If your source DataGrid has a list of string items (which I don't know why you'd really be using a DataGrid in that case) then change this method:

private void DestGrid_Drop(object sender, DragEventArgs e)
{
    DataGrid parent = (DataGrid)sender;
    object data = e.Data.GetData(typeof(string));
    ((IList)dragSource.ItemsSource).Remove(data);
    parent.Items.Add(data);
}

Change it to:
private void DestGrid_Drop(object sender, DragEventArgs e)
{
    DataGrid parent = (DataGrid)sender;

    // If the DataObject contains string data, extract it.
    if (e.Data.GetDataPresent(DataFormats.StringFormat))
    {
        var data = (string)e.Data.GetData(DataFormats.StringFormat);
        ((IList)dragSource.ItemsSource).Remove(data);
        parent.Items.Add(data);
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900