Articles in this section
Category / Section

How to synchronize the selection between WPF Chart (SfChart) and DataGrid?

2 mins read

You can synchronize the data source or items source of the data grid with WPF Chart (SfChart) by binding the same data source between the chart and data grid.

You can also synchronically make the selection between the data grid and chart by binding the SelectedIndex (row index) property of data grid with the SelectedIndex property of the chart’s selection behavior as follows.

XAML [SfChart]

<!--Initialization of the selection behavior-->
<syncfusion:SfChart.Behaviors>
    <syncfusion:ChartSelectionBehavior SelectionMode="MouseClick"/>
</syncfusion:SfChart.Behaviors>  
<!—ListenPropertyChange is made true to update the SfChart segment when the data is changed in the SfDataGrid-->
<!--Binding the SelectedIndex Property of the Series with the SelectedIndex property of the SfDataGrid-->
<syncfusion:ColumnSeries  x:Name="sampleSeries1" Label="2013" ListenPropertyChange="True" SegmentSelectionBrush="DarkBlue" SelectedIndex="{Binding ElementName=sampleGrid,   Path=SelectedIndex, Mode=TwoWay}" ItemsSource="{Binding Computers, Mode=TwoWay}" XBindingPath="Computer" YBindingPath="Year2013"/>
<syncfusion:ColumnSeries  x:Name="sampleSeries2" Label="2014" ListenPropertyChange="True" SegmentSelectionBrush="DarkBlue" SelectedIndex="{Binding ElementName=sampleGrid, Path=SelectedIndex, Mode=TwoWay}" ItemsSource="{Binding Computers, Mode=TwoWay}" XBindingPath="Computer" YBindingPath="Year2014"/>

XAML [Grid]

<syncfusion:SfDataGrid x:Name="sampleGrid" Grid.Row="1" AllowEditing="True" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Computers, Mode=TwoWay}" Margin="10,10,10,16" Height="146">
    <syncfusion:SfDataGrid.Columns>
        <syncfusion:GridTextColumn HeaderText="Computer" MappingName="Computer" MinimumWidth="329"/>
        <syncfusion:GridTextColumn TextAlignment="Right" HeaderText="2013" MappingName="Year2013" MinimumWidth="329"/>
        <syncfusion:GridTextColumn TextAlignment="Right" HeaderText="2014" MappingName="Year2014" MinimumWidth="329"/>
    </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

C# [SfChart]

//Creation of the binding for the datasource
Binding mainData = new Binding();
mainData.Path = new PropertyPath("Computers");
mainData.Mode = BindingMode.TwoWay;
//Creation of the binding for the SelectedIndex property of the SfDataGrid
Binding gridIndBind = new Binding();
gridIndBind.ElementName = "sampleGrid";
gridIndBind.Path = new PropertyPath("SelectedIndex");
gridIndBind.Mode = BindingMode.TwoWay;
//Initialization of the selection behavior
ChartSelectionBehavior clickSelection = new ChartSelectionBehavior();
clickSelection.SelectionMode = Syncfusion.UI.Xaml.Charts.SelectionMode.MouseClick;
sampleChart.Behaviors.Add(clickSelection);
//Binding the shared datasource
sampleSeries1.SetBinding(ColumnSeries.ItemsSourceProperty, mainData);
sampleSeries1.XBindingPath = "Computer";
sampleSeries1.YBindingPath = "Year2013";
// ListenPropertyChange is made true to update the SfChart segment when the data is changed in the SfDataGrid
sampleSeries1.ListenPropertyChange = true;
sampleSeries1.SegmentSelectionBrush = Brushes.DarkBlue;
// Binding the SelectedIndex Property of the Series with the SelectedIndex    property of the SfDataGrid
sampleSeries1.SetBinding(ColumnSeries.SelectedIndexProperty, gridIndBind);
sampleSeries2.SetBinding(ColumnSeries.ItemsSourceProperty, mainData);
sampleSeries2.XBindingPath = "Computer";
sampleSeries2.YBindingPath = "Year2014";
sampleSeries2.ListenPropertyChange = true;
sampleSeries2.SegmentSelectionBrush = Brushes.DarkBlue;
sampleSeries2.SetBinding(ColumnSeries.SelectedIndexProperty, gridIndBind);

C# [Grid]

//Binding the shared datasource
sampleGrid.SetBinding(SfDataGrid.ItemsSourceProperty, mainData);

Synchronized WPF Chart and Grid

When a segment in column series is selected using the mouse click, the corresponding SfDataGrid row also will be selected and vice-versa.

Selection synchronization between WPF Chart and Grid

The ListenPropertyChange property should be set to true for listening the update of the properties in the underlying data source.

The following screenshot illustrates the output after editing the data in a cell.

Note:

The segment is updated only when the cell loses its focus (since it updates the underlying values after that).

Modified Data Synchronization between WPF Chart and Grid

 

Conclusion

I hope you enjoyed learning about how to synchronize the selection between WPF Chart (SfChart) and DataGrid.

 You can refer to our WPF Chart feature tour  page to know about its other groundbreaking feature representations. You can also explore  documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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