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.