Skip to content

Commit 9fc101e

Browse files
committed
How to get the target record when drop the row in WPF DataGrid(SfDataGrid)?
1 parent 1c317ab commit 9fc101e

File tree

15 files changed

+681
-0
lines changed

15 files changed

+681
-0
lines changed

SfDataGridDemo/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>

SfDataGridDemo/App.ico

4.19 KB
Binary file not shown.

SfDataGridDemo/App.xaml

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

SfDataGridDemo/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 SfDataGridDemo
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}

SfDataGridDemo/MainWindow.xaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<Window x:Class="SfDataGridDemo.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
5+
xmlns:local="clr-namespace:SfDataGridDemo"
6+
Title="SfDataGrid Demo"
7+
Icon="App.ico"
8+
WindowStartupLocation="CenterScreen">
9+
<Window.DataContext>
10+
<local:ViewModel/>
11+
</Window.DataContext>
12+
<Grid>
13+
<Grid.ColumnDefinitions>
14+
<ColumnDefinition/>
15+
<ColumnDefinition Width="200"/>
16+
</Grid.ColumnDefinitions>
17+
<syncfusion:SfDataGrid x:Name="sfDataGrid"
18+
AllowDraggingRows="True"
19+
AllowResizingColumns="True"
20+
AllowDrop="True"
21+
ColumnSizer="Star"
22+
ShowGroupDropArea="True"
23+
ItemsSource="{Binding Orders}"
24+
AutoGenerateColumns="False">
25+
<syncfusion:SfDataGrid.Columns>
26+
<syncfusion:GridTextColumn MappingName="OrderID"/>
27+
<syncfusion:GridTextColumn MappingName="CustomerID" />
28+
<syncfusion:GridTextColumn MappingName="CustomerName" />
29+
<syncfusion:GridTextColumn MappingName="Country" />
30+
<syncfusion:GridTextColumn MappingName="UnitPrice"/>
31+
</syncfusion:SfDataGrid.Columns>
32+
</syncfusion:SfDataGrid>
33+
<StackPanel Grid.Column="1">
34+
<Label Content="Target Record" HorizontalAlignment="Center"/>
35+
<TextBox x:Name="txtDisplayRecord" />
36+
</StackPanel>
37+
</Grid>
38+
</Window>

SfDataGridDemo/MainWindow.xaml.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using Syncfusion.Data;
3+
using Syncfusion.UI.Xaml.Grid;
4+
using Syncfusion.UI.Xaml.Grid.Helpers;
5+
using System.Windows;
6+
7+
namespace SfDataGridDemo
8+
{
9+
/// <summary>
10+
/// Interaction logic for MainWindow.xaml
11+
/// </summary>
12+
public partial class MainWindow : Window
13+
{
14+
public MainWindow()
15+
{
16+
InitializeComponent();
17+
18+
sfDataGrid.RowDragDropController.Drop += RowDragDropController_Drop;
19+
}
20+
21+
private void RowDragDropController_Drop(object sender, GridRowDropEventArgs e)
22+
{
23+
var droppedIndex = (int)e.TargetRecord;
24+
25+
var rowIndex = this.sfDataGrid.ResolveToRowIndex(droppedIndex);
26+
27+
NodeEntry recordEntry = null;
28+
29+
if (this.sfDataGrid.View.TopLevelGroup != null)
30+
recordEntry = this.sfDataGrid.View.TopLevelGroup.DisplayElements[this.sfDataGrid.ResolveToRecordIndex(rowIndex)];
31+
else
32+
recordEntry = this.sfDataGrid.View.Records[this.sfDataGrid.ResolveToRecordIndex(rowIndex)];
33+
34+
var targetRecord = ((recordEntry as RecordEntry).Data as OrderInfo);
35+
36+
txtDisplayRecord.Text = "OrderId : " + targetRecord.OrderID + "\nCustomerID : " + targetRecord.CustomerID + "\nCustomerName : " + targetRecord.CustomerName + "\nCountry : " + targetRecord.Country + "\nUnitPrice : " + targetRecord.UnitPrice + "\nRow Index :" + droppedIndex;
37+
}
38+
}
39+
}
40+
41+
42+

