We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

SfChart itemssource to get current filtered data from sfDataGrid

Hi,
I see that I need to itterate thru all the filtered records which is ok if I had model class but sfDataGrid is linked to DataTable.

In general I would like to add current filtered data which is contained per documentation in  sfDataGrid.View.Records
to series1.ItemsSource of  sfChart1
something like this:

 series1.ItemsSource = sfDataGrid2.View.Records  //This doesn't work
 series1.XBindingPath = "Articlename";
 series1.YBindingPath = "Price";
 sfChart1.Series.Add(series1);

Any help is appreciated.

5 Replies

DS Durgadevi Selvaraj Syncfusion Team July 17, 2017 10:24 AM UTC

Hi Dubravko, 

Thanks for contacting Syncfusion Support. 

We have analyzed your requirement and we can set Filtered record of DataGrid collection to the ItemsSource of chart as like in the below code snippet, 


MainWindow.xaml: 

<syncfusion:SfDataGrid Name="sfgrid" FilterChanged="sfgrid_FilterChanged"  
                                       AllowDraggingColumns="True" 
                                       AllowEditing="True" 
                                       AllowFiltering="True"                                     
                                       ItemsSource="{Binding OrderList}" 
                                       ShowGroupDropArea="True" /> 

MainWindow.cs: 

private void sfgrid_FilterChanged(object sender, Syncfusion.UI.Xaml.Grid.GridFilterEventArgs e) 
        { 
            ObservableCollection<OrderInfo> order = new ObservableCollection<OrderInfo>(); 
            
            var records = (sender as SfDataGrid).View.Records; 
            foreach (RecordEntry record in records) 
                order.Add(record.Data as OrderInfo); 
 
            chart.Series.Clear(); 
 
            ColumnSeries series = new ColumnSeries() 
            { 
                ItemsSource = order, 
                XBindingPath = "ProductName", 
                YBindingPath = "Price", 
            }; 
 
 
             chart.Series.Add(series); 
        } 


Please find the output screenshot of filterd data, 
  
We have prepared a simple demo sample based on this and it can be downloaded from the below link, 

Sample:  SfChart_Sample 

Please let us know if you have any concerns. 

Regards,  
Durgadevi S 




DF df July 17, 2017 12:36 PM UTC

Hi,thanks for the reply. I see that you showed me the same sample I refered in my question (https://help.syncfusion.com/wpf/sfdatagrid/filtering#getting-the-filtered-records). I see that you have model class OrderInfo and that part is clear

.My problem is that ItemsSource of the data grid is DataSet so no class is involved. 

Let's say this is my DataSet which I populate the data grid. 

 public void FillDataSet()

        {

            

            DataTable dtArticles = new DataTable("Articles");

            dtArticles.Columns.Add("id");

            dtArticles.Columns.Add("name");

            dtArticles.Columns.Add("idgroup");


            dtArticles.Rows.Add(1, "Cola", 1);

            dtArticles.Rows.Add(2, "Pizza", 2);

            dtArticles.Rows.Add(3, "Steak", 2);


            DataTable dtGroups = new DataTable("Groups");

            dtGroups.Columns.Add("idgroup");

            dtGroups.Columns.Add("groupname");


            dtGroups.Rows.Add(1, "Drink");

            dtGroups.Rows.Add(2, "Food");


            ds = new DataSet("gastronomy");


            ds.Tables.Add(dtArticles);

            ds.Tables.Add(dtGroups);


        }       


 When I filter the data inside the sfDataGrid, let's say I want to see only Coca Cola inside the graph, this is where I have the problem.

I also attached  Visual Studio project file where you can see the problem.

1. When I run this test application I get this:Chart is empty.



 When I press ItemSource button to fill the chart I get this:


Which is also great. But now I make a filter to show me only Cola


 My grid now looks like:


And now the problem, how to translate this filtered grid data to chart so that I only see Cola, but not with model class because I don't have it.

Best regards


Attachment: WpfApplication1_e31a6d78.7z


DS Durgadevi Selvaraj Syncfusion Team July 18, 2017 12:37 PM UTC

Hi Dubravko, 
 
Thanks for your update. 
 
We have analyzed the reported problem and we can achieve your requirement of (setting  filtered  grid Data to chart) by taking the filtered view data from grid table and set it to the  ItemsSource of the chart as shown in the below code, 
 
 
  private void btnItemSource_Click(object sender, RoutedEventArgs e) 
        { 
            // This part links data source diretly to the chart from DataTable 
 
            sfChart1.Series.Clear(); 
 
            sfChart1.Header = "TEST header"; 
 
            CategoryAxis primaryAxis = new CategoryAxis(); 
            primaryAxis.Header = "Article Name"; 
            //primaryAxis.ShowTrackBallInfo = true; 
            sfChart1.PrimaryAxis = primaryAxis; 
 
            NumericalAxis secondaryAxis = new NumericalAxis(); 
            secondaryAxis.Header = "Group"; 
            sfChart1.SecondaryAxis = secondaryAxis; 
 
            ColumnSeries series1 = new ColumnSeries(); 
 
            // Here I'm converting DataView from grid to DataTable 
             
            DataTable myTable; 
                        DataView MyView; 
 
            //// Bind dataGridView to DataView. 
 
            MyView = (DataView)sfDataGrid2.ItemsSource; 
 
            // Bind  DataView to Table. 
 
           myTable = (DataTable)MyView.ToTable(); 
 
            series1.ItemsSource = myTable; 
            series1.XBindingPath = "Name"; 
            series1.YBindingPath = "idgroup"; 
            series1.ShowTooltip = true; 
 
            sfChart1.Series.Add(series1); 
 
 
 
        } 
 
 
Please find the output screenshot, 
 
 
We have modified the given sample based on your requirement and please downloaded from the below link, 
 
Regards,  
Durgadevi S 





DF df July 18, 2017 02:33 PM UTC

Thank you very much, it was so simple:

from

            myTable = (DataTable)MyView.Table;

to 

            myTable = (DataTable)MyView.ToTable();

Again thank you for your time.



DS Durgadevi Selvaraj Syncfusion Team July 19, 2017 03:48 AM UTC

Hi Dubravko, 
 
We glad to know that your problem has been resolved. Please let us know if you need any further assistance. 
 
Regards, 
Durgadevi S. 


Loader.
Up arrow icon