Howto linking a Chart to a RadGridView

3 posts, 1 answers
  1. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 03 Nov 2014 Link to this post

    Hi,

    I would like to generate a dynamic graphic chart linked to a normal radGridview, which would be automatically generated as a function of the value of certain columns.

    Somebody have made this before? any example available?

    I have been looking the radpivotGrid that has this feature natively but I have to use the radGridView.

    Thanks

    Francisco
  2. Answer
    Dess
    Admin
    Dess avatar
    1607 posts

    Posted 05 Nov 2014 Link to this post

    Hello Francisco,

    Thank you for writing.

    Currently, RadChartView supports integration only with RadPivotGrid out of the box. However, you can easily implement similar behavior between RadChartView and RadGridView. It is necessary to repopulate the RadChartView when the RadGridView selection is changed. Here is a sample code snippet demonstrating how to achieve the behavior illustrated in the attached gif file:
    public Form1()
    {
        InitializeComponent();
     
        this.radGridView1.SelectionMode = Telerik.WinControls.UI.GridViewSelectionMode.CellSelect;
        this.radGridView1.MultiSelect = true;
    }
     
    private void radGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if (this.radGridView1.SelectedCells.Count > 0)
        {
            PopulateChart(this.radGridView1.SelectedCells);
        }
    }
     
    private void PopulateChart(GridViewSelectedCellsCollection gridViewSelectedCellsCollection)
    {
        List<string> orderIds = new List<string>();
        this.radChartView1.Series.Clear();
        this.radChartView1.Axes.Clear();
        this.radChartView1.ShowLegend = true;
     
        foreach (var cell in gridViewSelectedCellsCollection)
        {
            double cellValue;
            if (double.TryParse(cell.Value + "", out cellValue))
            {
                BarSeries barSeries;
                DataRowView rowView = cell.RowInfo.DataBoundItem as DataRowView;
                if (!orderIds.Contains(rowView.Row["OrderID"].ToString()))
                {
                    orderIds.Add(rowView.Row["OrderID"].ToString());
                    barSeries = new BarSeries();
                    barSeries.Name = rowView.Row["OrderID"].ToString();
                    barSeries.LegendTitle = barSeries.Name;
                  
                    this.radChartView1.Series.Add(barSeries);
                }
                else
                {
                    barSeries = GetBarSeries(rowView.Row["OrderID"].ToString()) as BarSeries ;
                }
                barSeries.DataPoints.Add(new CategoricalDataPoint(cellValue, cell.ColumnInfo.Name)); 
            }
        }
     
        this.radChartView1.Invalidate();
    }
     
    private ChartSeries GetBarSeries(string p)
    {
        foreach (ChartSeries s in this.radChartView1.Series)
        {
            if (s.Name == p)
            {
                return s;
            }
        }
     
        return null;
    }

    Note that this is just a sample solution and it may not cover all possible cases. Feel free to modify it on a way which suits your requirement best.

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Desislava
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. FMorales
    FMorales avatar
    26 posts
    Member since:
    Aug 2014

    Posted 06 Nov 2014 in reply to Dess Link to this post

    Hi Desislava,

    I have maked the link without problems and my result code is very similar to your proposal.

    Thanks a lot, we are changing all our old Xceed grid used in a lot of applications by Telerik controls and ... I am happy to realize that your controls are simply ...fantastic.

    Thanks a lot.
Back to Top