SfDataGridDemo/Model/Model.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using Syncfusion.Windows.Shared;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.ComponentModel.DataAnnotations;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace SfDataGridDemo
11+
{
12+
public class OrderInfo : INotifyPropertyChanged
13+
{
14+
int orderID;
15+
string customerId;
16+
string country;
17+
string customerName;
18+
int unitPrice;
19+
20+
[Display(Name = "Order ID")]
21+
public int OrderID
22+
{
23+
get { return orderID; }
24+
set
25+
{
26+
orderID = value;
27+
OnPropertyChanged("OrderID");
28+
}
29+
}
30+
31+
[Display(Name = "Customer ID")]
32+
public string CustomerID
33+
{
34+
get { return customerId; }
35+
set
36+
{
37+
customerId = value;
38+
OnPropertyChanged("CustomerID");
39+
}
40+
}
41+
42+
[Display(Name = "Customer Name")]
43+
public string CustomerName
44+
{
45+
get { return customerName; }
46+
set
47+
{
48+
customerName = value;
49+
OnPropertyChanged("CustomerName");
50+
}
51+
}
52+
53+
[Display(Name = "Country")]
54+
public string Country
55+
{
56+
get { return country; }
57+
set
58+
{
59+
country = value;
60+
OnPropertyChanged("Country");
61+
}
62+
}
63+
64+
[Display(Name = "Unit Price")]
65+
public int UnitPrice
66+
{
67+
get { return unitPrice; }
68+
set
69+
{
70+
unitPrice = value;
71+
OnPropertyChanged("Salary");
72+
}
73+
}
74+
75+
public OrderInfo(int orderId, string customerName, string country, string customerId, int unitPrice)
76+
{
77+
this.OrderID = orderId;
78+
this.CustomerName = customerName;
79+
this.Country = country;
80+
this.CustomerID = customerId;
81+
this.UnitPrice = unitPrice;
82+
}
83+
84+
public event PropertyChangedEventHandler PropertyChanged;
85+
public void OnPropertyChanged(string PropertyName)
86+
{
87+
if (PropertyChanged != null)
88+
PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
89+
}
90+
}
91+
}
92+
93+
94+
95+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Reflection;
2+
using System.Resources;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
using System.Windows;
6+
7+
// General Information about an assembly is controlled through the following
8+
// set of attributes. Change these attribute values to modify the information
9+
// associated with an assembly.
10+
[assembly: AssemblyTitle("SfDataGridDemo")]
11+
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyConfiguration("")]
13+
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyProduct("SfDataGridDemo")]
15+
[assembly: AssemblyCopyright("Copyright © 2015")]
16+
[assembly: AssemblyTrademark("")]
17+
[assembly: AssemblyCulture("")]
18+
19+
// Setting ComVisible to false makes the types in this assembly not visible
20+
// to COM components. If you need to access a type in this assembly from
21+
// COM, set the ComVisible attribute to true on that type.
22+
[assembly: ComVisible(false)]
23+
24+
//In order to begin building localizable applications, set
25+
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
26+
//inside a <PropertyGroup>. For example, if you are using US english
27+
//in your source files, set the <UICulture> to en-US. Then uncomment
28+
//the NeutralResourceLanguage attribute below. Update the "en-US" in
29+
//the line below to match the UICulture setting in the project file.
30+
31+
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32+
33+
34+
[assembly: ThemeInfo(
35+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36+
//(used if a resource is not found in the page,
37+
// or application resource dictionaries)
38+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39+
//(used if a resource is not found in the page,
40+
// app, or any theme specific resource dictionaries)
41+
)]
42+
43+
44+
// Version information for an assembly consists of the following four values:
45+
//
46+
// Major Version
47+
// Minor Version
48+
// Build Number
49+
// Revision
50+
//
51+
// You can specify all the values or you can default the Build and Revision Numbers
52+
// by using the '*' as shown below:
53+
// [assembly: AssemblyVersion("1.0.*")]
54+
[assembly: AssemblyVersion("1.0.0.0")]
55+
[assembly: AssemblyFileVersion("1.0.0.0")]

SfDataGridDemo/Properties/Resources.Designer.cs

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)