Skip to content

Commit 8fee488

Browse files
Merge pull request #1 from ShobikaPalani/master
how to drag and drop rows between datagrid and treegrid in wpf
2 parents 923b0f0 + fabb2d9 commit 8fee488

24 files changed

+1502
-0
lines changed

App.config

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
5+
</startup>
6+
</configuration>

App.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="DragDropBetweenDataGridTreeGrid.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:DragDropBetweenDataGridTreeGrid"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>

App.xaml.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace DragDropBetweenDataGridTreeGrid
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

DragDropBehavior.cs

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
using Syncfusion.Data.Extensions;
2+
using Syncfusion.UI.Xaml.Grid;
3+
using Syncfusion.UI.Xaml.TreeGrid;
4+
using System;
5+
using System.Collections;
6+
using System.Collections.Generic;
7+
using System.Collections.ObjectModel;
8+
using System.Linq;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
using System.Windows.Interactivity;
12+
13+
namespace DragDropBetweenDataGridTreeGrid
14+
{
15+
public class DragDropBehavior:Behavior<MainWindow>
16+
{
17+
18+
protected override void OnAttached()
19+
{
20+
base.OnAttached();
21+
AssociatedObject.sfDataGrid.RowDragDropController.Drop += sfDataGrid_Drop;
22+
AssociatedObject.sfTreeGrid.RowDragDropController.Drop += sfTreeGrid_Drop;
23+
}
24+
25+
/// <summary>
26+
/// Customized TreeGrid Drop event.
27+
/// </summary>
28+
/// <param name="sender"></param>
29+
/// <param name="e"></param>
30+
private void sfTreeGrid_Drop(object sender, TreeGridRowDropEventArgs e)
31+
{
32+
if (e.IsFromOutSideSource)
33+
{
34+
35+
var draggingRecord = e.Data.GetData("Records") as ObservableCollection<object>;
36+
37+
var record = draggingRecord[0] as EmployeeInfo;
38+
39+
var dropPosition = e.DropPosition.ToString();
40+
41+
var newItem = new EmployeeInfo();
42+
43+
var rowIndex =AssociatedObject.sfTreeGrid.ResolveToRowIndex(e.TargetNode.Item);
44+
45+
if (dropPosition != "None" && rowIndex != -1)
46+
{
47+
if (AssociatedObject.sfTreeGrid.View is TreeGridSelfRelationalView)
48+
{
49+
var treeNode = e.TargetNode;
50+
if (treeNode == null)
51+
return;
52+
53+
var data = treeNode.Item;
54+
55+
AssociatedObject.sfTreeGrid.SelectionController.SuspendUpdates();
56+
57+
var dropIndex = -1;
58+
59+
TreeNode parentNode = null;
60+
61+
if (dropPosition == "DropBelow" || dropPosition == "DropAbove")
62+
{
63+
parentNode = treeNode.ParentNode;
64+
if (parentNode == null)
65+
{
66+
var treeNodeItem = treeNode.Item as EmployeeInfo;
67+
newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = treeNodeItem.ReportsTo };
68+
}
69+
else
70+
{
71+
var parentNodeItems = parentNode.Item as EmployeeInfo;
72+
newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parentNodeItems.ID };
73+
}
74+
}
75+
76+
else if (dropPosition == "DropAsChild")
77+
{
78+
79+
if (!treeNode.IsExpanded)
80+
AssociatedObject.sfTreeGrid.ExpandNode(treeNode);
81+
parentNode = treeNode;
82+
var parentNodeItems = parentNode.Item as EmployeeInfo;
83+
newItem = new EmployeeInfo() { FirstName = record.FirstName, LastName = record.LastName, ID = record.ID, Salary = record.Salary, Title = record.Title, ReportsTo = parentNodeItems.ID };
84+
85+
}
86+
87+
IList sourceCollection = null;
88+
89+
90+
if (dropPosition == "DropBelow" || dropPosition == "DropAbove")
91+
{
92+
93+
if (treeNode.ParentNode != null)
94+
{
95+
var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(treeNode.ParentNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable;
96+
sourceCollection = GetSourceListCollection(collection);
97+
}
98+
else
99+
{
100+
sourceCollection = GetSourceListCollection(AssociatedObject.sfTreeGrid.View.SourceCollection);
101+
}
102+
dropIndex = sourceCollection.IndexOf(data);
103+
104+
if (dropPosition == "DropBelow")
105+
{
106+
dropIndex += 1;
107+
}
108+
}
109+
110+
else if (dropPosition == "DropAsChild")
111+
{
112+
var collection = AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().GetValue(data, AssociatedObject.sfTreeGrid.ChildPropertyName) as IEnumerable;
113+
114+
sourceCollection = GetSourceListCollection(collection);
115+
116+
if (sourceCollection == null)
117+
{
118+
var list = data.GetType().GetProperty(AssociatedObject.sfTreeGrid.ChildPropertyName).PropertyType.CreateNew() as IList;
119+
120+
if (list != null)
121+
{
122+
AssociatedObject.sfTreeGrid.View.GetPropertyAccessProvider().SetValue(treeNode.Item, AssociatedObject.sfTreeGrid.ChildPropertyName, list);
123+
sourceCollection = list;
124+
}
125+
}
126+
dropIndex = sourceCollection.Count;
127+
}
128+
sourceCollection.Insert(dropIndex, newItem);
129+
130+
AssociatedObject.sfTreeGrid.SelectionController.ResumeUpdates();
131+
(AssociatedObject.sfTreeGrid.SelectionController as TreeGridRowSelectionController).RefreshSelection();
132+
e.Handled = true;
133+
}
134+
}
135+
AssociatedObject.sfDataGrid.View.Remove(record);
136+
}
137+
}
138+
139+
140+
141+
/// <summary>
142+
/// Gets the source collection of TreeGrid
143+
/// </summary>
144+
/// <param name="collection"></param>
145+
/// <returns></returns>
146+
private IList GetSourceListCollection(IEnumerable collection)
147+
{
148+
IList list = null;
149+
if (collection == null)
150+
collection = AssociatedObject.sfTreeGrid.View.SourceCollection;
151+
if ((collection as IList) != null)
152+
{
153+
list = collection as IList;
154+
}
155+
return list;
156+
}
157+
158+
/// <summary>
159+
/// Customize the Drop event.restrict the certain record and Drop position from drop.
160+
/// </summary>
161+
/// <param name="sender"></param>
162+
/// <param name="e"></param>
163+
private void sfDataGrid_Drop(object sender, GridRowDropEventArgs e)
164+
{
165+
166+
if (e.IsFromOutSideSource)
167+
{
168+
var draggingRecord = e.Data.GetData("Nodes") as ObservableCollection<TreeNode>;
169+
170+
var record = draggingRecord[0].Item as EmployeeInfo;
171+
172+
int dropIndex = (int)e.TargetRecord;
173+
174+
175+
var dropPosition = e.DropPosition.ToString();
176+
177+
if (record.Title == "Manager")
178+
{
179+
e.Handled = true;
180+
return;
181+
}
182+
183+
184+
IList collection = null;
185+
186+
collection = AssociatedObject.sfDataGrid.View.SourceCollection as IList;
187+
if (dropPosition == "DropAbove")
188+
{
189+
dropIndex--;
190+
collection.Insert(dropIndex, record);
191+
192+
}
193+
else
194+
{
195+
dropIndex++;
196+
collection.Insert(dropIndex, record);
197+
198+
}
199+
AssociatedObject.sfTreeGrid.View.Remove(record);
200+
e.Handled = true;
201+
}
202+
}
203+
204+
protected override void OnDetaching()
205+
{
206+
base.OnDetaching();
207+
208+
AssociatedObject.sfDataGrid.RowDragDropController.Drop -= sfDataGrid_Drop;
209+
AssociatedObject.sfTreeGrid.RowDragDropController.Drop -= sfTreeGrid_Drop;
210+
}
211+
212+
}
213+
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{A7057548-EB17-4CA0-A5CF-A009D04674BF}</ProjectGuid>
8+
<OutputType>WinExe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>DragDropBetweenDataGridTreeGrid</RootNamespace>
11+
<AssemblyName>DragDropBetweenDataGridTreeGrid</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
15+
<WarningLevel>4</WarningLevel>
16+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
17+
</PropertyGroup>
18+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
19+
<PlatformTarget>AnyCPU</PlatformTarget>
20+
<DebugSymbols>true</DebugSymbols>
21+
<DebugType>full</DebugType>
22+
<Optimize>false</Optimize>
23+
<OutputPath>bin\Debug\</OutputPath>
24+
<DefineConstants>DEBUG;TRACE</DefineConstants>
25+
<ErrorReport>prompt</ErrorReport>
26+
<WarningLevel>4</WarningLevel>
27+
</PropertyGroup>
28+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
29+
<PlatformTarget>AnyCPU</PlatformTarget>
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="ReachFramework" />
39+
<Reference Include="Syncfusion.Data.WPF">
40+
</Reference>
41+
<Reference Include="Syncfusion.SfGrid.WPF">
42+
</Reference>
43+
<Reference Include="Syncfusion.Shared.WPF">
44+
</Reference>
45+
<Reference Include="System" />
46+
<Reference Include="System.ComponentModel.DataAnnotations" />
47+
<Reference Include="System.Data" />
48+
<Reference Include="System.Runtime.Serialization" />
49+
<Reference Include="System.Windows.Interactivity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
50+
<Reference Include="System.Xml" />
51+
<Reference Include="Microsoft.CSharp" />
52+
<Reference Include="System.Core" />
53+
<Reference Include="System.Xml.Linq" />
54+
<Reference Include="System.Data.DataSetExtensions" />
55+
<Reference Include="System.Xaml">
56+
<RequiredTargetFramework>4.0</RequiredTargetFramework>
57+
</Reference>
58+
<Reference Include="WindowsBase" />
59+
<Reference Include="PresentationCore" />
60+
<Reference Include="PresentationFramework" />
61+
</ItemGroup>
62+
<ItemGroup>
63+
<ApplicationDefinition Include="App.xaml">
64+
<Generator>MSBuild:Compile</Generator>
65+
<SubType>Designer</SubType>
66+
</ApplicationDefinition>
67+
<Compile Include="DragDropBehavior.cs" />
68+
<Compile Include="Settings.cs" />
69+
<Compile Include="ViewModel.cs" />
70+
<Page Include="MainWindow.xaml">
71+
<Generator>MSBuild:Compile</Generator>
72+
<SubType>Designer</SubType>
73+
</Page>
74+
<Compile Include="App.xaml.cs">
75+
<DependentUpon>App.xaml</DependentUpon>
76+
<SubType>Code</SubType>
77+
</Compile>
78+
<Compile Include="MainWindow.xaml.cs">
79+
<DependentUpon>MainWindow.xaml</DependentUpon>
80+
<SubType>Code</SubType>
81+
</Compile>
82+
</ItemGroup>
83+
<ItemGroup>
84+
<Compile Include="EmployeeInfo.cs" />
85+
<Compile Include="Properties\AssemblyInfo.cs">
86+
<SubType>Code</SubType>
87+
</Compile>
88+
<Compile Include="Properties\Resources.Designer.cs">
89+
<AutoGen>True</AutoGen>
90+
<DesignTime>True</DesignTime>
91+
<DependentUpon>Resources.resx</DependentUpon>
92+
</Compile>
93+
<Compile Include="Properties\Settings.Designer.cs">
94+
<AutoGen>True</AutoGen>
95+
<DependentUpon>Settings.settings</DependentUpon>
96+
<DesignTimeSharedInput>True</DesignTimeSharedInput>
97+
</Compile>
98+
<EmbeddedResource Include="Properties\Resources.resx">
99+
<Generator>ResXFileCodeGenerator</Generator>
100+
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
101+
</EmbeddedResource>
102+
<None Include="Properties\Settings.settings">
103+
<Generator>SettingsSingleFileGenerator</Generator>
104+
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
105+
</None>
106+
<AppDesigner Include="Properties\" />
107+
</ItemGroup>
108+
<ItemGroup>
109+
<None Include="App.config" />
110+
</ItemGroup>
111+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
112+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
113+
Other similar extension points exist, see Microsoft.Common.targets.
114+
<Target Name="BeforeBuild">
115+
</Target>
116+
<Target Name="AfterBuild">
117+
</Target>
118+
-->
119+
</Project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DragDropBetweenDataGridTreeGrid_2010", "DragDropBetweenDataGridTreeGrid_2010.csproj", "{A7057548-EB17-4CA0-A5CF-A009D04674BF}"
5+
EndProject
6+
Global
7+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
8+
Debug|Any CPU = Debug|Any CPU
9+
Release|Any CPU = Release|Any CPU
10+
EndGlobalSection
11+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
12+
{A7057548-EB17-4CA0-A5CF-A009D04674BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
13+
{A7057548-EB17-4CA0-A5CF-A009D04674BF}.Debug|Any CPU.Build.0 = Debug|Any CPU
14+
{A7057548-EB17-4CA0-A5CF-A009D04674BF}.Release|Any CPU.ActiveCfg = Release|Any CPU
15+
{A7057548-EB17-4CA0-A5CF-A009D04674BF}.Release|Any CPU.Build.0 = Release|Any CPU
16+
EndGlobalSection
17+
GlobalSection(SolutionProperties) = preSolution
18+
HideSolutionNode = FALSE
19+
EndGlobalSection
20+
EndGlobal

0 commit comments

Comments
 (0)