Articles in this section
Category / Section

How to bind dictionary to column in WPF DataGrid (SfDataGrid)?

1 min read

WPF DataGrid (SfDataGrid) provides support to bind the Dictionary with SfDataGrid.Columns.

In the below code snippet marks dictionary bound to GridColumns by specifying the property name and key to MappingName of GridColumn.

XAML

<Syncfusion:SfDataGrid Name="sfdatagrid"
                               AllowEditing="True"
                               AllowGrouping="True"
                               AutoGenerateColumns="False"
                               ColumnSizer="Star"
                               ItemsSource="{Binding StudentDetails}"
                               ShowGroupDropArea="True">
       <Syncfusion:SfDataGrid.Columns>
             <Syncfusion:GridTextColumn MappingName="StudentID" />
             <Syncfusion:GridTextColumn MappingName="StudentName" />
             <Syncfusion:GridTextColumn MappingName="Major" />
             <Syncfusion:GridTextColumn MappingName="Marks[Subject1]" />
             <Syncfusion:GridTextColumn MappingName="Marks[Subject2]" />
       </Syncfusion:SfDataGrid.Columns>
</Syncfusion:SfDataGrid>

Below is the sample code snippet where Model is added with Dictionary property type (Marks property). You can bind value from dictionary by mapping its key with column.

C#

var dictionary=new Dictionary<string,int>();
dictionary.Add("Subject1", 85);
dictionary.Add("Subject2", 96); 
var studentList = new ObservableCollection<Model>();
 
studentDetails.Add(new Model()
{
   StudentID = 101,
   StudentName = "Joseph",
   Age = 25,
   Major = "Psychology",
   Marks=dictionary,
});

 

Bind the dictionary to column in WPF DataGrid

View sample in GitHub.

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied