Skip to content

Commit a00940d

Browse files
Merge pull request #1 from FarjanaParveen/master
How to bind the list of string to WPF DataGrid(SfDataGrid)?
2 parents 1e2d435 + de4e51e commit a00940d

15 files changed

+677
-2
lines changed

README.md

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,50 @@
1-
# how-to-bind-the-list-of-string-to-wpf-data-grid
2-
How to bind the list of string to WPF DataGrid(SfDataGrid)?
1+
# How to bind the list of string to WPF DataGrid(SfDataGrid)?
2+
3+
## About the sample
4+
5+
This example illustrates how to bind the list of string to WPF DataGrid
6+
7+
By default, SfDataGrid is bound to a collection with a class type. You can bind a list of string as an ItemsSource of SfDataGrid can be achieved by using GridTemplateColumn.
8+
9+
```c#
10+
public class ViewModel
11+
{
12+
public ViewModel()
13+
{
14+
this.DataFieldList = new List<string>() { "Item1", "Item2" , "Item3", "Item4", "Item5", "Item6", "Item7", "Item8", "Item9", "Item10" };
15+
}
16+
17+
public List<string> DataFieldList
18+
{
19+
get;
20+
set;
21+
}
22+
}
23+
```
24+
```xml
25+
<Window.DataContext>
26+
<local:ViewModel />
27+
</Window.DataContext>
28+
<Grid>
29+
<syncfusion:SfDataGrid Name="dataGrid"
30+
AllowFiltering="False"
31+
AllowResizingColumns="False"
32+
AutoGenerateColumns="False"
33+
ColumnSizer="Star"
34+
NavigationMode="Row"
35+
ItemsSource="{Binding DataFieldList, UpdateSourceTrigger=PropertyChanged}">
36+
<syncfusion:SfDataGrid.Columns>
37+
<syncfusion:GridTemplateColumn HeaderText="Column Name" >
38+
<syncfusion:GridTemplateColumn.CellTemplate>
39+
<DataTemplate>
40+
<TextBlock Text="{Binding}" />
41+
</DataTemplate>
42+
</syncfusion:GridTemplateColumn.CellTemplate>
43+
</syncfusion:GridTemplateColumn>
44+
</syncfusion:SfDataGrid.Columns>
45+
</syncfusion:SfDataGrid>
46+
</Grid>
47+
```
48+
49+
## Requirements to run the demo
50+
Visual Studio 2015 and above versions

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.xaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="SfDataGrid_MVVM.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:SfDataGrid_MVVM"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</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 SfDataGrid_MVVM
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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<Window x:Class="SfDataGrid_MVVM.MainWindow"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:SfDataGrid_MVVM"
7+
xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
8+
mc:Ignorable="d"
9+
Title="MainWindow" Height="450" Width="800">
10+
<Window.DataContext>
11+
<local:ViewModel />
12+
</Window.DataContext>
13+
<Grid>
14+
<syncfusion:SfDataGrid Name="dataGrid"
15+
AllowFiltering="False"
16+
AllowResizingColumns="False"
17+
AutoGenerateColumns="False"
18+
ColumnSizer="Star"
19+
NavigationMode="Row"
20+
ItemsSource="{Binding DataFieldList, UpdateSourceTrigger=PropertyChanged}">
21+
<syncfusion:SfDataGrid.Columns>
22+
<syncfusion:GridTemplateColumn HeaderText="Column Name" >
23+
<syncfusion:GridTemplateColumn.CellTemplate>
24+
<DataTemplate>
25+
<TextBlock Text="{Binding}" />
26+
</DataTemplate>
27+
</syncfusion:GridTemplateColumn.CellTemplate>
28+
</syncfusion:GridTemplateColumn>
29+
</syncfusion:SfDataGrid.Columns>
30+
</syncfusion:SfDataGrid>
31+
</Grid>
32+
33+
</Window>

SfDataGridDemo/MainWindow.xaml.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using Syncfusion.UI.Xaml.Grid;
2+
using Syncfusion.UI.Xaml.TreeGrid;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Data;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Windows;
10+
using System.Windows.Controls;
11+
using System.Windows.Data;
12+
using System.Windows.Documents;
13+
using System.Windows.Input;
14+
using System.Windows.Media;
15+
using System.Windows.Media.Imaging;
16+
using System.Windows.Navigation;
17+
using System.Windows.Shapes;
18+
19+
namespace SfDataGrid_MVVM
20+
{
21+
/// <summary>
22+
/// Interaction logic for MainWindow.xaml
23+
/// </summary>
24+
public partial class MainWindow : Window
25+
{
26+
public MainWindow()
27+
{
28+
InitializeComponent();
29+
}
30+
}
31+
}

SfDataGridDemo/Model.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Collections;
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 SfDataGrid_MVVM
11+
{
12+
public class EmployeeInfo: IDataErrorInfo,INotifyDataErrorInfo
13+
{
14+
int _id;
15+
string _firstName;
16+
string _lastName;
17+
private string _title;
18+
decimal _salary;
19+
int _reportsTo;
20+
21+
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
22+
23+
public string FirstName
24+
{
25+
get { return _firstName; }
26+
set { _firstName = value; }
27+
}
28+
29+
public string LastName
30+
{
31+
get { return _lastName; }
32+
set { _lastName = value; }
33+
}
34+
public int ID
35+
{
36+
get { return _id; }
37+
set { _id = value; }
38+
}
39+
40+
public string Title
41+
{
42+
get { return _title; }
43+
set { _title = value; }
44+
}
45+
46+
public decimal Salary
47+
{
48+
get { return _salary; }
49+
set { _salary = value; }
50+
}
51+
52+
public int ReportsTo
53+
{
54+
get { return _reportsTo; }
55+
set { _reportsTo = value; }
56+
}
57+
58+
[Display(AutoGenerateField = false)]
59+
60+
public string Error => string.Empty;
61+
62+
public string this[string columnName]
63+
{
64+
get
65+
{
66+
string errorMessage = string.Empty;
67+
if (!columnName.Equals("Salary"))
68+
return string.Empty;
69+
if (this.Salary < 30000)
70+
errorMessage+= "The 'Salary'should be less than 30000" ;
71+
if (this.Salary == 0)
72+
errorMessage += "\nThe 'Salary'should have some values";
73+
74+
return errorMessage;
75+
}
76+
}
77+
78+
[Display(AutoGenerateField = false)]
79+
80+
public bool HasErrors
81+
{
82+
get
83+
{
84+
return false;
85+
}
86+
}
87+
88+
private List<string> errors = new List<string>();
89+
public IEnumerable GetErrors(string propertyName)
90+
{
91+
return errors;
92+
}
93+
}
94+
}
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("SfDataGrid_MVVM")]
11+
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyConfiguration("")]
13+
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyProduct("SfDataGrid_MVVM")]
15+
[assembly: AssemblyCopyright("Copyright © 2019")]
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)